@@ -14,21 +14,28 @@ bool JsonExporter::toJson(const Any& any, nlohmann::json& dst) const
14
14
nlohmann::json json;
15
15
auto const & type = any.castedType ();
16
16
17
+ const std::string type_field = " __type" ;
18
+ const std::string value_field = " value" ;
19
+
17
20
if (any.isString ())
18
21
{
19
- dst = any.cast <std::string>();
22
+ dst[type_field] = " string" ;
23
+ dst[value_field] = any.cast <std::string>();
20
24
}
21
25
else if (type == typeid (int64_t ))
22
26
{
23
- dst = any.cast <int64_t >();
27
+ dst[type_field] = " int64_t" ;
28
+ dst[value_field] = any.cast <int64_t >();
24
29
}
25
30
else if (type == typeid (uint64_t ))
26
31
{
27
- dst = any.cast <uint64_t >();
32
+ dst[type_field] = " uint64_t" ;
33
+ dst[value_field] = any.cast <uint64_t >();
28
34
}
29
35
else if (type == typeid (double ))
30
36
{
31
- dst = any.cast <double >();
37
+ dst[type_field] = " double" ;
38
+ dst[value_field] = any.cast <double >();
32
39
}
33
40
else
34
41
{
@@ -51,32 +58,39 @@ JsonExporter::ExpectedEntry JsonExporter::fromJson(const nlohmann::json& source)
51
58
{
52
59
return nonstd::make_unexpected (" json object is null" );
53
60
}
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" ))
72
62
{
73
- return Entry{ BT::Any (source. get < bool >()), BT::TypeInfo::Create< bool >() } ;
63
+ return nonstd::make_unexpected ( " Missing field '__type' " ) ;
74
64
}
75
65
76
- if (! source.contains (" __type " ))
66
+ if (source.contains (" value " ))
77
67
{
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 >()),
76
+ BT::TypeInfo::Create<uint64_t >() };
77
+ }
78
+ if (source[" value" ].is_number_integer ())
79
+ {
80
+ return Entry{ BT::Any (source[" value" ].get <int64_t >()),
81
+ BT::TypeInfo::Create<int64_t >() };
82
+ }
83
+ if (source[" value" ].is_number_float ())
84
+ {
85
+ return Entry{ BT::Any (source[" value" ].get <double >()),
86
+ BT::TypeInfo::Create<double >() };
87
+ }
88
+ if (source[" value" ].is_boolean ())
89
+ {
90
+ return Entry{ BT::Any (source[" value" ].get <bool >()), BT::TypeInfo::Create<bool >() };
91
+ }
79
92
}
93
+
80
94
auto type_it = type_names_.find (source[" __type" ]);
81
95
if (type_it == type_names_.end ())
82
96
{
0 commit comments