32 template <
typename ObjectType>
41 template <
typename ObjectType>
42 bool ToJsonString(
const ObjectType&
object, std::string& jsonString);
50 template <
typename ObjectType>
59 template <
typename ObjectType>
60 bool ToObject(
const std::string& jsonString, ObjectType&
object);
68 template <
typename ObjectType>
69 bool ToObject(
const char* jsonString, ObjectType&
object);
96 struct FloatingPointSchema;
102 template <
typename ElementSchema,
typename ElementType>
126 template <
typename EnumDescription>
134 template <
typename EnumType>
142 template <
typename EnumType>
164 template <
typename ObjectDescription>
178 template <
typename ValueType,
typename RequiredType,
typename SchemaType,
size_t KeyPathSize>
190 template <
typename SchemaType,
typename RequiredType,
typename ObjectType>
191 auto make_field(
const char* key, ObjectType& field);
193 template <
typename SchemaType,
typename RequiredType,
typename ObjectType,
size_t ArraySize>
194 auto make_field(
const std::array<const char*, ArraySize>& keyPath, ObjectType& field);
202 template <
typename RequiredType,
typename ObjectType>
203 auto make_field(
const char* key, ObjectType& field);
205 template <
typename RequiredType,
typename ObjectType,
size_t ArraySize>
206 auto make_field(
const std::array<const char*, ArraySize>& keyPath, ObjectType& field);
213 template <
typename ObjectType>
214 auto make_field(
const char* key, ObjectType& field);
216 template <
typename ObjectType,
size_t ArraySize>
217 auto make_field(
const std::array<const char*, ArraySize>& keyPath, ObjectType& field);
234 template <
typename ObjectType,
typename Enable =
void>
240 template <
typename ObjectType>
246 template <
typename ...Args>
247 std::array<
const char*,
sizeof...(Args)>
MakeKeyPath(Args... args);
252 template <
typename ObjectType>
259 template <
typename ObjectType>
263 if (
ToJson(
object, value))
266 jsonString = writer.
write(value);
276 template <
typename ObjectType>
283 template <
typename ObjectType>
288 if (reader.
parse(jsonString, value))
299 template <
typename ObjectType>
302 return ToObject(std::string{jsonString}, object);
308 template <
typename IntegralType>
321 output =
static_cast<IntegralType
>(value.
asUInt());
331 template <
typename IntegralType>
342 template <
typename FloatingPo
intType>
351 output =
static_cast<FloatingPointType
>(value.
asDouble());
361 template <
typename FloatingPo
intType>
364 value =
static_cast<double>(input);
474 template <
typename ElementSchema,
typename ElementType>
477 template <
typename ContainerType>
485 for (
const auto& element : value)
487 output.emplace_back();
488 if (!ElementSchema::Parse(element, output.back()))
499 template <
typename ContainerType>
502 for (
const auto& element : input)
505 if (ElementSchema::Emit(element, value))
507 value.
append(elementValue);
520 template <
typename EnumDescription>
523 template <
typename EnumType>
532 return FindEnumMatch(EnumDescription::EnumMap(), value, output);
536 template <
typename EnumType>
539 return FindStringMatch(EnumDescription::EnumMap(), input, value);
543 template <
typename MapTuple,
typename EnumType>
546 return FindEnumMatchFromIndex<0>(tuple, value, output);
550 template <
int index,
typename MapTuple,
typename EnumType>
553 auto mapping = std::get<index>(tuple);
554 if (mapping.Match(value))
556 output = mapping.GetValue();
561 return FindEnumMatchFromIndex<index + 1>(tuple, value, output);
566 template <
int index,
typename MapTuple,
typename EnumType>
569 return ApplyDefault<EnumDescription>(output, 0);
573 template <
typename Description,
typename EnumType,
typename = decltype(Description::GetFallbackValue())>
577 output = Description::GetFallbackValue();
582 template <
typename Description,
typename EnumType>
592 template <
typename MapTuple,
typename EnumType>
595 return FindStringMatchFromIndex<0>(tuple, input, value);
598 template <
int index,
typename MapTuple,
typename EnumType>
601 auto mapping = std::get<index>(tuple);
602 if (mapping.GetValue() == input)
604 value = mapping.GetString();
609 return FindStringMatchFromIndex<index + 1>(tuple, input, value);
613 template <
int index,
typename MapTuple,
typename EnumType>
621 template <
typename EnumType>
632 template <
typename InputType>
633 constexpr
bool Match(
const InputType& type)
const 635 return type == mString;
656 template <
typename EnumType>
663 template <
typename ObjectDescription>
666 template <
typename OutputType>
674 if (ParseValues(value, ObjectDescription::BindFields(output)))
686 template <
typename InputType>
689 if (EmitValues(value, ObjectDescription::BindFields(input)))
702 template <
typename FieldsTuple>
705 return ParseValuesAtIndex<0>(value, std::forward<FieldsTuple>(tuple));
709 template <
int index,
typename FieldsTuple>
712 auto field = std::get<index>(tuple);
713 if (!field.Parse(value) && decltype(field)::IsRequired)
719 return ParseValuesAtIndex<index + 1>(value, std::forward<FieldsTuple>(tuple));
724 template <
int index,
typename FieldsTuple>
731 template <
typename FieldsTuple>
734 return EmitValuesAtIndex<0>(value, std::forward<FieldsTuple&&>(tuple));
738 template <
int index,
typename FieldsTuple>
741 auto field = std::get<index>(tuple);
742 if (!field.Emit(value) && decltype(field)::IsRequired)
748 return EmitValuesAtIndex<index + 1>(value, std::forward<FieldsTuple>(tuple));
753 template <
int index,
typename FieldsTuple>
763 static constexpr
bool IsRequired =
true;
768 static constexpr
bool IsRequired =
false;
772 template <
typename ValueType,
typename RequiredType,
typename SchemaType>
776 static constexpr
bool IsRequired = RequiredType::IsRequired;
787 return SchemaType::Parse(parent[mKey], mValue);
793 return SchemaType::Emit(mValue, parent[mKey]);
803 template <
typename ValueType,
typename RequiredType,
typename SchemaType,
size_t ArraySize>
806 static_assert(ArraySize != 0,
"Key path for JSON field must have at least one key.");
809 static constexpr
bool IsRequired = RequiredType::IsRequired;
820 return ParseHelper<0>(parent);
826 return EmitHelper<0>(parent);
834 const auto& jIntermediate = parent[mKeyPath[index]];
836 if(jIntermediate.isNull() || !jIntermediate.isObject())
841 return ParseHelper<index + 1>(jIntermediate);
848 return SchemaType::Parse(parent[mKeyPath[index]], mValue);
855 auto& jIntermediate = parent[mKeyPath[index]];
857 return EmitHelper<index + 1>(jIntermediate);
864 return SchemaType::Emit(mValue, parent[mKeyPath[index]]);
873 template <
typename SchemaType,
typename RequiredType,
typename ObjectType>
880 template <
typename SchemaType,
typename RequiredType,
typename ObjectType,
size_t ArraySize>
887 template <
typename RequiredType,
typename ObjectType>
890 return make_field<DefaultSchema<std::decay_t<ObjectType>>, RequiredType, ObjectType>(key, field);
894 template <
typename RequiredType,
typename ObjectType,
size_t ArraySize>
897 return make_field<DefaultSchema<std::decay_t<ObjectType>>, RequiredType, ObjectType, ArraySize>(keyPath, field);
901 template <
typename ObjectType>
904 return make_field<DefaultSchema<std::decay_t<ObjectType>>,
OptionalField, ObjectType>(key, field);
908 template <
typename ObjectType,
size_t ArraySize>
911 return make_field<DefaultSchema<std::decay_t<ObjectType>>,
OptionalField, ObjectType, ArraySize>(keyPath, field);
915 template <
typename ObjectType,
typename Enable>
918 static_assert(
sizeof(ObjectType) < 0,
"Default schema not found for object.");
922 template <
typename IntegralType>
929 template <
typename FloatingPo
intType>
950 template <
typename ElementType>
957 template <
typename ...Args>
960 return std::array<
const char*,
sizeof...(Args)>{{args...}};
bool ParseNum(const std::string &str, int &out)
static bool Parse(const ttv::json::Value &value, bool &output)
Definition: jsonserialization.h:396
std::array< const char *, ArraySize > mKeyPath
Definition: jsonserialization.h:868
bool RFC3339TimeToUnixTimestamp(const std::string &str, Timestamp &result)
static bool Parse(const ttv::json::Value &value, ttv::Color &output)
Definition: jsonserialization.h:452
static bool Parse(const ttv::json::Value &value, ContainerType &output)
Definition: jsonserialization.h:478
Definition: jsonserialization.h:340
bool Parse(const ttv::json::Value &parent)
Definition: jsonserialization.h:785
static bool Emit(IntegralType input, ttv::json::Value &value)
Definition: jsonserialization.h:332
std::enable_if_t<(index< ArraySize - 1), bool > EmitHelper(ttv::json::Value &parent)
Definition: jsonserialization.h:853
Definition: jsonserialization.h:761
std::array< const char *, sizeof...(Args)> MakeKeyPath(Args... args)
Definition: jsonserialization.h:958
JsonField(const char *key, ValueType &object)
Definition: jsonserialization.h:778
static bool Emit(EnumType input, ttv::json::Value &value)
Definition: jsonserialization.h:537
static bool Parse(const ttv::json::Value &value, std::string &output)
Definition: jsonserialization.h:372
static std::enable_if_t<(index< std::tuple_size< MapTuple >::value), bool > FindStringMatchFromIndex(const MapTuple &tuple, EnumType input, ttv::json::Value &value)
Definition: jsonserialization.h:599
static bool Parse(const ttv::json::Value &value, IntegralType &output)
Definition: jsonserialization.h:309
Definition: jsonserialization.h:127
static bool EmitValues(ttv::json::Value &value, FieldsTuple &&tuple)
Definition: jsonserialization.h:732
Definition: jsonserialization.h:418
Definition: jsonserialization.h:135
auto make_enum_mapping(const char *string, EnumType value)
Definition: jsonserialization.h:657
static std::enable_if_t<(index >=std::tuple_size< FieldsTuple >::value), bool > EmitValuesAtIndex(ttv::json::Value &, FieldsTuple &&)
Definition: jsonserialization.h:754
EnumType mValue
Definition: jsonserialization.h:652
static bool ApplyDefault(EnumType &output, int)
Definition: jsonserialization.h:574
auto make_field(const char *key, ObjectType &field)
Definition: jsonserialization.h:874
std::string asString() const
static bool Emit(const bool &input, ttv::json::Value &value)
Definition: jsonserialization.h:410
std::enable_if_t<(index< ArraySize - 1), bool > ParseHelper(const ttv::json::Value &parent)
Definition: jsonserialization.h:832
ValueType & mValue
Definition: jsonserialization.h:799
Definition: cpp11transition.h:22
uint32_t Timestamp
Definition: coretypes.h:27
static bool FindEnumMatch(const MapTuple &tuple, const ttv::json::Value &value, EnumType &output)
Definition: jsonserialization.h:544
bool ToObject(const ttv::json::Value &value, ObjectType &object)
Definition: jsonserialization.h:277
bool Parse(const ttv::json::Value &parent)
Definition: jsonserialization.h:818
static bool Emit(const InputType &input, ttv::json::Value &value)
Definition: jsonserialization.h:687
bool parse(const std::string &document, Value &root, bool collectComments=true)
Read a Value from a JSON document.
static std::enable_if_t<(index >=std::tuple_size< FieldsTuple >::value), bool > ParseValuesAtIndex(const ttv::json::Value &, FieldsTuple &&)
Definition: jsonserialization.h:725
constexpr const char * GetString() const
Definition: jsonserialization.h:645
typename DefaultSchemaProvider< ObjectType >::Type DefaultSchema
Definition: jsonserialization.h:241
Definition: jsonserialization.h:235
Definition: jsonserialization.h:370
JSON (JavaScript Object Notation).
Definition: adsapi.h:16
Definition: jsonserialization.h:766
static std::enable_if_t<(index< std::tuple_size< FieldsTuple >::value), bool > EmitValuesAtIndex(ttv::json::Value &value, FieldsTuple &&tuple)
Definition: jsonserialization.h:739
json::UInt64 UInt
Definition: value.h:125
static bool Parse(const ttv::json::Value &value, FloatingPointType &output)
Definition: jsonserialization.h:343
Definition: jsonserialization.h:165
bool Emit(ttv::json::Value &parent)
Definition: jsonserialization.h:824
Represents a JSON value.
Definition: value.h:114
Unserialize a JSON document into a Value.
Definition: reader.h:18
static bool Emit(const std::string &input, ttv::json::Value &value)
Definition: jsonserialization.h:386
constexpr EnumType GetValue() const
Definition: jsonserialization.h:639
ValueType & mValue
Definition: jsonserialization.h:869
static bool Parse(const ttv::json::Value &value, EnumType &output)
Definition: jsonserialization.h:524
bool ToJsonString(const ObjectType &object, std::string &jsonString)
Definition: jsonserialization.h:260
static bool ParseValues(const ttv::json::Value &value, FieldsTuple &&tuple)
Definition: jsonserialization.h:703
Definition: jsonserialization.h:306
static std::enable_if_t<(index< std::tuple_size< MapTuple >::value), bool > FindEnumMatchFromIndex(const MapTuple &tuple, const ttv::json::Value &value, EnumType &output)
Definition: jsonserialization.h:551
constexpr bool Match(const InputType &type) const
Definition: jsonserialization.h:633
static std::enable_if_t<(index >=std::tuple_size< MapTuple >::value), bool > FindStringMatchFromIndex(const MapTuple &, EnumType, ttv::json::Value &)
Definition: jsonserialization.h:614
std::enable_if_t<(index==ArraySize - 1), bool > EmitHelper(ttv::json::Value &parent)
Definition: jsonserialization.h:862
Value & append(const Value &value)
Append value to array at the end.
static std::enable_if_t<(index >=std::tuple_size< MapTuple >::value), bool > FindEnumMatchFromIndex(const MapTuple &, const ttv::json::Value &, EnumType &output)
Definition: jsonserialization.h:567
std::string UnixTimestampToRFC3339String(Timestamp timestamp)
'null' value
Definition: value.h:28
ValueType
Type of the value held by a Value object.
Definition: value.h:26
std::enable_if_t<(index==ArraySize - 1), bool > ParseHelper(const ttv::json::Value &parent)
Definition: jsonserialization.h:846
static bool Emit(ttv::Timestamp input, ttv::json::Value &value)
Definition: jsonserialization.h:442
static std::enable_if_t<(index< std::tuple_size< FieldsTuple >::value), bool > ParseValuesAtIndex(const ttv::json::Value &value, FieldsTuple &&tuple)
Definition: jsonserialization.h:710
const char * mKey
Definition: jsonserialization.h:798
Definition: jsonserialization.h:103
constexpr EnumMapping(const char *string, EnumType value)
Definition: jsonserialization.h:625
Definition: jsonserialization.h:179
bool ToJson(const ObjectType &object, ttv::json::Value &value)
Definition: jsonserialization.h:253
static bool ApplyDefault(EnumType &, long)
Definition: jsonserialization.h:583
bool ParseColor(const std::string &str, Color &result)
static bool FindStringMatch(const MapTuple &tuple, EnumType input, ttv::json::Value &value)
Definition: jsonserialization.h:593
uint32_t Color
Definition: coretypes.h:28
static bool Emit(FloatingPointType input, ttv::json::Value &value)
Definition: jsonserialization.h:362
virtual std::string write(const Value &root)
Definition: jsonserialization.h:450
bool Emit(ttv::json::Value &parent)
Definition: jsonserialization.h:791
static bool Emit(const ContainerType &input, ttv::json::Value &value)
Definition: jsonserialization.h:500
Outputs a Value in JSON format without formatting (not human friendly).
Definition: writer.h:31
Definition: jsonserialization.h:394
static bool Parse(const ttv::json::Value &value, OutputType &output)
Definition: jsonserialization.h:667
const char * mString
Definition: jsonserialization.h:651
static bool Parse(const ttv::json::Value &value, ttv::Timestamp &output)
Definition: jsonserialization.h:420
JsonField(const std::array< const char *, ArraySize > &keyPath, ValueType &object)
Definition: jsonserialization.h:811