Mercurial > minori
annotate include/core/json.h @ 318:3b355fa948c7
config: use TOML instead of INI
unfortunately, INI is not enough, and causes some paths including
semicolons to break with our current storage of the library folders.
so, I decided to switch to TOML which does support real arrays...
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Wed, 12 Jun 2024 05:25:41 -0400 |
parents | b1f4d1867ab1 |
children | 1686fac290c5 |
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 |
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
10 template<typename T> |
258 | 11 void to_json(nlohmann::json& j, const std::optional<T>& v) { |
198
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
12 if (v.has_value()) |
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
13 j = v.value(); |
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
14 else |
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
15 j = nullptr; |
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
16 } |
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
17 |
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
18 template<typename T> |
258 | 19 void from_json(const nlohmann::json& j, std::optional<T>& v) { |
220 | 20 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
|
21 } |
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
22 |
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
23 } // namespace nlohmann |
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
24 |
9 | 25 namespace JSON { |
64 | 26 |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
261
diff
changeset
|
27 /* TODO: refactor these to return a std::optional... */ |
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
28 template<typename T = std::string> |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
29 T GetString(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr, T def) { |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
30 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
|
31 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
|
32 else |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
33 return def; |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
34 } |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
35 |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
36 template<typename T = int> |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
37 T GetNumber(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr, T def = 0) { |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
38 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
|
39 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
|
40 else |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
41 return def; |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
42 } |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
43 |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
44 template<typename T = std::vector<std::string>> |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
45 T GetArray(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr, T def = 0) { |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
46 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
|
47 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
|
48 else |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
49 return def; |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
50 } |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
51 |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
52 nlohmann::json GetValue(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr); |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
53 bool GetBoolean(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr, bool def = false); |
64 | 54 |
9 | 55 } // namespace JSON |
64 | 56 |
261
3ec7804abf17
include: make header guards more sane
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
57 #endif // MINORI_CORE_JSON_H_ |