Reflection for RapidJSON  0.0.15
Reflection for serializing/deserializing with RapidJSON
reflector-chronoutilities.h
Go to the documentation of this file.
1 #ifndef REFLECTIVE_RAPIDJSON_JSON_REFLECTOR_CHRONO_UTILITIES_H
2 #define REFLECTIVE_RAPIDJSON_JSON_REFLECTOR_CHRONO_UTILITIES_H
3 
11 #include "./reflector.h"
12 
13 #include <c++utilities/chrono/datetime.h>
14 #include <c++utilities/chrono/timespan.h>
15 #include <c++utilities/conversion/conversionexception.h>
16 
17 namespace ReflectiveRapidJSON {
18 namespace JsonReflector {
19 
20 // define functions to "push" values to a RapidJSON array or object
21 
22 template <>
23 inline void push<CppUtilities::DateTime>(
24  const CppUtilities::DateTime &reflectable, RAPIDJSON_NAMESPACE::Value &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator)
25 {
26  const std::string str(reflectable.toIsoString());
27  value.SetString(str.data(), rapidJsonSize(str.size()), allocator);
28 }
29 
30 template <>
31 inline void push<CppUtilities::TimeSpan>(
32  const CppUtilities::TimeSpan &reflectable, RAPIDJSON_NAMESPACE::Value &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator)
33 {
34  const std::string str(reflectable.toString());
35  value.SetString(str.data(), rapidJsonSize(str.size()), allocator);
36 }
37 
38 // define functions to "pull" values from a RapidJSON array or object
39 
40 template <>
41 inline void pull<CppUtilities::DateTime>(CppUtilities::DateTime &reflectable,
42  const RAPIDJSON_NAMESPACE::GenericValue<RAPIDJSON_NAMESPACE::UTF8<char>> &value, JsonDeserializationErrors *errors)
43 {
44  std::string str;
45  pull(str, value, errors);
46  try {
47  reflectable = CppUtilities::DateTime::fromIsoStringGmt(str.data());
48  } catch (const CppUtilities::ConversionException &) {
49  if (errors) {
51  }
52  }
53 }
54 
55 template <>
56 inline void pull<CppUtilities::TimeSpan>(CppUtilities::TimeSpan &reflectable,
57  const RAPIDJSON_NAMESPACE::GenericValue<RAPIDJSON_NAMESPACE::UTF8<char>> &value, JsonDeserializationErrors *errors)
58 {
59  std::string str;
60  pull(str, value, errors);
61  try {
62  reflectable = CppUtilities::TimeSpan::fromString(str.data());
63  } catch (const CppUtilities::ConversionException &) {
64  if (errors) {
66  }
67  }
68 }
69 
70 } // namespace JsonReflector
71 } // namespace ReflectiveRapidJSON
72 
73 #endif // REFLECTIVE_RAPIDJSON_JSON_REFLECTOR_CHRONO_UTILITIES_H
ReflectiveRapidJSON::JsonDeserializationErrors
The JsonDeserializationErrors struct can be passed to fromJson() for error handling.
Definition: errorhandling.h:154
reflector.h
Contains BinaryReader and BinaryWriter supporting binary (de)serialization of primitive and custom ty...
ReflectiveRapidJSON::JsonReflector::pull
void pull(Type &reflectable, const RAPIDJSON_NAMESPACE::GenericValue< RAPIDJSON_NAMESPACE::UTF8< char >>::ConstObject &value, JsonDeserializationErrors *errors)
Pulls the reflectable which has a custom type from the specified object.
Definition: reflector-boosthana.h:40
ReflectiveRapidJSON::JsonReflector::rapidJsonSize
constexpr RAPIDJSON_NAMESPACE::SizeType rapidJsonSize(std::size_t size)
Casts the specified size to the size type used by RapidJSON ensuring no overflow happens.
Definition: reflector.h:51
ReflectiveRapidJSON::JsonDeserializationErrors::reportConversionError
void reportConversionError(JsonType jsonType)
Reports a conversion error.
Definition: errorhandling.h:239
ReflectiveRapidJSON
Definition: traits.h:13
ReflectiveRapidJSON::JsonType::String
@ String