Thoth
A functional, expressive, asynchronous C++26 webdev library
Loading...
Searching...
No Matches
Thoth::NJson::Json Struct Reference

#include <Json.hpp>

Public Types

using Value = std::variant< Null, String, Number, Bool, Object, Array >
 
using PredicatePointer = bool(*)(const Json &)
 

Public Member Functions

 Json ()
 
 Json (const JsonObject &child)
 
 Json (JsonObject &&child)
 
 Json (const Array &child)
 
 Json (Array &&child)
 
 Json (Value &&newValue) noexcept
 
 Json (const Value &newValue)
 
 Json (Json &&other) noexcept
 
 Json (const Json &other)
 
 Json (bool other)
 
template<class T >
requires std::floating_point<T> || std::integral<T> && (!std::same_as<T, bool>)
 Json (T other)
 
template<class T >
requires std::constructible_from<std::string, T>
 Json (T &&other)
 
Jsonoperator= (const JsonObject &other)
 
Jsonoperator= (JsonObject &&other)
 
Jsonoperator= (const Array &child)
 
Jsonoperator= (Array &&child)
 
Jsonoperator= (Value &&newValue) noexcept
 
Jsonoperator= (const Value &newValue)
 
Jsonoperator= (Json &&other) noexcept
 
Jsonoperator= (const Json &other)
 
Jsonoperator= (bool other)
 
template<class T >
requires std::floating_point<T> || std::integral<T> && (!std::same_as<T, bool>)
Jsonoperator= (T other)
 
template<class T >
requires std::constructible_from<std::string, T>
Jsonoperator= (T &&other)
 
 operator Value & ()
 
 operator const Value & () const
 
template<class T >
bool IsOf () const
 
template<class T >
T & As ()
 
template<class T >
const T & As () const
 
template<class T >
T & AsMut ()
 
template<class T >
AsMov () &&
 
template<class T >
const T & AsRef () const
 
template<class T >
std::optional< T * > Ensure ()
 
template<class T >
std::optional< T * > Ensure () const
 
template<class T >
std::optional< T * > EnsureMut ()
 
template<class T >
std::optional< T > EnsureMov () &&
 
template<class T >
std::optional< const T * > EnsureRef () const
 
template<class T >
std::expected< T *, Http::RequestErrorEnsureOrError ()
 
template<class T >
std::expected< T *, Http::RequestErrorEnsureOrError () const
 
template<class T >
std::expected< T *, Http::RequestErrorEnsureMutOrError ()
 
template<class T >
std::expected< T, Http::RequestErrorEnsureMovOrError () &&
 
template<class T >
std::expected< const T *, Http::RequestErrorEnsureRefOrError () const
 
bool operator== (const Json &other) const
 
template<class Callable >
constexpr decltype(auto) Visit (Callable &&callable)
 convenient call to std::visit() on _value.
 
template<class Callable >
constexpr decltype(auto) Visit (Callable &&callable) const
 convenient call to std::visit() on _value.
 
Get Functions

Will get the direct child of this element with a given key/index.

OptRefValWrapper Get (Key key)
 Return a ref of the element with this index/key if this is an Object or Array, std::nullopt otherwise.
 
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.
 
OptValWrapper GetCopy (Key key) const
 Return a copy of the element with this index/key if this is an Object or Array, std::nullopt otherwise.
 
OptValWrapper GetAndMove (Key key) &&
 Return (with move) the element with this index/key if this is an Object or Array, std::nullopt otherwise.
 
ValWrapper GetCopyOrNull (Key key) const
 Return a copy of the element with this index/key if this is an Object or Array, null otherwise.
 
ValWrapper GetAndMoveOrNull (Key key) &&
 Move the element with this index/key if this is an Object or Array, null otherwise.
 
ExpRefValWrapper GetOrError (Key key)
 Return a ref of the element with this index/key if this is an Object or Array, RequestError otherwise.
 
ExpCRefValWrapper GetOrError (Key key) const
 Return a ref of the element with this index/key if this is an Object or Array, RequestError otherwise.
 
ExpValWrapper GetCopyOrError (Key key) const
 Return a copy of the element with this index/key if this is an Object or Array, RequestError otherwise.
 
ExpValWrapper GetAndMoveOrError (Key key) &&
 Move the element with this index/key if this is an Object or Array. Return RequestError otherwise.
 
Find Functions

Will find a nested child in the tree. Something like ["data"][-1]["contents"][0]["name"].

