Reflection for RapidJSON  0.0.3
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<ChronoUtilities::DateTime>(
24  const ChronoUtilities::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<ChronoUtilities::TimeSpan>(
32  const ChronoUtilities::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<ChronoUtilities::DateTime>(ChronoUtilities::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 = ChronoUtilities::DateTime::fromIsoStringGmt(str.data());
48  } catch (const ConversionUtilities::ConversionException &) {
49  if (errors) {
50  errors->reportConversionError(JsonType::String);
51  }
52  }
53 }
54 
55 template <>
56 inline void pull<ChronoUtilities::TimeSpan>(ChronoUtilities::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 = ChronoUtilities::TimeSpan::fromString(str.data());
63  } catch (const ConversionUtilities::ConversionException &) {
64  if (errors) {
65  errors->reportConversionError(JsonType::String);
66  }
67  }
68 }
69 
70 } // namespace JsonReflector
71 } // namespace ReflectiveRapidJSON
72 
73 #endif // REFLECTIVE_RAPIDJSON_JSON_REFLECTOR_CHRONO_UTILITIES_H
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:47
The JsonDeserializationErrors struct can be passed to fromJson() for error handling.
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.
Contains functions to (de)serialize basic types such as int, double, bool, std::string, std::vector, ...