Smolgui
Immediate gui library based on SFML
Loading...
Searching...
No Matches
SerializeCore.h
Go to the documentation of this file.
1#pragma once
2
3#include <unordered_map>
4#include <nlohmann/json.hpp>
5
7
8// for readability
9using json = nlohmann::json;
10
11namespace sgui
12{
16template <typename Key, typename Type>
17void to_json (json& j, const std::unordered_map <Key, Type>& table)
18{
19 // note that user should use NLOHMANN_JSON_SERIALIZE_ENUM if they
20 // want to have human readable enumeration in json if Key is an Enum
21 for (const auto& element : table) {
22 j [element.first] = element.second;
23 }
24}
25
26template <typename Key, typename Type>
27void from_json (const json& j, std::unordered_map <Key, Type>& table)
28{
29 // note that user should use NLOHMANN_JSON_SERIALIZE_ENUM if they
30 // want to have human readable enumeration in json if Key is an Enum
31 for (auto elem = j.begin (); elem != j.end (); elem++) {
32 const Key key = elem.key ();
33 const Type value = elem.value ();
34 table.insert ({key, value});
35 }
36}
37
38
42template <typename Object, typename ObjectId>
44{
45 for (const auto id : pool.ids ()) {
46 j [id] = pool.get (id);
47 }
48}
49
50template <typename Object, typename ObjectId>
52{
53 for (auto elem = j.begin (); elem != j.end (); elem++) {
54 const ObjectId id = elem.key ();
55 const Object value = elem.value ();
56 pool.add (value, id);
57 }
58}
59
60} // namespace sgui
nlohmann::json json
Definition LoadJson.h:8
: implement a generic pool of objects that are stored continuously in memory without fragmentation,...
Definition ObjectPool.h:16
std::vector< ObjectId > ids() const
Get list of all identifiers used to store object.
Object & get(const ObjectId &object)
Get oject in pool.
Object & add(Object &&object, const ObjectId &id)
Add object in pool.
Definition Interpolation.h:16
void from_json(const json &j, std::unordered_map< Key, Type > &table)
Definition SerializeCore.h:27
void to_json(json &j, const std::unordered_map< Key, Type > &table)
Definition SerializeCore.h:17