simjson 1.2.1
Very simple JSON library for simstr
 
Loading...
Searching...
No Matches
simjson::JsonValueTempl< K > Class Template Reference

Класс для представления json значения. More...

#include <json.h>

Public Member Functions

 JsonValueTempl ()
 
SIMJSON_API JsonValueTempl (const JsonValueTempl &other)
 
 JsonValueTempl (JsonValueTempl &&other) noexcept
 
SIMJSON_API ~JsonValueTempl ()
 
 JsonValueTempl (int8_t v)
 
 JsonValueTempl (int16_t v)
 
 JsonValueTempl (int32_t v)
 
 JsonValueTempl (int64_t v)
 
 JsonValueTempl (bool v)
 
 JsonValueTempl (double v)
 
 JsonValueTempl (strType t)
 
 JsonValueTempl (ssType t)
 
template<StrExprForType< K > A>
 JsonValueTempl (const A &e)
 
template<typename T, size_t N = const_lit_for<K, T>::Count>
 JsonValueTempl (T &&str)
 
 JsonValueTempl (const null_t &)
 
 JsonValueTempl (const emptyString_t &)
 
 JsonValueTempl (const emptyObject_t &)
 
 JsonValueTempl (const emptyArray_t &)
 
SIMJSON_API JsonValueTempl (Type type)
 
 JsonValueTempl (ObjectInit &&init)
 Constructor from initializer to create json::object. JsonValue v = {{"key1"_h, 1}, {"key2", "text"}, {"key3"_h, false}}; -> {"key1": 10, "key2": "text", "key3": false}
 
 JsonValueTempl (ArrayInit &&init)
 Constructor from initializer to create json::array. JsonValue json = {"key1", 12, false, Json::null, 1.25, true}; -> ["key1", 12, false, null, 1.25, true]
 
 JsonValueTempl (jt::JsonArraySource< K > auto const &obj)
 Constructor from any standard array container containing JSON values.
 
 JsonValueTempl (jt::JsonObjectSource< K > auto const &obj)
 Constructor from any standard container with pairs containing a string key in first and a JSON value in second.
 
SIMJSON_API JsonValueTempl (const Clone &clone)
 Clone constructor. In this case, "deep" copies are created for objects and arrays.
 
Type type () const
 
json_value clone () const
 
bool is_undefined () const
 
bool is_null () const
 
bool is_boolean () const
 
bool is_integer () const
 
bool is_real () const
 
bool is_text () const
 
bool is_array () const
 
bool is_object () const
 
bool as_boolean () const
 Get the value as boolean. The debug version checks that the value is indeed boolean.
 
std::optional< bool > boolean () const
 Get a boolean if a boolean is stored, or nothing.
 
template<typename Exc, typename ... Args>
requires (std::is_constructible_v<Exc, Args...>)
bool boolean_or_throw (Args &&...args) const
 
SIMJSON_API bool to_boolean () const
 
int64_t as_integer () const
 
std::optional< int64_t > integer () const
 
template<typename Exc, typename ... Args>
requires (std::is_constructible_v<Exc, Args...>)
int64_t integer_or_throw (Args &&...args) const
 
SIMJSON_API std::optional< int64_t > to_integer () const
 Get the value converted to an integer or nothing. The logic is the same as in javascript 1 * val. If the result is NaN or Inf, then nothing. Non-integer numbers are converted to an integer. Example: int val = json.to_integer().value_or(10);
 
template<typename Exc, typename ... Args>
requires (std::is_constructible_v<Exc, Args...>)
int64_t to_integer_or_throw (Args &&...args) const
 
double as_real () const
 
std::optional< double > real () const
 
template<typename Exc, typename ... Args>
requires (std::is_constructible_v<Exc, Args...>)
double real_or_throw (Args &&...args)
 
SIMJSON_API double to_real () const
 
std::optional< int64_t > number_int () const
 
template<typename Exc, typename ... Args>
requires (std::is_constructible_v<Exc, Args...>)
int64_t number_int_or_throw (Args &&...args) const
 
SIMJSON_API std::optional< double > number_real () const
 
template<typename Exc, typename ... Args>
requires (std::is_constructible_v<Exc, Args...>)
double number_real_or_throw (Args &&...args) const
 
