nfx-serialization 0.9.3
Cross-platform C++ JSON serialization library with extensible trait capabilities
Loading...
Searching...
No Matches
DateTimeTraits.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// TimeSpan support - enabled only if header is available
46//=====================================================================
47
48#if __has_include( <nfx/datetime/TimeSpan.h>)
49
50# include <nfx/datetime/TimeSpan.h>
51
52namespace nfx::serialization::json
53{
57 template <>
58 struct SerializationTraits<nfx::time::TimeSpan>
59 {
66 static void serialize( const nfx::time::TimeSpan& obj, Builder& builder )
67 {
68 builder.write( obj.toString() );
69 }
70
76 static void fromDocument( const Document& doc, nfx::time::TimeSpan& obj )
77 {
78 if( doc.is<std::string>( "" ) )
79 {
80 auto val = doc.get<std::string>( "" );
81 if( val.has_value() && !val.value().empty() )
82 {
83 if( !nfx::time::TimeSpan::fromString( val.value(), obj ) )
84 {
85 throw std::runtime_error{ "Invalid TimeSpan format: expected ISO 8601 duration string" };
86 }
87 }
88 }
89 }
90 };
91} // namespace nfx::serialization::json
92
93#endif // __has_include(<nfx/datetime/TimeSpan.h>)
94
95//=====================================================================
96// DateTime support - enabled only if header is available
97//=====================================================================
98
99#if __has_include( <nfx/datetime/DateTime.h>)
100
101# include <nfx/datetime/DateTime.h>
102
103namespace nfx::serialization::json
104{
108 template <>
109 struct SerializationTraits<nfx::time::DateTime>
110 {
117 static void serialize( const nfx::time::DateTime& obj, Builder& builder )
118 {
119 builder.write( obj.toString( nfx::time::DateTime::Format::Iso8601Precise ) );
120 }
121
127 static void fromDocument( const Document& doc, nfx::time::DateTime& obj )
128 {
129 if( doc.is<std::string>( "" ) )
130 {
131 auto val = doc.get<std::string>( "" );
132 if( val.has_value() && !val.value().empty() )
133 {
134 if( !nfx::time::DateTime::fromString( val.value(), obj ) )
135 {
136 throw std::runtime_error{ "Invalid DateTime format: expected ISO 8601 string" };
137 }
138 }
139 }
140 }
141 };
142} // namespace nfx::serialization::json
143
144#endif // __has_include(<nfx/datetime/DateTime.h>)
145
146//=====================================================================
147// DateTimeOffset support - enabled only if header is available
148//=====================================================================
149
150#if __has_include( <nfx/datetime/DateTimeOffset.h>)
151
152# include <nfx/datetime/DateTimeOffset.h>
153
154namespace nfx::serialization::json
155{
159 template <>
160 struct SerializationTraits<nfx::time::DateTimeOffset>
161 {
168 static void serialize( const nfx::time::DateTimeOffset& obj, Builder& builder )
169 {
170 builder.write( obj.toString( nfx::time::DateTime::Format::Iso8601Precise ) );
171 }
172
178 static void fromDocument( const Document& doc, nfx::time::DateTimeOffset& obj )
179 {
180 if( doc.is<std::string>( "" ) )
181 {
182 auto val = doc.get<std::string>( "" );
183 if( val.has_value() && !val.value().empty() )
184 {
185 if( !nfx::time::DateTimeOffset::fromString( val.value(), obj ) )
186 {
187 throw std::runtime_error{ "Invalid DateTimeOffset format: expected ISO 8601 string" };
188 }
189 }
190 }
191 }
192 };
193} // namespace nfx::serialization::json
194
195#endif // __has_include(<nfx/datetime/DateTimeOffset.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).