OptRefValWrapper Find (Keys keys)
 Same as successive calls to Get, std::nullopt at the first fail.
 
OptCRefValWrapper Find (Keys keys) const
 Same as successive calls to Get, std::nullopt at the first fail.
 
OptValWrapper FindCopy (Keys keys) const
 Same as successive calls to GetCopy, std::nullopt at the first fail.
 
OptValWrapper FindAndMove (Keys keys) &&
 Same as successive calls to GetAndMove, std::nullopt at the first fail.
 
ValWrapper FindCopyOrNull (Keys keys) const
 Same as successive calls to GetCopy. Return null at the first fail.
 
ValWrapper FindAndMoveOrNull (Keys keys) &&
 Same as successive calls to GetAndMove. Return null at the first fail.
 
ExpRefValWrapper FindOrError (Keys keys)
 Same as successive calls to Get. Return RequestError at the first fail.
 
ExpCRefValWrapper FindOrError (Keys keys) const
 Same as successive calls to Get. Return RequestError at the first fail.
 
ExpValWrapper FindCopyOrError (Keys keys) const
 Same as successive calls to GetCopy. Return RequestError at the first fail.
 
ExpValWrapper FindAndMoveOrError (Keys keys) &&
 Same as successive calls to Find. Return RequestError at the first fail.
 
Search Functions

Will pick the fist element that satisfies the given predicate.

template<class Pred = PredicatePointer>
requires std::predicate<Pred, Json>
OptRefValWrapper Search (Pred &&pred)
 Will search the childs for the first element that matches the predicate and return it, or std::nullopt if no matches.
 
template<class Pred = PredicatePointer>
requires std::predicate<Pred, Json>
OptCRefValWrapper Search (Pred &&pred) const
 Will search the childs for the first element that matches the predicate and return it, or std::nullopt if no matches.
 
template<class Pred = PredicatePointer>
requires std::predicate<Pred, Json>
OptValWrapper SearchCopy (Pred &&pred) const
 Will search the childs for the first element that matches the predicate and clone it, or std::nullopt if no matches.
 
template<class Pred = PredicatePointer>
requires std::predicate<Pred, Json>
OptValWrapper SearchAndMove (Pred &&pred) &&
 Will search the childs for the first element that matches the predicate and move it, or std::nullopt if no matches.
 
template<class Pred = PredicatePointer>
requires std::predicate<Pred, Json>
ValWrapper SearchCopyOrNull (Pred &&pred) const
 Will search the childs for the first element that matches the predicate and clone it, or RequestError if no matches.
 
template<class Pred = PredicatePointer>
requires std::predicate<Pred, Json>
ValWrapper SearchAndMoveOrNull (Pred &&pred)
 Will search the childs for the first element that matches the predicate and move it, or RequestError if no matches.
 
template<class Pred = PredicatePointer>
requires std::predicate<Pred, Json>
ExpRefValWrapper SearchOrError (Pred &&pred)
 Will search the childs for the first element that matches the predicate and return it, or std::nullopt if no matches.
 
template<class Pred = PredicatePointer>
requires std::predicate<Pred, Json>
ExpCRefValWrapper SearchOrError (Pred &&pred) const
 Will search the childs for the first element that matches the predicate and return it, or std::nullopt if no matches.
 
template<class Pred = PredicatePointer>
requires std::predicate<Pred, Json>
ExpValWrapper SearchCopyOrError (Pred &&pred) const
 Will search the childs for the first element that matches the predicate and clone it, or std::nullopt if no matches.
 
template<class Pred = PredicatePointer>
requires std::predicate<Pred, Json>
ExpValWrapper SearchAndMoveOrError (Pred &&pred) &&
 Will search the childs for the first element that matches the predicate and move it, or std::nullopt if no matches.
 

Static Public Member Functions

template<class T >
static bool IsOfType (const Json &val)
 
template<class T >
static T & AsType (Json &val)
 
static std::expected< Json, Http::RequestErrorParse (std::string_view input)
 Tries to parse the Json from a string.
 
static std::expected< Json, Http::RequestErrorParseText (std::string_view input, bool copyData=true, bool checkFinal=true)
 Tries to parse the Json from a string.
 

Member Typedef Documentation

◆ PredicatePointer

using Thoth::NJson::Json::PredicatePointer = bool(*)(const Json&)

◆ Value

Constructor & Destructor Documentation

◆ Json() [1/12]

