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>
38 using AVector<std::pair<AString, AJson>>::AVector;
55 [[nodiscard]] API_AUI_JSON
AJson& operator[](
const AString& key);
57 [[nodiscard]]
const AJson& operator[](
const AString& key)
const {
77 using JsonVariant = std::variant<std::nullopt_t, std::nullptr_t, int, int64_t, double, bool, AString, aui::impl::JsonArray, aui::impl::JsonObject>;
84class AJson:
public aui::impl::JsonVariant {
86 using super = aui::impl::JsonVariant;
90 bool is()
const noexcept {
91 return std::holds_alternative<T>(*
this);
100 if (
auto p = std::get_if<T>(
this)) {
108 const T& as()
const {
109 if (
auto p = std::get_if<T>(
this)) {
112 if constexpr(std::is_same_v<T, aui::impl::JsonObject>) {
114 }
else if constexpr(std::is_same_v<T, aui::impl::JsonArray>) {
121 using aui::impl::JsonVariant::variant;
123 using Array = aui::impl::JsonArray;
126 AJson(std::initializer_list<std::pair<AString, AJson>> elems): aui::impl::JsonVariant(
aui::impl::JsonObject(std::move(elems))) {
130 AJson(
const char* name): aui::impl::JsonVariant(
AString(name)) {}
131 AJson(
const AJson& json) =
default;
132 AJson(AJson&&)
noexcept =
default;
133 AJson& operator=(
const AJson&) =
default;
134 AJson& operator=(AJson&&)
noexcept =
default;
136 AJson()
noexcept: aui::impl::JsonVariant(std::nullopt) {}
139 bool isInt()
const noexcept {
144 bool isLongInt()
const noexcept {
145 return isInt() || is<int64_t>();
149 bool isEmpty()
const noexcept {
150 return is<std::nullopt_t>();
154 bool isNumber()
const noexcept {
155 return isInt() || is<double>();
159 bool isBool()
const noexcept {
164 bool isNull()
const noexcept {
165 return is<std::nullptr_t>();
169 bool isString()
const noexcept {
170 return is<AString>();
174 bool isArray()
const noexcept {
175 return is<aui::impl::JsonArray>();
179 bool isObject()
const noexcept {
180 return is<aui::impl::JsonObject>();
189 int64_t asLongInt()
const {
191 [](
auto&& e) -> std::int64_t {
194 [](std::int64_t v) -> std::int64_t {
197 [](
int v) -> std::int64_t {
200 }, (super)
const_cast<AJson&
>(*this));
204 double asNumber()
const {
206 [](
auto&& e) ->
double {
209 [](
double v) ->
double {
212 [](
int v) ->
double {
215 [](int64_t v) ->
double {
218 }, (super)
const_cast<AJson&
>(*this));
222 bool asBool()
const {
227 const AString& asString()
const {
228 return as<AString>();
232 const aui::impl::JsonArray& asArray()
const {
233 return as<aui::impl::JsonArray>();
238 return as<aui::impl::JsonObject>();
243 aui::impl::JsonArray& asArray() {
244 return as<aui::impl::JsonArray>();
253 bool contains(
const AString& mapKey)
const {
254 return as<Object>().contains(mapKey);
259 if (
auto c = as<Object>().contains(mapKey)) {
265 AJson& operator[](
const AString& mapKey) {
266 return asObject()[mapKey];
269 const AJson& operator[](
const AString& mapKey)
const {
271 return const_cast<AJson&
>(*this)[mapKey];
275 AJson& operator[](
int arrayIndex) {
276 return as<Array>().at(arrayIndex);
279 const AJson& operator[](
int arrayIndex)
const {
280 return const_cast<AJson&
>(*this)[arrayIndex];
283 void push_back(AJson elem) {
284 asArray().push_back(std::move(elem));
316 [[nodiscard]]
static API_AUI_JSON
AString toString(
const AJson& json);
317 [[nodiscard]]
static API_AUI_JSON AJson fromString(
const AString& json);
319 return aui::deserialize<AJson>(stream);
325#include <AUI/Json/Conversion.h>
326#include <AUI/Json/Serialization.h>
Acts like std::string_view but for AByteBuffer.
Definition AByteBufferView.h:24
static AString name()
[ARROW_ERROR_MESSAGE_EXAMPLE]
Definition AClass.h:28
Definition Exception.h:33
Json atom.
Definition AJson.h:84
API_AUI_JSON AJson mergedWith(const AJson &other)
Merges other json object into this object.
Utility wrapper implementing the stack-allocated (fast) optional idiom.
Definition AOptional.h:33
Represents a Unicode character string.
Definition AString.h:38
A std::vector with AUI extensions.
Definition AVector.h:39
const AJson & at(const AString &key) const
If container contains key, returns reference to the element.
Definition AJson.h:71
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:50
API_AUI_JSON AJson & at(const AString &key)
If container contains key, returns reference to the element.
API_AUI_JSON std::pair< AString, AJson > * contains(const AString &key) noexcept
If container contains key, returns pointer to the element. nullptr otherwise.
Definition callables.h:36
Does not allow escaping, allowing to accept lvalue ref, rvalue ref, shared_ptr and etc without overhe...
Definition values.h:128