JSON Voorhees
Killer JSON for C++
Loading...
Searching...
No Matches
serializer_for.hpp
Go to the documentation of this file.
1
/// \file jsonv/serialization/serializer_for.hpp
2
///
3
/// Copyright (c) 2015-2026 by Travis Gockel. All rights reserved.
4
///
5
/// This program is free software: you can redistribute it and/or modify it under the terms of the Apache License
6
/// as published by the Apache Software Foundation, either version 2 of the License, or (at your option) any later
7
/// version.
8
///
9
/// \author Travis Gockel (travis@gockelhut.com)
10
#pragma once
11
12
#include <
jsonv/config.hpp
>
13
#include <
jsonv/serialization/serialize.hpp
>
14
#include <
jsonv/serialization/serializer.hpp
>
15
#include <
jsonv/value.hpp
>
16
17
#include <typeinfo>
18
19
namespace
jsonv
20
{
21
22
/// \addtogroup Serialization
23
/// \{
24
25
/// A \c serializer for type \c T. This is a utility class which converts the `const void*` used in the \c serializer
26
/// interface into the more-friendly \c T.
27
///
28
/// \see adapter_for
29
/// \see value_serializer_for
30
template
<
typename
T>
31
class
serializer_for
:
32
public
serializer
33
{
34
public
:
35
/// \see serializer::get_type
36
JSONV_NODISCARD
37
virtual
const
std::type_info&
get_type
() const noexcept
override
38
{
39
return
typeid
(T);
40
}
41
42
/// \see serializer::serialize
43
virtual
void
serialize
(
const
serialization_context
&
context
,
const
void
* from,
writer
& to)
const override
44
{
45
serialize
(
context
, *
static_cast<
const
T*
>
(from), to);
46
}
47
48
protected
:
49
/// Write \a from into \a to.
50
///
51
/// \see serializer::serialize
52
virtual
void
serialize
(
const
serialization_context
&
context
,
const
T& from,
writer
& to)
const
= 0;
53
};
54
55
/// A base for serializers written against the older \c value -based interface.
56
///
57
/// The \c value the subclass's \c to_json builds is written whole into the writer with \c writer::write. This costs the
58
/// tree the \c writer exists to avoid -- so it is a stepping stone for ports, not a destination. New serializers should
59
/// derive from \c serializer_for and write into the \c writer.
60
template
<
typename
T>
61
class
value_serializer_for
:
62
public
serializer_for
<T>
63
{
64
protected
:
65
virtual
void
serialize
(
const
serialization_context
&
context
,
const
T& from,
writer
& to)
const
final
override
66
{
67
to.write(
to_json
(
context
, from));
68
}
69
70
/// Convert \a from into a \c value.
71
///
72
/// \param context Extra information to help you encode sub-objects for your type, such as the ability to find other
73
/// \c formats.
74
/// \param from The value to convert.
75
JSONV_NODISCARD
76
virtual
value
to_json
(
const
serialization_context
&
context
,
const
T& from)
const
= 0;
77
};
78
79
/// \}
80
81
}
jsonv::context
Provides extra information to routines used for deserialization and serialization.
Definition
context.hpp:26
jsonv::serialization_context
Provides extra information to routines used for serialization: the formats to find other serializers ...
Definition
serialize.hpp:42
jsonv::serializer_for
A serializer for type T.
Definition
serializer_for.hpp:33
jsonv::serializer_for::get_type
virtual const std::type_info & get_type() const noexcept override
Definition
serializer_for.hpp:37
jsonv::serializer_for::serialize
virtual void serialize(const serialization_context &context, const void *from, writer &to) const override
Definition
serializer_for.hpp:43
jsonv::serializer_for::serialize
virtual void serialize(const serialization_context &context, const T &from, writer &to) const =0
Write from into to.
jsonv::serializer
A serializer holds the method for converting an arbitrary C++ type into JSON, written token by token ...
Definition
serializer.hpp:27
jsonv::value_serializer_for
A base for serializers written against the older value -based interface.
Definition
serializer_for.hpp:63
jsonv::value_serializer_for::to_json
virtual value to_json(const serialization_context &context, const T &from) const =0
Convert from into a value.
jsonv::value_serializer_for::serialize
virtual void serialize(const serialization_context &context, const T &from, writer &to) const final override
Write from into to.
Definition
serializer_for.hpp:65
jsonv::value
Represents a single JSON value, which can be any one of a potential kind, each behaving slightly diff...
Definition
value.hpp:156
jsonv::writer
A writer instance writes a JSON ast_node sequence to some form of sink: an encoder,...
Definition
writer.hpp:68
config.hpp
Copyright (c) 2014-2020 by Travis Gockel.
JSONV_NODISCARD
#define JSONV_NODISCARD
Warn if the caller discards the result of this function.
Definition
config.hpp:143
serialize.hpp
Serialization of C++ types into a JSON token stream.
serializer.hpp
Copyright (c) 2015-2026 by Travis Gockel.
value.hpp
Copyright (c) 2012-2020 by Travis Gockel.
jsonv
serialization
serializer_for.hpp
Generated by
1.9.8