@@ -14,21 +14,28 @@ bool JsonExporter::toJson(const Any& any, nlohmann::json& dst) const
1414 nlohmann::json json;
1515 auto const & type = any.castedType ();
1616
17+ const std::string type_field = " __type" ;
18+ const std::string value_field = " value" ;
19+
1720 if (any.isString ())
1821 {
19- dst = any.cast <std::string>();
22+ dst[type_field] = " string" ;
23+ dst[value_field] = any.cast <std::string>();
2024 }
2125 else if (type == typeid (int64_t ))
2226 {
23- dst = any.cast <int64_t >();
27+ dst[type_field] = " int64_t" ;
28+ dst[value_field] = any.cast <int64_t >();
2429 }
2530 else if (type == typeid (uint64_t ))
2631 {
27- dst = any.cast <uint64_t >();
32+ dst[type_field] = " uint64_t" ;
33+ dst[value_field] = any.cast <uint64_t >();
2834 }
2935 else if (type == typeid (double ))
3036 {
31- dst = any.cast <double >();
37+ dst[type_field] = " double" ;
38+ dst[value_field] = any.cast <double >();
3239 }
3340 else
3441 {
@@ -51,32 +58,36 @@ JsonExporter::ExpectedEntry JsonExporter::fromJson(const nlohmann::json& source)
5158 {
5259 return nonstd::make_unexpected (" json object is null" );
5360 }
54- if (source.is_string ())
55- {
56- return Entry{ BT::Any (source.get <std::string>()),
57- BT::TypeInfo::Create<std::string>() };
58- }
59- if (source.is_number_unsigned ())
60- {
61- return Entry{ BT::Any (source.get <uint64_t >()), BT::TypeInfo::Create<uint64_t >() };
62- }
63- if (source.is_number_integer ())
64- {
65- return Entry{ BT::Any (source.get <int64_t >()), BT::TypeInfo::Create<int64_t >() };
66- }
67- if (source.is_number_float ())
68- {
69- return Entry{ BT::Any (source.get <double >()), BT::TypeInfo::Create<double >() };
70- }
71- if (source.is_boolean ())
61+ if (!source.contains (" __type" ))
7262 {
73- return Entry{ BT::Any (source. get < bool >()), BT::TypeInfo::Create< bool >() } ;
63+ return nonstd::make_unexpected ( " Missing field '__type' " ) ;
7464 }
7565
76- if (! source.contains (" __type " ))
66+ if ( source.contains (" value " ))
7767 {
78- return nonstd::make_unexpected (" Missing field '__type'" );
68+ if (source[" value" ].is_string ())
69+ {
70+ return Entry{ BT::Any (source[" value" ].get <std::string>()),
71+ BT::TypeInfo::Create<std::string>() };
72+ }
73+ if (source[" value" ].is_number_unsigned ())
74+ {
75+ return Entry{ BT::Any (source[" value" ].get <uint64_t >()), BT::TypeInfo::Create<uint64_t >() };
76+ }
77+ if (source[" value" ].is_number_integer ())
78+ {
79+ return Entry{ BT::Any (source[" value" ].get <int64_t >()), BT::TypeInfo::Create<int64_t >() };
80+ }
81+ if (source[" value" ].is_number_float ())
82+ {
83+ return Entry{ BT::Any (source[" value" ].get <double >()), BT::TypeInfo::Create<double >() };
84+ }
85+ if (source[" value" ].is_boolean ())
86+ {
87+ return Entry{ BT::Any (source[" value" ].get <bool >()), BT::TypeInfo::Create<bool >() };
88+ }
7989 }
90+
8091 auto type_it = type_names_.find (source[" __type" ]);
8192 if (type_it == type_names_.end ())
8293 {
0 commit comments