nfx-serialization 0.3.0
Cross-platform C++ JSON serialization library with extensible trait capabilities
Loading...
Searching...
No Matches
SerializationTraits.h
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) 2025 nfx
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
40
41#pragma once
42
43#include <stdexcept>
44#include <string>
45#include <vector>
46
48
49namespace nfx::serialization::json
50{
51 //=====================================================================
52 // Forward declarations
53 //=====================================================================
54
55 template <typename T>
56 class Serializer;
57
58 namespace detail
59 {
60 template <typename T>
62
63 template <typename T>
65
66 template <typename T>
68
69 template <typename T>
71 } // namespace detail
72
73 //=====================================================================
74 // Serialization Traits (extensible by users)
75 //=====================================================================
76
84 template <typename T>
86 {
92 static void serialize( const T& obj, Document& doc )
93 {
94 // Look for serialize method with no parameters
96 {
97 doc = obj.serialize();
98 }
99 // Look for serialize method returning Document with serializer parameter
101 {
102 Serializer<T> serializer;
103 doc = obj.serialize( serializer );
104 }
105 // Look for traditional serialize method with serializer and document parameters
106 else if constexpr ( detail::has_serialize_method<T>::value )
107 {
108 Serializer<T> serializer;
109 obj.serialize( serializer, doc );
110 }
111 else
112 {
116 "Type must either specialize SerializationTraits or have a serialize() member method" );
117 }
118 }
119
125 static void deserialize( T& obj, const Document& doc )
126 {
127 // Look for member deserialize method
129 {
130 Serializer<T> serializer;
131 obj.deserialize( serializer, doc );
132 }
133 else
134 {
136 "Type must either specialize SerializationTraits or have a deserialize() member method" );
137 }
138 }
139 };
140} // namespace nfx::serialization::json
Generic document abstraction for JSON serialization.
Generic JSON document abstraction for serialization.
Definition Document.h:59
Templated JSON serializer with compile-time type mapping.
Definition Serializer.h:52
Document serialize(const T &obj) const
Serialize object to JSON document.
T deserialize(const Document &doc) const
Deserialize object from JSON document.
Default serialization traits - users can specialize this.
static void serialize(const T &obj, Document &doc)
Default serialize implementation - delegates to member method.
static void deserialize(T &obj, const Document &doc)
Default deserialize implementation - delegates to member method.