Thoth
A functional, expressive, asynchronous C++26 webdev library
Loading...
Searching...
No Matches
Json.hpp
Go to the documentation of this file.
1#pragma once
2#include <variant>
3#include <memory>
4#include <vector>
5#include <string>
6#include <optional>
7#include <expected>
8#include <concepts>
9#include <span>
10
13
14namespace Thoth::NJson {
15
16 struct Json{
17 using Value = std::variant<Null, String, Number, Bool, Object, Array>;
18 using PredicatePointer = bool(*)(const Json&);
19
20 // NOLINTBEGIN(*)
22 Json(const JsonObject& child);
23 Json(JsonObject&& child);
24
25 Json(const Array& child);
26 Json(Array&& child);
27
28 Json(Value&& newValue) noexcept;
29 Json(const Value& newValue);
30 Json(Json&& other) noexcept;
31 Json(const Json& other);
32
33 Json(bool other);
34 template<class T>
35 requires std::floating_point<T> || std::integral<T> && (!std::same_as<T, bool>)
36 Json(T other);
37 template<class T>
38 requires std::constructible_from<std::string, T>
39 Json(T&& other);
40
41 Json& operator=(const JsonObject& other);
43
44 Json& operator=(const Array& child);
45 Json& operator=(Array&& child);
46
47 Json& operator=(Value&& newValue) noexcept;
48 Json& operator=(const Value& newValue);
49 Json& operator=(Json&& other) noexcept;
50 Json& operator=(const Json& other);
51
52 Json& operator=(bool other);
53 template<class T>
54 requires std::floating_point<T> || std::integral<T> && (!std::same_as<T, bool>)
55 Json& operator=(T other);
56 template<class T>
57 requires std::constructible_from<std::string, T>
58 Json& operator=(T&& other);
59
60
61 operator Value&();
62 [[nodiscard]] operator const Value&() const;
63 // NOLINTEND(*)
64
65 template<class T>
66 static bool IsOfType(const Json& val);
67 template<class T>
68 [[nodiscard]] bool IsOf() const;
69
70 template<class T>
71 static T& AsType(Json& val);
72 template<class T>
73 [[nodiscard]] T& As();
74 template<class T>
75 [[nodiscard]] const T& As() const;
76
77 template<class T>
78 [[nodiscard]] T& AsMut();
79 template<class T>
80 [[nodiscard]] T AsMov() &&;
81 template<class T>
82 [[nodiscard]] const T& AsRef() const;
83
84 template<class T>
85 std::optional<T*> Ensure();
86 template<class T>
87 std::optional<T*> Ensure() const;
88
89 template<class T>
90 std::optional<T*> EnsureMut();
91 template<class T>
92 std::optional<T> EnsureMov() &&;
93 template<class T>
94 std::optional<const T*> EnsureRef() const;
95
96 template<class T>
97 std::expected<T*, Http::RequestError> EnsureOrError();
98 template<class T>
99 std::expected<T*, Http::RequestError> EnsureOrError() const;
100
101 template<class T>
102 std::expected<T*, Http::RequestError> EnsureMutOrError();
103 template<class T>
104 std::expected<T, Http::RequestError> EnsureMovOrError() &&;
105 template<class T>
106 std::expected<const T*, Http::RequestError> EnsureRefOrError() const;
107
108 [[nodiscard]] bool operator==(const Json& other) const;
109
114 static std::expected<Json, Http::RequestError> Parse(std::string_view input);
115
116
122 static std::expected<Json, Http::RequestError> ParseText(std::string_view input, bool copyData = true, bool checkFinal = true);
123
124
125#pragma region Get Functions
129
133 [[nodiscard]] OptCRefValWrapper Get(Key key) const;
135 [[nodiscard]] OptValWrapper GetCopy(Key key) const;
138
139
141 [[nodiscard]] ValWrapper GetCopyOrNull(Key key) const;
144
145
149 [[nodiscard]] ExpCRefValWrapper GetOrError(Key key) const;
151 [[nodiscard]] ExpValWrapper GetCopyOrError(Key key) const;
154
156
157#pragma endregion
158
159#pragma region Find Functions
163
167 [[nodiscard]] OptCRefValWrapper Find(Keys keys) const;
169 [[nodiscard]] OptValWrapper FindCopy(Keys keys) const;
172
173
175 [[nodiscard]] ValWrapper FindCopyOrNull(Keys keys) const;
178
179
183 [[nodiscard]] ExpCRefValWrapper FindOrError(Keys keys) const;
185 [[nodiscard]] ExpValWrapper FindCopyOrError(Keys keys) const;
188
190#pragma endregion
191
192#pragma region Search Functions
196
198 template<class Pred = PredicatePointer> requires std::predicate<Pred, Json>
201 template<class Pred = PredicatePointer> requires std::predicate<Pred, Json>
202 [[nodiscard]] OptCRefValWrapper Search(Pred&& pred) const;
204 template<class Pred = PredicatePointer> requires std::predicate<Pred, Json>
205 [[nodiscard]] OptValWrapper SearchCopy(Pred&& pred) const;
207 template<class Pred = PredicatePointer> requires std::predicate<Pred, Json>
209
211 template<class Pred = PredicatePointer> requires std::predicate<Pred, Json>
212 [[nodiscard]] ValWrapper SearchCopyOrNull(Pred&& pred) const;
214 template<class Pred = PredicatePointer> requires std::predicate<Pred, Json>
216
217
219 template<class Pred = PredicatePointer> requires std::predicate<Pred, Json>
222 template<class Pred = PredicatePointer> requires std::predicate<Pred, Json>
223 [[nodiscard]] ExpCRefValWrapper SearchOrError(Pred&& pred) const;
225 template<class Pred = PredicatePointer> requires std::predicate<Pred, Json>
226 [[nodiscard]] ExpValWrapper SearchCopyOrError(Pred&& pred) const;
228 template<class Pred = PredicatePointer> requires std::predicate<Pred, Json>
230
232#pragma endregion
233
234
236 template<class Callable>
237 constexpr decltype(auto) Visit(Callable&& callable);
238
240 template<class Callable>
241 [[nodiscard]] constexpr decltype(auto) Visit(Callable&& callable) const;
242 private:
243 Value _value;
244 };
245
246 inline static constexpr Null NullV{};
247 inline static Json NullJ{};
248}
249
250
251#include <Thoth/NJson/Json.tpp>
Definition Definitions.hpp:19
std::vector< Json > Array
Definition Definitions.hpp:29
std::expected< CRefValWrapper, Http::RequestError > ExpCRefValWrapper
Definition Definitions.hpp:54
std::expected< ValWrapper, Http::RequestError > ExpValWrapper
Definition Definitions.hpp:62
std::optional< CRefValWrapper > OptCRefValWrapper
Definition Definitions.hpp:51
std::variant< int, JsonObjKey > Key
Definition Definitions.hpp:71
std::monostate Null
Definition Definitions.hpp:24
std::span< const Key > Keys
Definition Definitions.hpp:72
std::optional< RefValWrapper > OptRefValWrapper
Definition Definitions.hpp:50
std::optional< ValWrapper > OptValWrapper
Definition Definitions.hpp:59
std::expected< RefValWrapper, Http::RequestError > ExpRefValWrapper
Definition Definitions.hpp:53
Definition Json.hpp:16
bool operator==(const Json &other) const
Json(Json &&other) noexcept
std::optional< const T * > EnsureRef() const
ExpValWrapper FindCopyOrError(Keys keys) const
Same as successive calls to GetCopy. Return RequestError at the first fail.
ValWrapper FindCopyOrNull(Keys keys) const
Same as successive calls to GetCopy. Return null at the first fail.
Json & operator=(Json &&other) noexcept
ExpRefValWrapper SearchOrError(Pred &&pred)
Will search the childs for the first element that matches the predicate and return it,...
Json & operator=(Array &&child)
bool IsOf() const
Json(const Json &other)
Json(Array &&child)
Json & operator=(Value &&newValue) noexcept
OptValWrapper GetAndMove(Key key) &&
Return (with move) the element with this index/key if this is an Object or Array, std::nullopt otherw...
OptRefValWrapper Find(Keys keys)
Same as successive calls to Get, std::nullopt at the first fail.
OptRefValWrapper Get(Key key)
Return a ref of the element with this index/key if this is an Object or Array, std::nullopt otherwise...
OptValWrapper SearchAndMove(Pred &&pred) &&
Will search the childs for the first element that matches the predicate and move it,...
Json(const Value &newValue)
ExpCRefValWrapper SearchOrError(Pred &&pred) const
Will search the childs for the first element that matches the predicate and return it,...
OptValWrapper SearchCopy(Pred &&pred) const
Will search the childs for the first element that matches the predicate and clone it,...
Json(bool other)
ExpValWrapper SearchAndMoveOrError(Pred &&pred) &&
Will search the childs for the first element that matches the predicate and move it,...
const T & AsRef() const
OptRefValWrapper Search(Pred &&pred)
Will search the childs for the first element that matches the predicate and return it,...
static T & AsType(Json &val)
ValWrapper FindAndMoveOrNull(Keys keys) &&
Same as successive calls to GetAndMove. Return null at the first fail.
const T & As() const
std::variant< Null, String, Number, Bool, Object, Array > Value
Definition Json.hpp:17
Json(const Array &child)
ExpCRefValWrapper FindOrError(Keys keys) const
Same as successive calls to Get. Return RequestError at the first fail.
Json & operator=(T &&other)
Json & operator=(bool other)
static bool IsOfType(const Json &val)
constexpr decltype(auto) Visit(Callable &&callable)
convenient call to std::visit() on _value.
Json(JsonObject &&child)
std::expected< const T *, Http::RequestError > EnsureRefOrError() const
ExpValWrapper FindAndMoveOrError(Keys keys) &&
Same as successive calls to Find. Return RequestError at the first fail.
ExpValWrapper GetAndMoveOrError(Key key) &&
Move the element with this index/key if this is an Object or Array. Return RequestError otherwise.
std::expected< T, Http::RequestError > EnsureMovOrError() &&
std::optional< T * > Ensure()
Json(Value &&newValue) noexcept
OptValWrapper FindCopy(Keys keys) const
Same as successive calls to GetCopy, std::nullopt at the first fail.
std::expected< T *, Http::RequestError > EnsureOrError()
std::expected< T *, Http::RequestError > EnsureMutOrError()
Json & operator=(const Array &child)
OptCRefValWrapper Find(Keys keys) const
Same as successive calls to Get, std::nullopt at the first fail.
Json & operator=(JsonObject &&other)
static std::expected< Json, Http::RequestError > Parse(std::string_view input)
Tries to parse the Json from a string.
ExpCRefValWrapper GetOrError(Key key) const
Return a ref of the element with this index/key if this is an Object or Array, RequestError otherwise...
ExpRefValWrapper FindOrError(Keys keys)
Same as successive calls to Get. Return RequestError at the first fail.
ValWrapper SearchAndMoveOrNull(Pred &&pred)
Will search the childs for the first element that matches the predicate and move it,...
Json & operator=(const Value &newValue)
ValWrapper SearchCopyOrNull(Pred &&pred) const
Will search the childs for the first element that matches the predicate and clone it,...
ExpValWrapper GetCopyOrError(Key key) const
Return a copy of the element with this index/key if this is an Object or Array, RequestError otherwis...
ExpValWrapper SearchCopyOrError(Pred &&pred) const
Will search the childs for the first element that matches the predicate and clone it,...
std::optional< T * > Ensure() const
ValWrapper GetCopyOrNull(Key key) const
Return a copy of the element with this index/key if this is an Object or Array, null otherwise.
Json & operator=(const Json &other)
OptValWrapper GetCopy(Key key) const
Return a copy of the element with this index/key if this is an Object or Array, std::nullopt otherwis...
OptValWrapper FindAndMove(Keys keys) &&
Same as successive calls to GetAndMove, std::nullopt at the first fail.
constexpr decltype(auto) Visit(Callable &&callable) const
convenient call to std::visit() on _value.
std::optional< T * > EnsureMut()
Json(const JsonObject &child)
ValWrapper GetAndMoveOrNull(Key key) &&
Move the element with this index/key if this is an Object or Array, null otherwise.
OptCRefValWrapper Search(Pred &&pred) const
Will search the childs for the first element that matches the predicate and return it,...
ExpRefValWrapper GetOrError(Key key)
Return a ref of the element with this index/key if this is an Object or Array, RequestError otherwise...
Json & operator=(const JsonObject &other)
std::optional< T > EnsureMov() &&
bool(*)(const Json &) PredicatePointer
Definition Json.hpp:18
std::expected< T *, Http::RequestError > EnsureOrError() const
OptCRefValWrapper Get(Key key) const
Return a ref of the element with this index/key if this is an Object or Array, std::nullopt otherwise...
static std::expected< Json, Http::RequestError > ParseText(std::string_view input, bool copyData=true, bool checkFinal=true)
Tries to parse the Json from a string.
Definition JsonObject.hpp:14