Thoth::NJson::Json::Json ( )

◆ Json() [2/12]

Thoth::NJson::Json::Json ( const JsonObject child)

◆ Json() [3/12]

Thoth::NJson::Json::Json ( JsonObject &&  child)

◆ Json() [4/12]

Thoth::NJson::Json::Json ( const Array child)

◆ Json() [5/12]

Thoth::NJson::Json::Json ( Array &&  child)

◆ Json() [6/12]

Thoth::NJson::Json::Json ( Value &&  newValue)
noexcept

◆ Json() [7/12]

Thoth::NJson::Json::Json ( const Value newValue)

◆ Json() [8/12]

Thoth::NJson::Json::Json ( Json &&  other)
noexcept

◆ Json() [9/12]

Thoth::NJson::Json::Json ( const Json other)

◆ Json() [10/12]

Thoth::NJson::Json::Json ( bool  other)

◆ Json() [11/12]

template<class T >
requires std::floating_point<T> || std::integral<T> && (!std::same_as<T, bool>)
Thoth::NJson::Json::Json ( other)

◆ Json() [12/12]

template<class T >
requires std::constructible_from<std::string, T>
Thoth::NJson::Json::Json ( T &&  other)

Member Function Documentation

◆ As() [1/2]

template<class T >
T & Thoth::NJson::Json::As ( )

◆ As() [2/2]

template<class T >
const T & Thoth::NJson::Json::As ( ) const

◆ AsMov()

template<class T >
T Thoth::NJson::Json::AsMov ( ) &&

◆ AsMut()

template<class T >
T & Thoth::NJson::Json::AsMut ( )

◆ AsRef()

template<class T >
const T & Thoth::NJson::Json::AsRef ( ) const

◆ AsType()

template<class T >
static T & Thoth::NJson::Json::AsType ( Json val)
static

◆ Ensure() [1/2]

template<class T >
std::optional< T * > Thoth::NJson::Json::Ensure ( )

◆ Ensure() [2/2]

template<class T >
std::optional< T * > Thoth::NJson::Json::Ensure ( ) const

◆ EnsureMov()

template<class T >
std::optional< T > Thoth::NJson::Json::EnsureMov ( ) &&

◆ EnsureMovOrError()

template<class T >
std::expected< T, Http::RequestError > Thoth::NJson::Json::EnsureMovOrError ( ) &&

◆ EnsureMut()

template<class T >
std::optional< T * > Thoth::NJson::Json::EnsureMut ( )

◆ EnsureMutOrError()

template<class T >
std::expected< T *, Http::RequestError > Thoth::NJson::Json::EnsureMutOrError ( )

◆ EnsureOrError() [1/2]

template<class T >
std::expected< T *, Http::RequestError > Thoth::NJson::Json::EnsureOrError ( )

◆ EnsureOrError() [2/2]

template<class T >
std::expected< T *, Http::RequestError > Thoth::NJson::Json::EnsureOrError ( ) const

◆ EnsureRef()

template<class T >
std::optional< const T * > Thoth::NJson::Json::EnsureRef ( ) const

◆ EnsureRefOrError()

template<class T >
std::expected< const T *, Http::RequestError > Thoth::NJson::Json::EnsureRefOrError ( ) const

◆ Find() [1/2]

OptRefValWrapper Thoth::NJson::Json::Find ( Keys  keys)

Same as successive calls to Get, std::nullopt at the first fail.

◆ Find() [2/2]

OptCRefValWrapper Thoth::NJson::Json::Find ( Keys  keys) const

Same as successive calls to Get, std::nullopt at the first fail.

◆ FindAndMove()

OptValWrapper Thoth::NJson::Json::FindAndMove ( Keys  keys) &&

Same as successive calls to GetAndMove, std::nullopt at the first fail.

◆ FindAndMoveOrError()

ExpValWrapper Thoth::NJson::Json::FindAndMoveOrError ( Keys  keys) &&

Same as successive calls to Find. Return RequestError at the first fail.

◆ FindAndMoveOrNull()

ValWrapper Thoth::NJson::Json::FindAndMoveOrNull ( Keys  keys) &&

Same as successive calls to GetAndMove. Return null at the first fail.

◆ FindCopy()

OptValWrapper Thoth::NJson::Json::FindCopy ( Keys  keys) const

Same as successive calls to GetCopy, std::nullopt at the first fail.

◆ FindCopyOrError()

