nfx-serialization 0.9.3
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
44
45#pragma once
46
47#include <nfx/json/Builder.h>
48#include <nfx/json/Document.h>
49
50#include <stdexcept>
51#include <variant>
52#include <string>
53#include <type_traits>
54#include <vector>
55
56using namespace nfx::json;
57
58namespace nfx::serialization::json
59{
60 //=====================================================================
61 // Forward declarations
62 //=====================================================================
63
64 template <typename T>
65 class Serializer;
66
67 template <typename T>
69
70 //=====================================================================
71 // SFINAE detectors
72 //=====================================================================
73
74 namespace detail
75 {
80 template <typename T, typename = void>
81 struct has_streaming_serialization : std::false_type
82 {
83 };
84
90 template <typename T>
92 T,
93 std::void_t<decltype( SerializationTraits<T>::serialize(
94 std::declval<const T&>(), std::declval<nfx::json::Builder&>() ) )>> : std::true_type
95 {
96 };
97
101 template <typename T>
103
108 template <typename T, typename = void>
109 struct has_factory_deserialization : std::false_type
110 {
111 };
112
118 template <typename T>
120 T,
121 std::void_t<decltype( SerializationTraits<T>::fromDocument( std::declval<const Document&>() ) )>>
122 : std::true_type
123 {
124 };
125
129 template <typename T>
131 } // namespace detail
132
133 //=====================================================================
134 // SerializationTraits - Read/Write serialization interface
135 //=====================================================================
136
201 template <typename T>
203 {
210 static void fromDocument( const Document& doc, T& obj )
211 {
212 Serializer<T> serializer;
213 obj.fromDocument( doc, serializer );
214 }
215
216 // No default implementation for serialize() - must be specialized if needed
217 // SFINAE detector will check if serialize() is available
218 };
219
226 template <>
227 struct SerializationTraits<std::monostate>
228 {
233 static void serialize( const std::monostate&, Builder& builder )
234 {
235 builder.write( nullptr );
236 }
237
243 static void fromDocument( const Document& doc, std::monostate& )
244 {
245 // monostate is always empty - no data to deserialize
246 // Just verify the JSON is null
247 if( !doc.isNull( "" ) )
248 {
249 throw std::runtime_error{ "Expected null for std::monostate" };
250 }
251 }
252 };
253} // namespace nfx::serialization::json
constexpr bool has_streaming_serialization_v
Helper variable template for has_streaming_serialization.
constexpr bool has_factory_deserialization_v
Helper variable template for has_factory_deserialization.
Serialization traits template (forward declaration).
static void fromDocument(const Document &doc, T &obj)
Convert Document to object (deserialization).
Templated JSON serializer with compile-time type mapping.
Definition Serializer.h:55
static void fromDocument(const Document &doc, std::monostate &)
Deserialize std::monostate from JSON null.
static void serialize(const std::monostate &, Builder &builder)
Serialize std::monostate to JSON null.