const strType & as_text () const
 
std::optional< strType > text () const
 
template<typename Exc, typename ... Args>
requires (std::is_constructible_v<Exc, Args...>)
const strType & text_or_throw (Args &&...args) const
 
template<typename Exc, typename ... Args>
requires (std::is_constructible_v<Exc, Args...>)
const strType & not_empty_text_or_throw (Args &&...args) const
 
SIMJSON_API strType to_text () const
 
json_object & as_object ()
 
const json_object & as_object () const
 
json_array & as_array ()
 
const json_array & as_array () const
 
void swap (json_value &other) noexcept
 
JsonValueTemploperator= (json_value t) noexcept
 
template<jt::JsonKeyType< K > T>
const json_valueat (T &&key) const
 Access to a property of a constant object by key.
 
template<jt::JsonKeyType< K > T>
const json_valueoperator[] (T &&key) const
 Access to a property of a constant object by key.
 
template<jt::JsonKeyType< K > T, typename... Args>
const json_valueoperator() (T &&key, Args &&...args) const
 Access to a property of a constant object by a set of keys.
 
template<jt::JsonKeyType< K > T>
json_valueoperator[] (T &&key)
 Access to an object property by key.
 
template<jt::JsonKeyType< K > Key, typename ... Args>
json_valueset (Key &&key, Args &&... args)
 Setting the value of a json object property by key.
 
const json_valueat (size_t idx) const
 
const json_valueoperator[] (size_t idx) const
 
json_valueoperator[] (size_t idx)
 Access an array element by index.
 
size_t size () const
 
SIMJSON_API void merge (const json_value &other, bool replace=true, bool append_arrays=false)
 Merge with other JSON.
 
SIMJSON_API void store (lstring< K, 0, true > &stream, bool prettify=false, bool order_keys=false, K indent_symbol=' ', unsigned indent_count=2) const
 Serialize json value to string.
 
lstring< K, 0, true > store (bool prettify=false, bool order_keys=false, K indent_symbol=' ', unsigned indent_count=2) const
 Serialize a json value to a string.
 

Static Public Member Functions

static std::tuple< json_value, JsonParseResult, unsigned, unsigned > parse (ssType jsonString)
 Parse text to json.
 

Detailed Description

template<typename K>
class simjson::JsonValueTempl< K >

Класс для представления json значения.

Template Parameters
K- тип символов.

Class to represent a json value.

Template Parameters
K- character type.

Constructor & Destructor Documentation

◆ JsonValueTempl() [1/21]

template<typename K>
simjson::JsonValueTempl< K >::JsonValueTempl ( )
inline

Creates an empty object of type Undefined.

◆ JsonValueTempl() [2/21]

template<typename K>
SIMJSON_API simjson::JsonValueTempl< K >::JsonValueTempl ( const JsonValueTempl< K > & other)

Copy constructor. Objects and arrays are copied by reference.

◆ JsonValueTempl() [3/21]

template<typename K>
simjson::JsonValueTempl< K >::JsonValueTempl ( JsonValueTempl< K > && other)
inlinenoexcept

Move constructor.

◆ ~JsonValueTempl()

template<typename K>
SIMJSON_API simjson::JsonValueTempl< K >::~JsonValueTempl ( )

Destructor.

◆ JsonValueTempl() [4/21]

template<typename K>
simjson::JsonValueTempl< K >::JsonValueTempl ( int8_t v)
inline

Constructor from int8_t.

◆ JsonValueTempl() [5/21]

template<typename K>
simjson::JsonValueTempl< K >::JsonValueTempl ( int16_t v)
inline

Constructor from int16_t.

◆ JsonValueTempl() [6/21]

template<typename K>
simjson::JsonValueTempl< K >::JsonValueTempl ( int32_t v)
inline

Constructor from int32_t.

◆ JsonValueTempl() [7/21]

template<typename K>
simjson::JsonValueTempl< K >::JsonValueTempl ( int64_t v)
inline

Constructor from int64_t.

◆ JsonValueTempl() [8/21]

template<typename K>
simjson::JsonValueTempl< K >::JsonValueTempl ( bool v)
inline

Constructor from bool.

◆ JsonValueTempl() [9/21]

