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>
15#include <jsonv/value.hpp>
16
17#include <typeinfo>
18
19namespace 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
30template <typename T>
32 public serializer
33{
34public:
35 /// \see serializer::get_type
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
48protected:
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.
60template <typename T>
62 public serializer_for<T>
63{
64protected:
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.
76 virtual value to_json(const serialization_context& context, const T& from) const = 0;
77};
78
79/// \}
80
81}
Provides extra information to routines used for deserialization and serialization.
Definition context.hpp:26
Provides extra information to routines used for serialization: the formats to find other serializers ...
Definition serialize.hpp:42
A serializer for type T.
virtual const std::type_info & get_type() const noexcept override
virtual void serialize(const serialization_context &context, const void *from, writer &to) const override
virtual void serialize(const serialization_context &context, const T &from, writer &to) const =0
Write from into to.
A serializer holds the method for converting an arbitrary C++ type into JSON, written token by token ...
A base for serializers written against the older value -based interface.
virtual value to_json(const serialization_context &context, const T &from) const =0
Convert from into a value.
virtual void serialize(const serialization_context &context, const T &from, writer &to) const final override
Write from into to.
Represents a single JSON value, which can be any one of a potential kind, each behaving slightly diff...
Definition value.hpp:156
A writer instance writes a JSON ast_node sequence to some form of sink: an encoder,...
Definition writer.hpp:68
Copyright (c) 2014-2020 by Travis Gockel.
#define JSONV_NODISCARD
Warn if the caller discards the result of this function.
Definition config.hpp:143
Serialization of C++ types into a JSON token stream.
Copyright (c) 2015-2026 by Travis Gockel.
Copyright (c) 2012-2020 by Travis Gockel.