Mercurial > minori
annotate include/core/json.h @ 377:1b0b8e746d83 default tip
CI/windows: stupid msys2
author | Paper <paper@tflc.us> |
---|---|
date | Fri, 25 Jul 2025 12:40:25 -0400 |
parents | 47c9f8502269 |
children |
rev | line source |
---|---|
261
3ec7804abf17
include: make header guards more sane
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
1 #ifndef MINORI_CORE_JSON_H_ |
3ec7804abf17
include: make header guards more sane
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
2 #define MINORI_CORE_JSON_H_ |
64 | 3 |
175 | 4 #include "json/json.hpp" |
64 | 5 |
198
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
6 #include <optional> |
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
7 |
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
8 namespace nlohmann { |
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
9 |
369 | 10 /* hack to get std::optional working */ |
198
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
11 template<typename T> |
369 | 12 void to_json(nlohmann::json &j, const std::optional<T> &v) |
13 { | |
198
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
14 if (v.has_value()) |
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
15 j = v.value(); |
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
16 else |
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
17 j = nullptr; |
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
18 } |
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
19 |
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
20 template<typename T> |
369 | 21 void from_json(const nlohmann::json &j, std::optional<T> &v) |
22 { | |
220 | 23 v = j.is_null() ? std::nullopt : j.get<T>(); |
198
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
24 } |
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
25 |
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
26 } // namespace nlohmann |
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
27 |
9 | 28 namespace JSON { |
64 | 29 |
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
30 template<typename T = std::string> |
369 | 31 T GetString(const nlohmann::json &json, const nlohmann::json::json_pointer &ptr, T def) |
32 { | |
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
33 if (json.contains(ptr) && json[ptr].is_string()) |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
34 return json[ptr].get<T>(); |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
35 else |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
36 return def; |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
37 } |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
38 |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
39 template<typename T = int> |
369 | 40 T GetNumber(const nlohmann::json &json, const nlohmann::json::json_pointer &ptr, T def = 0) |
41 { | |
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
42 if (json.contains(ptr) && json[ptr].is_number()) |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
43 return json[ptr].get<T>(); |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
44 else |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
45 return def; |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
46 } |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
47 |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
48 template<typename T = std::vector<std::string>> |
369 | 49 T GetArray(const nlohmann::json &json, const nlohmann::json::json_pointer &ptr, T def = 0) |
50 { | |
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
51 if (json.contains(ptr) && json[ptr].is_array()) |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
52 return json[ptr].get<T>(); |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
53 else |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
54 return def; |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
55 } |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
56 |
369 | 57 nlohmann::json GetValue(const nlohmann::json &json, const nlohmann::json::json_pointer &ptr); |
58 bool GetBoolean(const nlohmann::json &json, const nlohmann::json::json_pointer &ptr, bool def = false); | |
64 | 59 |
9 | 60 } // namespace JSON |
64 | 61 |
261
3ec7804abf17
include: make header guards more sane
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
62 #endif // MINORI_CORE_JSON_H_ |