template<typename K>
simjson::JsonValueTempl< K >::JsonValueTempl ( double v)
inline

Constructor from double.

◆ JsonValueTempl() [10/21]

template<typename K>
simjson::JsonValueTempl< K >::JsonValueTempl ( strType t)
inline

Constructor from sstring.

◆ JsonValueTempl() [11/21]

template<typename K>
simjson::JsonValueTempl< K >::JsonValueTempl ( ssType t)
inline

Constructor from the other strings.

◆ JsonValueTempl() [12/21]

template<typename K>
template<StrExprForType< K > A>
simjson::JsonValueTempl< K >::JsonValueTempl ( const A & e)
inline

Constructor from string expressions.

◆ JsonValueTempl() [13/21]

template<typename K>
template<typename T, size_t N = const_lit_for<K, T>::Count>
simjson::JsonValueTempl< K >::JsonValueTempl ( T && str)
inline

Constructor from string literals.

◆ JsonValueTempl() [14/21]

template<typename K>
simjson::JsonValueTempl< K >::JsonValueTempl ( const null_t & )
inline

Constructor for creating json::null.

◆ JsonValueTempl() [15/21]

template<typename K>
simjson::JsonValueTempl< K >::JsonValueTempl ( const emptyString_t & )
inline

Constructor for creating an empty string.

◆ JsonValueTempl() [16/21]

template<typename K>
simjson::JsonValueTempl< K >::JsonValueTempl ( const emptyObject_t & )
inline

Constructor for creating json::object.

◆ JsonValueTempl() [17/21]

template<typename K>
simjson::JsonValueTempl< K >::JsonValueTempl ( const emptyArray_t & )
inline

Constructor for creating json::array.

◆ JsonValueTempl() [18/21]

template<typename K>
SIMJSON_API simjson::JsonValueTempl< K >::JsonValueTempl ( Type type)

Constructor for creating a default value with type type.

◆ JsonValueTempl() [19/21]

template<typename K>
simjson::JsonValueTempl< K >::JsonValueTempl ( jt::JsonArraySource< K > auto const & obj)
inline

Constructor from any standard array container containing JSON values.

Parameters
objis the object.

◆ JsonValueTempl() [20/21]

template<typename K>
simjson::JsonValueTempl< K >::JsonValueTempl ( jt::JsonObjectSource< K > auto const & obj)
inline

Constructor from any standard container with pairs containing a string key in first and a JSON value in second.

Parameters
objis the object.

◆ JsonValueTempl() [21/21]

template<typename K>
SIMJSON_API simjson::JsonValueTempl< K >::JsonValueTempl ( const Clone & clone)

Clone constructor. In this case, "deep" copies are created for objects and arrays.

Parameters
clone- cloned object.
Returns
a copy of the json value.

Member Function Documentation

◆ as_array() [1/2]

template<typename K>
json_array & simjson::JsonValueTempl< K >::as_array ( )
inline

Get value as json Array. The debug version checks that the value is indeed an Array.

◆ as_array() [2/2]

template<typename K>
const json_array & simjson::JsonValueTempl< K >::as_array ( ) const
inline

Get value as json Array. The debug version checks that the value is indeed an Array.

◆ as_integer()

template<typename K>
int64_t simjson::JsonValueTempl< K >::as_integer ( ) const
inline

Get value as integer. The debug version checks that the value is really an integer

◆ as_object() [1/2]

template<typename K>
json_object & simjson::JsonValueTempl< K >::as_object ( )
inline

Get value as json Object. The debug version checks that the value is really Object.

◆ as_object() [2/2]

template<typename K>
const json_object & simjson::JsonValueTempl< K >::as_object ( ) const
inline

Get value as json Object. The debug version checks that the value is indeed Object.

◆ as_real()

template<typename K>
double simjson::JsonValueTempl< K >::as_real ( ) const
inline

Get the value as double. The debug version checks that the value is really double

◆ as_text()

template<typename K>
const strType & simjson::JsonValueTempl< K >::as_text ( ) const
inline

Get the value as text. The debug version checks that the value is actually text.

◆ at() [1/2]

template<typename K>
const json_value & simjson::JsonValueTempl< K >::at ( size_t idx) const
inline

