27 const std::string& key;
28 const json::json& node;
33 template <
class T,
class CheckFunction>
34 void insertAs(CheckFunction&& checkFunction,
const char* valueStr) {
35 if(!(node[
"value"].*checkFunction)())
36 throw Exception(
"sub-node '%s' not regconized as %s", key, valueStr);
37 T value = node[
"value"];
38 map.insert(key, value);
44 void insertAsArrayOf() {
45 Array<T> array = node[
"value"];
46 map.insert(key, array);
53 std::vector<std::string> keys;
54 keys.reserve(map_.size());
55 for(
auto it = map_.begin(), end = map_.end(); it != end; ++it)
56 keys.push_back(it->first);
61 std::vector<TypeID> types;
62 types.reserve(map_.size());
63 for(
auto it = map_.begin(), end = map_.end(); it != end; ++it)
64 types.push_back(it->second.type());
71 }
catch(std::out_of_range&) {
72 throw Exception(
"no key '%s' exists", key);
80 }
catch(std::out_of_range&) {
81 throw Exception(
"no key '%s' exists", key);
91 for(
auto it = map_.cbegin(), end = map_.cend(); it != end; ++it) {
93 const std::string& key = it->first;
95 jsonNode[key][
"type_id"] =
static_cast<int>(value.
type());
101 case TypeID::Boolean:
104 valueNode.push_back(v);
106 valueNode = value.
as<
bool>();
112 valueNode.push_back(v);
114 valueNode = value.
as<
int>();
120 valueNode.push_back(v);
122 valueNode = value.
as<std::int64_t>();
125 case TypeID::Float32:
128 valueNode.push_back(v);
130 valueNode = value.
as<
float>();
133 case TypeID::Float64:
136 valueNode.push_back(v);
138 valueNode = value.
as<
double>();
144 valueNode.push_back(v);
146 valueNode = value.
as<std::string>();
153 jsonNode[key][
"value"] = valueNode;
162 if(jsonNode.is_null() || jsonNode.empty())
165 for(
auto it = jsonNode.begin(), end = jsonNode.end(); it != end; ++it) {
167 if(!it->count(
"type_id"))
168 throw Exception(
"sub-node '%s' has no node 'type_id'", it.key());
170 if(!it->count(
"value"))
171 throw Exception(
"sub-node '%s' has no node 'value'", it.key());
173 const json::json& node = it.value();
174 const std::string& key = it.key();
175 const int typeAsInt = node[
"type_id"];
179 InsertHelper insertHelper{*
this, key, node};
182 case TypeID::Boolean:
184 insertHelper.insertAsArrayOf<
bool>();
186 insertHelper.insertAs<
bool>(&json::json::is_boolean,
"boolean");
191 insertHelper.insertAsArrayOf<
int>();
193 insertHelper.insertAs<
int>(&json::json::is_number_integer,
"integer");
198 insertHelper.insertAsArrayOf<std::int64_t>();
200 insertHelper.insertAs<std::int64_t>(&json::json::is_number_integer,
"integer");
203 case TypeID::Float32:
205 insertHelper.insertAsArrayOf<
float>();
207 insertHelper.insertAs<
float>(&json::json::is_number,
"floating pointer number");
210 case TypeID::Float64:
212 insertHelper.insertAsArrayOf<
double>();
214 insertHelper.insertAs<
double>(&json::json::is_number,
"floating pointer number");
219 insertHelper.insertAsArrayOf<std::string>();
221 insertHelper.insertAs<std::string>(&json::json::is_string,
"string");
231 std::stringstream ss;
234 for(
auto it = s.
begin(), end = s.
end(); it != end; ++it) {
235 ss <<
"\"" << it->first <<
"\": ";
240 ss << it->second.as<std::string>();
243 if(++itCp != s.
end())
247 return (stream << ss.str());
static std::string toString(const Array< T > &array)
Convert to string.
static bool isArray(TypeID id) noexcept
Check if type is an array of types.
std::vector< T > Array
Array class used by serialbox to store meta-information.
Namespace of the serialbox library.
TypeID
Type-id of types recognized by serialbox.
static TypeID getPrimitive(TypeID id) noexcept
Return the underlying primitve type.
#define serialbox_unreachable(msg)
Marks that the current location is not supposed to be reachable.
std::ostream & operator<<(std::ostream &stream, const FieldID &f)
Convert FieldID to stream.
Exception class which stores a human-readable error description.