ExpValWrapper Thoth::NJson::Json::FindCopyOrError ( Keys  keys) const

Same as successive calls to GetCopy. Return RequestError at the first fail.

◆ FindCopyOrNull()

ValWrapper Thoth::NJson::Json::FindCopyOrNull ( Keys  keys) const

Same as successive calls to GetCopy. Return null at the first fail.

◆ FindOrError() [1/2]

ExpRefValWrapper Thoth::NJson::Json::FindOrError ( Keys  keys)

Same as successive calls to Get. Return RequestError at the first fail.

◆ FindOrError() [2/2]

ExpCRefValWrapper Thoth::NJson::Json::FindOrError ( Keys  keys) const

Same as successive calls to Get. Return RequestError at the first fail.

◆ Get() [1/2]

OptRefValWrapper Thoth::NJson::Json::Get ( Key  key)

Return a ref of the element with this index/key if this is an Object or Array, std::nullopt otherwise.

◆ Get() [2/2]

OptCRefValWrapper Thoth::NJson::Json::Get ( Key  key) const

Return a ref of the element with this index/key if this is an Object or Array, std::nullopt otherwise.

◆ GetAndMove()

OptValWrapper Thoth::NJson::Json::GetAndMove ( Key  key) &&

Return (with move) the element with this index/key if this is an Object or Array, std::nullopt otherwise.

◆ GetAndMoveOrError()

ExpValWrapper Thoth::NJson::Json::GetAndMoveOrError ( Key  key) &&

Move the element with this index/key if this is an Object or Array. Return RequestError otherwise.

◆ GetAndMoveOrNull()

ValWrapper Thoth::NJson::Json::GetAndMoveOrNull ( Key  key) &&

Move the element with this index/key if this is an Object or Array, null otherwise.

◆ GetCopy()

OptValWrapper Thoth::NJson::Json::GetCopy ( Key  key) const

Return a copy of the element with this index/key if this is an Object or Array, std::nullopt otherwise.

◆ GetCopyOrError()

ExpValWrapper Thoth::NJson::Json::GetCopyOrError ( Key  key) const

Return a copy of the element with this index/key if this is an Object or Array, RequestError otherwise.

◆ GetCopyOrNull()

ValWrapper Thoth::NJson::Json::GetCopyOrNull ( Key  key) const

Return a copy of the element with this index/key if this is an Object or Array, null otherwise.

◆ GetOrError() [1/2]

ExpRefValWrapper Thoth::NJson::Json::GetOrError ( Key  key)

Return a ref of the element with this index/key if this is an Object or Array, RequestError otherwise.

◆ GetOrError() [2/2]

ExpCRefValWrapper Thoth::NJson::Json::GetOrError ( Key  key) const

Return a ref of the element with this index/key if this is an Object or Array, RequestError otherwise.

◆ IsOf()

template<class T >
bool Thoth::NJson::Json::IsOf ( ) const

◆ IsOfType()

template<class T >
static bool Thoth::NJson::Json::IsOfType ( const Json val)
static

◆ operator const Value &()

Thoth::NJson::Json::operator const Value & ( ) const

◆ operator Value &()

Thoth::NJson::Json::operator Value & ( )

◆ operator=() [1/11]

Json & Thoth::NJson::Json::operator= ( Array &&  child)

◆ operator=() [2/11]

Json & Thoth::NJson::Json::operator= ( bool  other)

◆ operator=() [3/11]

Json & Thoth::NJson::Json::operator= ( const Array child)

◆ operator=() [4/11]

Json & Thoth::NJson::Json::operator= ( const Json other)

◆ operator=() [5/11]

Json & Thoth::NJson::Json::operator= ( const JsonObject other)

◆ operator=() [6/11]

Json & Thoth::NJson::Json::operator= ( const Value newValue)

◆ operator=() [7/11]

Json & Thoth::NJson::Json::operator= ( Json &&  other)
noexcept

◆ operator=() [8/11]

Json & Thoth::NJson::Json::operator= ( JsonObject &&  other)

◆ operator=() [9/11]

template<class T >
requires std::constructible_from<std::string, T>
Json & Thoth::NJson::Json::operator= ( T &&  other)

◆ operator=() [10/11]

template<class T >
requires std::floating_point<T> || std::integral<T> && (!std::same_as<T, bool>)
Json & Thoth::NJson::Json::operator= ( other)

◆ operator=() [11/11]

