13#ifndef MDE_SERIALIZATION_H
14#define MDE_SERIALIZATION_H
18#include <nlohmann/json.hpp>
24using JSON = nlohmann::json;
69template<
typename MapT>
73 ret.push_back(JSON::array({JSON::array({
i.first.left,
i.first.right}),
i.second}));
86template<
typename MapT>
90 if (!
obj.is_array()) {
95 if (!
tuple.is_array() || !(
tuple.size() == 2)) {
115template<
typename MapT>
118 for (
auto &
i : map) {
119 JSON::array({
i.first,
i.second});
132template<
typename MapT>
136 if (!
obj.is_array()) {
141 if (!
tuple.is_array() || !(
tuple.size() == 2)) {
158template<
typename StoreT,
typename Serializer>
180template<
typename MDET,
typename Serializer>
183 using PropertyElement =
typename MDET::PropertyElement;
185 if (!
obj.is_array()) {
195 data.push_back(PropertyElement(
serializer.load(
i)));
197 mde.register_set(std::move(data));
209template<
typename Serializer,
typename StoreT>
221 },
elem.get_value());
235template <
typename Tuple, std::size_t...
Is>
240template <
typename Tuple>
242 constexpr std::size_t
N = std::tuple_size_v<Tuple>;
255template<
typename Serializer,
typename MDET>
258 using PropertyElement =
typename MDET::PropertyElement;
259 using ChildValueList =
typename MDET::Nesting::ChildValueList;
260 constexpr const Size num_children = MDET::Nesting::num_children;
262 if (!
obj.is_array()) {
274 }
else if (!
i[1].
is_array() ||
i[1].size() != num_children) {
276 "Expected array of size " + std::to_string(num_children) +
282 mde.register_set(std::move(data));
286template<
typename MDET>
312 },
root.get_reflist());
326template<
typename MDET>
337template<
typename MDET>
363 },
root.get_reflist());
372template<
typename MDET>
391 return JSON::to_bson(
obj);
414 std::ofstream
f(
file_path, std::ios::out | std::ios::binary);
421 f.write(
reinterpret_cast<const char *
>(data.data()), data.size());
432 return JSON::parse(
f);
442 return JSON::from_bson(
f);
445template<
typename MDET>
450template<
typename MDET>
455template<
typename MDET>
460template<
typename MDET>
Any common components go into this file.
#define MDE_VERSION_STRING
void load_bson(MDET &mde, const String &file_path)
void register_storage_from_json(MDET &mde, const JSON &obj, Serializer &serializer)
Inserts data from the JSON representation to the equivalent C++ data strucure for a PropertySetStorag...
void register_storage_from_json_nested(MDET &mde, const JSON &obj, Serializer &serializer)
Inserts data from the JSON representation to the equivalent C++ data strucure for a PropertySetStorag...
void load(MDET &mde, const String &file_path)
bool json_to_file_bson(const JSON &obj, const String &file_path)
bool save_bson(const MDET &mde, const String &file_path)
JSON unary_operation_map_to_json(const MapT &map)
Converts an integer -> integer map to JSON.
Tuple json_list_to_tuple_internal(const JSON &l, std::index_sequence< Is... >)
void unary_operation_map_from_json(MapT &map, const JSON &obj)
Inserts data from the JSON representation to the equivalent C++ data strucure for the integer -> inte...
void mde_from_json_internal(MDET &root, const JSON &obj, HashSet< void * > &visited, String &path)
JSON binary_operation_map_to_json(const MapT &map)
Converts OperationNode -> integer maps to JSON.
Tuple json_list_to_tuple(const JSON &l)
JSON mde_to_json(MDET &root)
Converts an MDE and its referenced child MDEs to JSON. The individual MDEs are identified using paths...
bool save(const MDET &mde, const String &file_path)
JSON load_bson_file(const String &file_path)
Vector< uint8_t > json_to_bson(const JSON &obj)
JSON storage_array_to_json_nested(const StoreT &store, Serializer &serializer)
Converts an MDE storage array to JSON in the nested case.
bool json_to_file(const JSON &obj, const String &file_path)
Writes a JSON object to a file.
void binary_operation_map_from_json(MapT &map, const JSON &obj)
Inserts data from the JSON representation to the equivalent C++ data strucure for the OperationNode -...
String json_to_string(const JSON &obj)
Returns a string representation of the supplied JSON object.
void mde_to_json_internal(MDET &root, JSON &obj, HashSet< void * > &visited, String &path)
JSON load_json_file(const String &file_path)
JSON storage_array_to_json(const StoreT &store, Serializer &serializer)
Converts a storage array to its JSON representation.
void mde_from_json(MDET &root, const JSON &obj)
Loads all data from the supplied JSON object into MDE.
std::hash< T > DefaultHash
Default serialization behavior. If the type of the property in MDE is something that can be directly ...
SerializationError(const String &message)
Struct for serializing/deserializing values in MDE.