49#if __has_include( <nfx/datetime/TimeSpan.h>)
51# include <nfx/datetime/TimeSpan.h>
53namespace nfx::serialization::json
66 static void serialize(
const nfx::time::TimeSpan& obj, Document& doc )
68 doc.set<int64_t>(
"", obj.ticks() );
76 static void deserialize( nfx::time::TimeSpan& obj,
const Document& doc )
78 if ( doc.is<
int>(
"" ) )
80 auto ticksVal = doc.get<int64_t>(
"" );
81 if ( ticksVal.has_value() )
83 obj = nfx::time::TimeSpan( ticksVal.value() );
88 throw std::runtime_error(
"Invalid TimeSpan format: expected integer ticks" );
100#if __has_include( <nfx/datetime/DateTime.h>)
102# include <nfx/datetime/DateTime.h>
104namespace nfx::serialization::json
117 static void serialize(
const nfx::time::DateTime& obj, Document& doc )
119 std::string value = obj.toIso8601Extended();
120 doc.set<std::string>(
"", value );
128 static void deserialize( nfx::time::DateTime& obj,
const Document& doc )
130 if ( doc.is<std::string>(
"" ) )
132 auto val = doc.get<std::string>(
"" );
133 if ( val.has_value() && !val.value().empty() )
135 if ( !nfx::time::DateTime::fromString( val.value(), obj ) )
137 throw std::runtime_error(
"Invalid DateTime format: expected ISO 8601 string" );
151#if __has_include( <nfx/datetime/DateTimeOffset.h>)
153# include <nfx/datetime/DateTimeOffset.h>
155namespace nfx::serialization::json
168 static void serialize(
const nfx::time::DateTimeOffset& obj, Document& doc )
170 std::string value = obj.toIso8601Extended();
171 doc.set<std::string>(
"", value );
179 static void deserialize( nfx::time::DateTimeOffset& obj,
const Document& doc )
181 if ( doc.is<std::string>(
"" ) )
183 auto val = doc.get<std::string>(
"" );
184 if ( val.has_value() && !val.value().empty() )
186 if ( !nfx::time::DateTimeOffset::fromString( val.value(), obj ) )
188 throw std::runtime_error(
"Invalid DateTimeOffset format: expected ISO 8601 string" );
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.