14#include <AUI/IO/IOutputStream.h>
15#include "AUI/Common/AException.h"
16#include "AUI/Common/AOptional.h"
17#include "AUI/Common/SharedPtr.h"
18#include "AUI/IO/IInputStream.h"
20#include "AUI/Common/AByteBufferView.h"
22#include <AUI/Common/AUuid.h>
23#include <AUI/Common/AMap.h>
24#include <AUI/Json/AJson.h>
25#include <AUI/Json/Exception.h>
26#include <AUI/Traits/callables.h>
39 API_AUI_JSON std::pair<AString, AJson>*
contains(
const AString& key)
noexcept;
50 [[nodiscard]] API_AUI_JSON
AJson& operator[](
const AString& key);
52 [[nodiscard]]
const AJson& operator[](
const AString& key)
const {
72 using JsonVariant = std::variant<std::nullopt_t, std::nullptr_t, int, int64_t, double, bool, AString, aui::impl::JsonArray, aui::impl::JsonObject>;
79class AJson:
public aui::impl::JsonVariant {
81 using super = aui::impl::JsonVariant;
85 bool is()
const noexcept {
86 return std::holds_alternative<T>(*
this);
95 if (
auto p = std::get_if<T>(
this)) {
103 const T& as()
const {
104 if (
auto p = std::get_if<T>(
this)) {
107 if constexpr(std::is_same_v<T, aui::impl::JsonObject>) {
109 }
else if constexpr(std::is_same_v<T, aui::impl::JsonArray>) {
116 using aui::impl::JsonVariant::variant;
121 AJson(std::initializer_list<std::pair<AString, AJson>> elems): aui::impl::JsonVariant(
aui::impl::JsonObject(std::move(elems))) {
125 AJson(
const char* name): aui::impl::JsonVariant(
AString(name)) {}
131 AJson()
noexcept: aui::impl::JsonVariant(std::nullopt) {}
134 bool isInt()
const noexcept {
139 bool isLongInt()
const noexcept {
140 return isInt() || is<int64_t>();
144 bool isEmpty()
const noexcept {
145 return is<std::nullopt_t>();
149 bool isNumber()
const noexcept {
150 return isInt() || is<double>();
154 bool isBool()
const noexcept {
159 bool isNull()
const noexcept {
160 return is<std::nullptr_t>();
164 bool isString()
const noexcept {
165 return is<AString>();
169 bool isArray()
const noexcept {
170 return is<aui::impl::JsonArray>();
174 bool isObject()
const noexcept {
175 return is<aui::impl::JsonObject>();
184 int64_t asLongInt()
const {
186 [](
auto&& e) -> std::int64_t {
189 [](std::int64_t v) -> std::int64_t {
192 [](
int v) -> std::int64_t {
195 }, (super)
const_cast<AJson&
>(*
this));
199 double asNumber()
const {
201 [](
auto&& e) ->
double {
204 [](
double v) ->
double {
207 [](
int v) ->
double {
210 [](int64_t v) ->
double {
213 }, (super)
const_cast<AJson&
>(*
this));
217 bool asBool()
const {
222 const AString& asString()
const {
223 return as<AString>();
228 return as<aui::impl::JsonArray>();
233 return as<aui::impl::JsonObject>();
239 return as<aui::impl::JsonArray>();
248 bool contains(
const AString& mapKey)
const {
249 return as<Object>().contains(mapKey);
254 if (
auto c = as<Object>().contains(mapKey)) {
261 return asObject()[mapKey];
266 return const_cast<AJson&
>(*this)[mapKey];
270 AJson& operator[](
int arrayIndex) {
271 return as<Array>().at(arrayIndex);
274 const AJson& operator[](
int arrayIndex)
const {
275 return const_cast<AJson&
>(*this)[arrayIndex];
278 void push_back(
AJson elem) {
279 asArray().push_back(std::move(elem));
311 [[nodiscard]]
static API_AUI_JSON
AString toString(
const AJson& json);
312 [[nodiscard]]
static API_AUI_JSON
AJson fromString(
const AString& json);
314 return aui::deserialize<AJson>(stream);
320#include <AUI/Json/Conversion.h>
321#include <AUI/Json/Serialization.h>
Acts like std::string_view but for AByteBuffer.
Definition: AByteBufferView.h:24
Definition: Exception.h:33
Json atom.
Definition: AJson.h:79
API_AUI_JSON AJson mergedWith(const AJson &other)
Merges other json object into this object.
Definition: AJson.cpp:33
Utility wrapper implementing the stack-allocated (fast) optional idiom.
Definition: AOptional.h:32
Represents a Unicode character string.
Definition: AString.h:37
A std::vector with AUI extensions.
Definition: AVector.h:38
const AJson & at(const AString &key) const
If container contains key, returns reference to the element.
Definition: AJson.h:66
API_AUI_JSON std::pair< AString, AJson > * contains(const AString &key) noexcept
If container contains key, returns pointer to the element. nullptr otherwise.
Definition: AJson.cpp:44
API_AUI_JSON AJson & at(const AString &key)
If container contains key, returns reference to the element.
Definition: AJson.cpp:59
const std::pair< AString, AJson > * contains(const AString &key) const noexcept
If container contains key, returns pointer to the element. nullptr otherwise.
Definition: AJson.h:45
Definition: callables.h:34
Does not allow escaping, allowing to accept lvalue ref, rvalue ref, shared_ptr and etc without overhe...
Definition: values.h:127