nfx-serialization 0.9.3
Cross-platform C++ JSON serialization library with extensible trait capabilities
Loading...
Searching...
No Matches
DatatypesTraits.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
39
40#pragma once
41
43
44//=====================================================================
45// Int128 support - enabled only if header is available
46//=====================================================================
47
48#if __has_include( "nfx/datatypes/Int128.h" )
49
50# include "nfx/datatypes/Int128.h"
51
52// Register Int128 as nfx extension type
53namespace nfx::serialization::json::detail
54{
55 template <>
56 struct is_nfx_extension_type<nfx::datatypes::Int128> : std::true_type
57 {
58 };
59} // namespace nfx::serialization::json::detail
60
61namespace nfx::serialization::json
62{
66 template <>
67 struct SerializationTraits<nfx::datatypes::Int128>
68 {
75 static void serialize( const nfx::datatypes::Int128& obj, nfx::json::Builder& builder )
76 {
77 std::string value = obj.toString();
78 builder.write( value );
79 }
80
88 static void fromDocument( const Document& doc, nfx::datatypes::Int128& obj )
89 {
90 if( doc.is<std::string>( "" ) )
91 {
92 auto val = doc.get<std::string>( "" );
93 if( val.has_value() && !val.value().empty() )
94 {
95 if( !nfx::datatypes::Int128::fromString( val.value(), obj ) )
96 {
97 throw std::runtime_error{ "Invalid Int128 format: unable to parse string representation" };
98 }
99 }
100 }
101 }
102 };
103} // namespace nfx::serialization::json
104
105#endif // __has_include("nfx/datatypes/Int128.h")
106
107//=====================================================================
108// Decimal support - enabled only if header is available
109//=====================================================================
110
111#if __has_include( "nfx/datatypes/Decimal.h" )
112
113# include "nfx/datatypes/Decimal.h"
114
115// Register Decimal as nfx extension type
116namespace nfx::serialization::json::detail
117{
118 template <>
119 struct is_nfx_extension_type<nfx::datatypes::Decimal> : std::true_type
120 {
121 };
122} // namespace nfx::serialization::json::detail
123
124namespace nfx::serialization::json
125{
129 template <>
130 struct SerializationTraits<nfx::datatypes::Decimal>
131 {
138 static void serialize( const nfx::datatypes::Decimal& obj, nfx::json::Builder& builder )
139 {
140 std::string value = obj.toString();
141 builder.write( value );
142 }
143
151 static void fromDocument( const Document& doc, nfx::datatypes::Decimal& obj )
152 {
153 if( doc.is<std::string>( "" ) )
154 {
155 auto val = doc.get<std::string>( "" );
156 if( val.has_value() && !val.value().empty() )
157 {
158 if( !nfx::datatypes::Decimal::fromString( val.value(), obj ) )
159 {
160 throw std::runtime_error{ "Invalid Decimal format: unable to parse string representation" };
161 }
162 }
163 }
164 }
165 };
166} // namespace nfx::serialization::json
167
168#endif // __has_include("nfx/datatypes/Decimal.h")
Templated JSON serializer with compile-time type mapping.
Serialization traits template (forward declaration).
static void fromDocument(const Document &doc, T &obj)
Convert Document to object (deserialization).
Identifies types from nfx:: namespaces that have SerializationTraits.
Definition Concepts.h:115