Accessing a constant array element by index. If this is not a json array or an index outside the bounds of the array, it returns a reference to UNDEFINED.

◆ at() [2/2]

template<typename K>
template<jt::JsonKeyType< K > T>
const json_value & simjson::JsonValueTempl< K >::at ( T && key) const
inline

Access to a property of a constant object by key.

Template Parameters
T- key type.
Parameters
key- key.
Returns
const json_value& - reference to the key value. If the value is not an object, or there is no key, does not create a new pair, but returns a reference to UNDEFINED.

◆ boolean()

template<typename K>
std::optional< bool > simjson::JsonValueTempl< K >::boolean ( ) const
inline

Get a boolean if a boolean is stored, or nothing.

Example: bool val = json.boolean().value_or(false);

◆ boolean_or_throw()

template<typename K>
template<typename Exc, typename ... Args>
requires (std::is_constructible_v<Exc, Args...>)
bool simjson::JsonValueTempl< K >::boolean_or_throw ( Args &&... args) const
inline

Get boolean if boolean is stored, or throw an exception. Example: bool val = json.boolean_or_throw<std::runtime_error>("Need boolean value");

◆ clone()

template<typename K>
json_value simjson::JsonValueTempl< K >::clone ( ) const
inline

Clone value.

◆ integer()

template<typename K>
std::optional< int64_t > simjson::JsonValueTempl< K >::integer ( ) const
inline

Get an integer if the stored value is an integer, or nothing. Example: auto val = json.integer().value_or(10);

◆ integer_or_throw()

template<typename K>
template<typename Exc, typename ... Args>
requires (std::is_constructible_v<Exc, Args...>)
int64_t simjson::JsonValueTempl< K >::integer_or_throw ( Args &&... args) const
inline

Get an integer if an integer is stored, or throw an exception. Example: int val = json.integer_or_throw<std::runtime_error>("Need integer value");

◆ is_array()

template<typename K>
bool simjson::JsonValueTempl< K >::is_array ( ) const
inline

The value is array.

◆ is_boolean()

template<typename K>
bool simjson::JsonValueTempl< K >::is_boolean ( ) const
inline

The value is boolean.

◆ is_integer()

template<typename K>
bool simjson::JsonValueTempl< K >::is_integer ( ) const
inline

The value is integer.

◆ is_null()

template<typename K>
bool simjson::JsonValueTempl< K >::is_null ( ) const
inline

The value is null.

◆ is_object()

template<typename K>
bool simjson::JsonValueTempl< K >::is_object ( ) const
inline

The value is object.

◆ is_real()

template<typename K>
bool simjson::JsonValueTempl< K >::is_real ( ) const
inline

The value is real (double).

◆ is_text()

template<typename K>
bool simjson::JsonValueTempl< K >::is_text ( ) const
inline

The value is text.

◆ is_undefined()

template<typename K>
bool simjson::JsonValueTempl< K >::is_undefined ( ) const
inline

The value is undefined.

◆ merge()

template<typename K>
SIMJSON_API void simjson::JsonValueTempl< K >::merge ( const json_value & other,
bool replace = true,
bool append_arrays = false )

Merge with other JSON.

Parameters
other- another json value.
replace- replace with something else when merging.
append_arrays- when merging json arrays, combine them.

If replace == true then another JSON takes precedence and for simple types, if it is not undefined it will replace the current one. If both objects are json arrays, then when append_arrays = true, another array is appended to this array, otherwise if replace == true, it will replace the current array. If both are JSON objects, then they are merged by keys. Keys from another object that are not in the current one will be added to the current one object. Which exist - with replace == true they will be replaced.

◆ not_empty_text_or_throw()

template<typename K>
template<typename Exc, typename ... Args>
requires (std::is_constructible_v<Exc, Args...>)
const strType & simjson::JsonValueTempl< K >::not_empty_text_or_throw ( Args &&... args) const
inline

Get the text if the text is stored and it is not empty, or throw an exception. Example: auto val = json.text_or_throw<std::runtime_error>("Need text value");

◆ number_int()

template<typename K>
std::optional< int64_t > simjson::JsonValueTempl< K >::number_int ( ) const

Returns int64_t if stored int64_t or double that fits into int64_t, or nothing.

◆ number_int_or_throw()