Json & Thoth::NJson::Json::operator= ( Value &&  newValue)
noexcept

◆ operator==()

bool Thoth::NJson::Json::operator== ( const Json other) const

◆ Parse()

static std::expected< Json, Http::RequestError > Thoth::NJson::Json::Parse ( std::string_view  input)
static

Tries to parse the Json from a string.

Requires only one parameter so it's convenient to monads. Calls ParseText with default parameters.

Parameters
inputthe text to parse.
Returns
A Json if the parse success, std::nullopt otherwise.

◆ ParseText()

static std::expected< Json, Http::RequestError > Thoth::NJson::Json::ParseText ( std::string_view  input,
bool  copyData = true,
bool  checkFinal = true 
)
static

Tries to parse the Json from a string.

Parameters
inputthe text to parse.
copyDatacopy the input to an internal buffer if true, keeps a reference otherwise.
checkFinalensure that there is only space chars after the end of the json.
Returns
A Json if the parse success, std::nullopt otherwise.

◆ Search() [1/2]

template<class Pred = PredicatePointer>
requires std::predicate<Pred, Json>
OptRefValWrapper Thoth::NJson::Json::Search ( Pred &&  pred)

Will search the childs for the first element that matches the predicate and return it, or std::nullopt if no matches.

◆ Search() [2/2]

template<class Pred = PredicatePointer>
requires std::predicate<Pred, Json>
OptCRefValWrapper Thoth::NJson::Json::Search ( Pred &&  pred) const

Will search the childs for the first element that matches the predicate and return it, or std::nullopt if no matches.

◆ SearchAndMove()

template<class Pred = PredicatePointer>
requires std::predicate<Pred, Json>
OptValWrapper Thoth::NJson::Json::SearchAndMove ( Pred &&  pred) &&

Will search the childs for the first element that matches the predicate and move it, or std::nullopt if no matches.

◆ SearchAndMoveOrError()

template<class Pred = PredicatePointer>
requires std::predicate<Pred, Json>
ExpValWrapper Thoth::NJson::Json::SearchAndMoveOrError ( Pred &&  pred) &&

Will search the childs for the first element that matches the predicate and move it, or std::nullopt if no matches.

◆ SearchAndMoveOrNull()

template<class Pred = PredicatePointer>
requires std::predicate<Pred, Json>
ValWrapper Thoth::NJson::Json::SearchAndMoveOrNull ( Pred &&  pred)

Will search the childs for the first element that matches the predicate and move it, or RequestError if no matches.

◆ SearchCopy()

template<class Pred = PredicatePointer>
requires std::predicate<Pred, Json>
OptValWrapper Thoth::NJson::Json::SearchCopy ( Pred &&  pred) const

Will search the childs for the first element that matches the predicate and clone it, or std::nullopt if no matches.

◆ SearchCopyOrError()

template<class Pred = PredicatePointer>
requires std::predicate<Pred, Json>
ExpValWrapper Thoth::NJson::Json::SearchCopyOrError ( Pred &&  pred) const

Will search the childs for the first element that matches the predicate and clone it, or std::nullopt if no matches.

◆ SearchCopyOrNull()

template<class Pred = PredicatePointer>
requires std::predicate<Pred, Json>
ValWrapper Thoth::NJson::Json::SearchCopyOrNull ( Pred &&  pred) const

Will search the childs for the first element that matches the predicate and clone it, or RequestError if no matches.

◆ SearchOrError() [1/2]

template<class Pred = PredicatePointer>
requires std::predicate<Pred, Json>
ExpRefValWrapper Thoth::NJson::Json::SearchOrError ( Pred &&  pred)

Will search the childs for the first element that matches the predicate and return it, or std::nullopt if no matches.

◆ SearchOrError() [2/2]

template<class Pred = PredicatePointer>
requires std::predicate<Pred, Json>
ExpCRefValWrapper Thoth::NJson::Json::SearchOrError ( Pred &&  pred) const

Will search the childs for the first element that matches the predicate and return it, or std::nullopt if no matches.

◆ Visit() [1/2]

template<class Callable >
constexpr decltype(auto) Thoth::NJson::Json::Visit ( Callable &&  callable)
constexpr

convenient call to std::visit() on _value.

◆ Visit() [2/2]

template<class Callable >
constexpr decltype(auto) Thoth::NJson::Json::Visit ( Callable &&  callable) const
constexpr

convenient call to std::visit() on _value.


The documentation for this struct was generated from the following file: