nfx-serialization 0.3.0
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
44
45//=====================================================================
46// TimeSpan support - enabled only if header is available
47//=====================================================================
48
49#if __has_include( <nfx/datetime/TimeSpan.h>)
50
51# include <nfx/datetime/TimeSpan.h>
52
53namespace nfx::serialization::json
54{
58 template <>
59 struct SerializationTraits<nfx::time::TimeSpan>
60 {
66 static void serialize( const nfx::time::TimeSpan& obj, Document& doc )
67 {
68 doc.set<int64_t>( "", obj.ticks() );
69 }
70
76 static void deserialize( nfx::time::TimeSpan& obj, const Document& doc )
77 {
78 if ( doc.is<int>( "" ) )
79 {
80 auto ticksVal = doc.get<int64_t>( "" );
81 if ( ticksVal.has_value() )
82 {
83 obj = nfx::time::TimeSpan( ticksVal.value() );
84 }
85 }
86 else
87 {
88 throw std::runtime_error( "Invalid TimeSpan format: expected integer ticks" );
89 }
90 }
91 };
92} // namespace nfx::serialization::json
93
94#endif // __has_include(<nfx/datetime/TimeSpan.h>)
95
96//=====================================================================
97// DateTime support - enabled only if header is available
98//=====================================================================
99
100#if __has_include( <nfx/datetime/DateTime.h>)
101
102# include <nfx/datetime/DateTime.h>
103
104namespace nfx::serialization::json
105{
109 template <>
110 struct SerializationTraits<nfx::time::DateTime>
111 {
117 static void serialize( const nfx::time::DateTime& obj, Document& doc )
118 {
119 std::string value = obj.toIso8601Extended();
120 doc.set<std::string>( "", value );
121 }
122
128 static void deserialize( nfx::time::DateTime& obj, const Document& doc )
129 {
130 if ( doc.is<std::string>( "" ) )
131 {
132 auto val = doc.get<std::string>( "" );
133 if ( val.has_value() && !val.value().empty() )
134 {
135 if ( !nfx::time::DateTime::fromString( val.value(), obj ) )
136 {
137 throw std::runtime_error( "Invalid DateTime format: expected ISO 8601 string" );
138 }
139 }
140 }
141 }
142 };
143} // namespace nfx::serialization::json
144
145#endif // __has_include(<nfx/datetime/DateTime.h>)
146
147//=====================================================================
148// DateTimeOffset support - enabled only if header is available
149//=====================================================================
150
151#if __has_include( <nfx/datetime/DateTimeOffset.h>)
152
153# include <nfx/datetime/DateTimeOffset.h>
154
155namespace nfx::serialization::json
156{
160 template <>
161 struct SerializationTraits<nfx::time::DateTimeOffset>
162 {
168 static void serialize( const nfx::time::DateTimeOffset& obj, Document& doc )
169 {
170 std::string value = obj.toIso8601Extended();
171 doc.set<std::string>( "", value );
172 }
173
179 static void deserialize( nfx::time::DateTimeOffset& obj, const Document& doc )
180 {
181 if ( doc.is<std::string>( "" ) )
182 {
183 auto val = doc.get<std::string>( "" );
184 if ( val.has_value() && !val.value().empty() )
185 {
186 if ( !nfx::time::DateTimeOffset::fromString( val.value(), obj ) )
187 {
188 throw std::runtime_error( "Invalid DateTimeOffset format: expected ISO 8601 string" );
189 }
190 }
191 }
192 }
193 };
194} // namespace nfx::serialization::json
195
196#endif // __has_include(<nfx/datetime/DateTimeOffset.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.