template<typename K>
template<typename Exc, typename ... Args>
requires (std::is_constructible_v<Exc, Args...>)
int64_t simjson::JsonValueTempl< K >::number_int_or_throw ( Args &&... args) const
inline

Get int64_t if stored int64_t or double that fits into int64_t, or throw an exception.

◆ number_real()

template<typename K>
SIMJSON_API std::optional< double > simjson::JsonValueTempl< K >::number_real ( ) const

Returns double if double or int64_t is stored, or nothing.

◆ number_real_or_throw()

template<typename K>
template<typename Exc, typename ... Args>
requires (std::is_constructible_v<Exc, Args...>)
double simjson::JsonValueTempl< K >::number_real_or_throw ( Args &&... args) const
inline

Get double if double or int64_t is stored, or throw an exception.

◆ operator()()

template<typename K>
template<jt::JsonKeyType< K > T, typename... Args>
const json_value & simjson::JsonValueTempl< K >::operator() ( T && key,
Args &&... args ) const
inline

Access to a property of a constant object by a set of keys.

The function moves sequentially from value to value based on the given keys. As soon as the specified key is not found, the search stops. For example: config("a", "b", "c") for object {"a": {"b": {"c": 10}}} will return a reference to 10.

Template Parameters
T- key type.
Parameters
key- key.
...args- other keys.
Returns
const json_value& - reference to the key value. If the value is not an object, or the specified key does not exist, does not create a new pair, but returns a reference to UNDEFINED.

◆ operator=()

template<typename K>
JsonValueTempl & simjson::JsonValueTempl< K >::operator= ( json_value t)
inlinenoexcept

Assignment.

◆ operator[]() [1/4]

template<typename K>
json_value & simjson::JsonValueTempl< K >::operator[] ( size_t idx)
inline

Access an array element by index.

Parameters
idx- element index.
Returns
json_value& - a reference to the specified array element.

If this value is not a json array, "turns" it into an array. If index == -1 - adds one more element to the array. If the index is greater than the length of the array, increases the array to the specified index.

◆ operator[]() [2/4]

template<typename K>
const json_value & simjson::JsonValueTempl< K >::operator[] ( size_t idx) const
inline

Accessing a constant array element by index. If this is not a json array or an index outside the bounds of the array, it returns a reference to UNDEFINED.

◆ operator[]() [3/4]

template<typename K>
template<jt::JsonKeyType< K > T>
json_value & simjson::JsonValueTempl< K >::operator[] ( T && key)
inline

Access to an object property by key.

Template Parameters
T- type of the key.
Parameters
key- key.
Returns
json_value& - a reference to the key value.

If the value is not a json object, then "turns" it into a json object. If the specified key does not exist, adds this key with an empty value to the json object.

◆ operator[]() [4/4]

template<typename K>
template<jt::JsonKeyType< K > T>
const json_value & simjson::JsonValueTempl< K >::operator[] ( T && key) const
inline

Access to a property of a constant object by key.

Template Parameters
T- key type.
Parameters
key- key.
Returns
const json_value& - reference to the key value. If the value is not an object, or there is no key, does not create a new pair, but returns a reference to UNDEFINED.

◆ parse()

template<typename K>
std::tuple< JsonValueTempl< K >, JsonParseResult, unsigned, unsigned > simjson::JsonValueTempl< K >::parse ( ssType jsonString)
static

Parse text to json.

Parameters
jsonString- the text string to be parsed.
Returns
std::tuple<json_value, JsonParseResult, unsigned, unsigned> - tuple, содержащую: json_value - the resulting value if the parsing is successful, or UNDEFINED in case of errors; JsonParseResult - parsing error code, Success if successful; unsigned line, unsigned col - in case of an error, these are the line/column numbers where the error occurred.

◆ real()

template<typename K>
std::optional< double > simjson::JsonValueTempl< K >::real ( ) const
inline

Get double if double is stored, or nothing. Example: auto val = json.real().value_or(10.0);

◆ real_or_throw()

template<typename K>
template<typename Exc, typename ... Args>
requires (std::is_constructible_v<Exc, Args...>)
double simjson::JsonValueTempl< K >::real_or_throw ( Args &&... args)
inline

Get double if double is stored, or throw an exception. Example: double val = json.real_or_throw<std::runtime_error>("Need double value");

◆ set()

template<typename K>
template<jt::JsonKeyType< K > Key, typename ... Args>
json_value & simjson::JsonValueTempl< K >::set ( Key && key,
Args &&... args )
inline

Setting the value of a json object property by key.

Template Parameters
Key- the type of the key.
Args- types of arguments to create a json value for the specified key.
Parameters
key- key.
args- arguments for creating a json value for the specified key.
Returns
json_value& - a link to the json value for the specified key.

If the value is not a json object, then "turns" it into a json object. If the specified key does not exist, adds this key with the given arguments to the json object, otherwise assigns arguments to it.

◆ size()

template<typename K>
size_t simjson::JsonValueTempl< K >::size ( ) const
inline

The number of elements of a json array or keys of a json object.

◆ store() [1/2]

template<typename K>
lstring< K, 0, true > simjson::JsonValueTempl< K >::store ( bool prettify = false,
bool order_keys = false,
K indent_symbol = ' ',
unsigned indent_count = 2 ) const
inline

Serialize a json value to a string.

Parameters
prettify- "decorate", if true, line breaks and indentations will be added to the line.
order_keys- order the keys of json objects. According to the standard, the order of keys in JSON is not specified and does not affect for validity, but often it is required to repeat the results to stick to the same output order for different runs. In this case, the keys will be output in an orderly manner "byte-by-byte comparison".
indent_symbol- when "decorating" sets the symbol for indentation, default is space.
indent_count- number of indentation characters per level, default 2.
Returns
a string containing JSON.

◆ store() [2/2]

template<typename K>
SIMJSON_API void simjson::JsonValueTempl< K >::store ( lstring< K, 0, true > & stream,
bool prettify = false,
bool order_keys = false,
K indent_symbol = ' ',
unsigned indent_count = 2 ) const

Serialize json value to string.

Parameters
stream- the string to save into.
prettify- "decorate", if true, line breaks and indentations will be added to the text.
order_keys- order the keys of json objects. According to the standard, the order of keys in JSON is not specified and does not affect for validity, but often it is required to repeat the results to stick to the same output order for different runs. In this case, the keys will be output in an orderly manner "byte-by-byte comparison".
indent_symbol- when "decorating" sets the symbol for indentation, default is space.
indent_count- number of indentation characters per level, default 2.

◆ swap()

template<typename K>
void simjson::JsonValueTempl< K >::swap ( json_value & other)
inlinenoexcept

Exchange values.

◆ text()

template<typename K>
std::optional< strType > simjson::JsonValueTempl< K >::text ( ) const
inline

Get text if text is stored, or nothing. Example: auto val = json.text().value_or("default value");

◆ text_or_throw()

template<typename K>
template<typename Exc, typename ... Args>
requires (std::is_constructible_v<Exc, Args...>)
const strType & simjson::JsonValueTempl< K >::text_or_throw ( Args &&... args) const
inline

Get the text if text is stored, or throw an exception. Example: auto val = json.text_or_throw<std::runtime_error>("Need text value");

◆ to_boolean()

template<typename K>
SIMJSON_API bool simjson::JsonValueTempl< K >::to_boolean ( ) const

Get the value converted to boolean. The logic of operation is the same as in javascript !!val. Example: bool val = json.to_boolean();

◆ to_integer_or_throw()

template<typename K>
template<typename Exc, typename ... Args>
requires (std::is_constructible_v<Exc, Args...>)
int64_t simjson::JsonValueTempl< K >::to_integer_or_throw ( Args &&... args) const
inline

Get the value from to_integer if it exists, or throw an exception.

◆ to_real()

template<typename K>
SIMJSON_API double simjson::JsonValueTempl< K >::to_real ( ) const

Get the value converted to double. The logic is the same as in javascript 1 * val. For "non-numbers" returns NaN.

◆ to_text()

template<typename K>
SIMJSON_API JsonValueTempl< K >::strType simjson::JsonValueTempl< K >::to_text ( ) const

Get the value converted to text. The logic is the same as in javascript "" + val.

◆ type()

template<typename K>
Type simjson::JsonValueTempl< K >::type ( ) const
inline

Get value type.


The documentation for this class was generated from the following files: