nfx-serialization 0.3.0
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
44
45//=====================================================================
46// Int128 support - enabled only if header is available
47//=====================================================================
48
49#if __has_include( "nfx/datatypes/Int128.h" )
50
51# include "nfx/datatypes/Int128.h"
52
53namespace nfx::serialization::json
54{
58 template <>
59 struct SerializationTraits<nfx::datatypes::Int128>
60 {
69 static void serialize( const nfx::datatypes::Int128& obj, Document& doc )
70 {
71 std::string value = obj.toString();
72 doc.set<std::string>( "", value );
73 }
74
82 static void deserialize( nfx::datatypes::Int128& obj, const Document& doc )
83 {
84 if ( doc.is<std::string>( "" ) )
85 {
86 auto val = doc.get<std::string>( "" );
87 if ( val.has_value() && !val.value().empty() )
88 {
89 if ( !nfx::datatypes::Int128::fromString( val.value(), obj ) )
90 {
91 throw std::runtime_error( "Invalid Int128 format: unable to parse string representation" );
92 }
93 }
94 }
95 }
96 };
97} // namespace nfx::serialization::json
98
99#endif // __has_include("nfx/datatypes/Int128.h")
100
101//=====================================================================
102// Decimal support - enabled only if header is available
103//=====================================================================
104
105#if __has_include( "nfx/datatypes/Decimal.h" )
106
107# include "nfx/datatypes/Decimal.h"
108
109namespace nfx::serialization::json
110{
114 template <>
115 struct SerializationTraits<nfx::datatypes::Decimal>
116 {
124 static void serialize( const nfx::datatypes::Decimal& obj, Document& doc )
125 {
126 std::string value = obj.toString();
127 doc.set<std::string>( "", value );
128 }
129
137 static void deserialize( nfx::datatypes::Decimal& obj, const Document& doc )
138 {
139 if ( doc.is<std::string>( "" ) )
140 {
141 auto val = doc.get<std::string>( "" );
142 if ( val.has_value() && !val.value().empty() )
143 {
144 if ( !nfx::datatypes::Decimal::fromString( val.value(), obj ) )
145 {
146 throw std::runtime_error( "Invalid Decimal format: unable to parse string representation" );
147 }
148 }
149 }
150 }
151 };
152} // namespace nfx::serialization::json
153
154#endif // __has_include("nfx/datatypes/Decimal.h")
Generic document abstraction for JSON serialization.
Serialization traits and type specializations for JSON serialization.
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.