Thoth
A functional, expressive, asynchronous C++26 webdev library
Loading...
Searching...
No Matches
JsonObject.hpp
Go to the documentation of this file.
1#pragma once
2#include <optional>
3#include <format>
4#include <string>
5#include <vector>
6
8
9
10namespace Thoth::NJson {
11 using JsonObjKey = std::string;
12 using JsonObjKeyRef = std::string_view;
13
14 struct JsonObject {
15 using JsonValRef = Json&;
16
17 using JsonPair = std::pair<JsonObjKey, Json>;
18 using JsonPairRef = std::pair<JsonObjKeyRef, JsonValRef>;
20
21 using IterType = decltype(MapType{}.begin());
22 using CIterType = decltype(MapType{}.cbegin());
23
25
26 JsonObject() = default;
27 JsonObject(JsonObject&& other) noexcept;
28 JsonObject(const JsonObject& other);
29
31 explicit JsonObject(MapType&& initAs);
32
33 // NOLINTNEXTLINE(*)
34 JsonObject(std::initializer_list<JsonPair> init);
35
36
37 JsonObject& operator=(JsonObject&& other) noexcept;
39
40 JsonObject& operator=(std::initializer_list<JsonPair> list);
41
45 [[nodiscard]] bool Exists(JsonObjKeyRef key) const;
46
50 [[nodiscard]] bool Exists(JsonPairRef p) const;
51
56 [[nodiscard]] bool Exists(JsonObjKeyRef key, JsonValRef val) const;
60
63
66
71
72
75
83 [[nodiscard]] OptCRefValWrapper Get(JsonObjKeyRef key) const;
87 [[nodiscard]] OptValWrapper GetCopy(JsonObjKeyRef key) const;
92
96 [[nodiscard]] ValWrapper GetCopyOrNull(JsonObjKeyRef key) const;
101
102
103
104 IterType begin() { return _pairs.begin(); }
105 IterType end() { return _pairs.end(); }
106 [[nodiscard]] CIterType begin() const { return _pairs.cbegin(); }
107 [[nodiscard]] CIterType end() const { return _pairs.cend(); }
108
109
110
112 void Clear();
113
115 [[nodiscard]] size_t Size() const;
116
118 [[nodiscard]] bool Empty() const;
119
120
121
127 const Json& operator[](JsonObjKeyRef key) const;
128
130 bool operator==(const JsonObject& other) const;
131
132 friend Json;
133 friend bool Details::ReadObject(std::string_view& input, auto& val, const Details::BufferInfo& info);
134 private:
135 MapType _pairs{};
136
137 friend struct std::formatter<JsonObject>;
138 };
139}
140
141#include <Thoth/NJson/JsonObject.tpp>
Definition Definitions.hpp:19
std::string_view JsonObjKeyRef
Definition Definitions.hpp:69
std::optional< CRefValWrapper > OptCRefValWrapper
Definition Definitions.hpp:51
std::optional< RefValWrapper > OptRefValWrapper
Definition Definitions.hpp:50
std::string JsonObjKey
Definition Definitions.hpp:68
std::optional< ValWrapper > OptValWrapper
Definition Definitions.hpp:59
constexpr const_iterator cend() const
constexpr const_iterator cbegin() const
constexpr iterator begin()
constexpr iterator end()
Definition Definitions.hpp:32
Definition Json.hpp:16
Definition JsonObject.hpp:14
friend Json
Definition JsonObject.hpp:132
void Set(JsonPairRef p)
Add a value with the specified key. Replace if already exists.
JsonObject & operator=(JsonObject &&other) noexcept
bool Exists(JsonObjKeyRef key, JsonValRef val) const
check if a key=val exists.
bool SetIfNull(JsonPairRef p)
If key not exists, set it to value.
JsonObject(MapType &&initAs)
Create with an existing map.
bool SetIfNull(JsonObjKeyRef key, JsonValRef val)
same as SetIfNull(JsonPairRef p).
ValWrapper GetCopyOrNull(JsonObjKeyRef key) const
Copy of a value or return null if it not exists.
friend bool Details::ReadObject(std::string_view &input, auto &val, const Details::BufferInfo &info)
bool operator==(const JsonObject &other) const
void Clear()
Clear all keys.
const Json & operator[](JsonObjKeyRef key) const
OptValWrapper GetCopy(JsonObjKeyRef key) const
Copy of a value if it exists.
void Set(JsonObjKeyRef key, JsonValRef val)
same as Add(JsonPairRef p).
JsonObject(JsonObject &&other) noexcept
CIterType begin() const
Definition JsonObject.hpp:106
ValWrapper GetOrNullAndMove(JsonObjKeyRef key) &&
Copy of a value or return null if it not exists.
OptRefValWrapper Get(JsonObjKeyRef key)
Get the reference of a key but don't create if it not exists.
OptCRefValWrapper Get(JsonObjKeyRef key) const
Get the reference of a key but don't create if it not exists.
Json & operator[](JsonObjKeyRef key)
CIterType end() const
Definition JsonObject.hpp:107
std::pair< JsonObjKey, Json > JsonPair
Definition JsonObject.hpp:17
std::pair< JsonObjKeyRef, JsonValRef > JsonPairRef
Definition JsonObject.hpp:18
decltype(MapType{}.cbegin()) CIterType
Definition JsonObject.hpp:22
bool Exists(JsonObjKeyRef key) const
check if a key exists.
JsonObject & operator=(const JsonObject &other)
bool Exists(JsonPairRef p) const
check if a key exists.
IterType end()
Definition JsonObject.hpp:105
OptValWrapper GetAndMove(JsonObjKeyRef key) &&
Copy of a value if it exists.
bool Remove(JsonObjKeyRef key)
Removes a key.
JsonObject(const JsonObject &other)
IterType begin()
Definition JsonObject.hpp:104
JsonObject & operator=(std::initializer_list< JsonPair > list)
decltype(MapType{}.begin()) IterType
Definition JsonObject.hpp:21
JsonObject(std::initializer_list< JsonPair > init)