Класс для представления 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 |
| JsonValueTempl & | operator= (json_value t) noexcept |
| template<jt::JsonKeyType< K > T> | |
| const json_value & | at (T &&key) const |
| Access to a property of a constant object by key. | |
| template<jt::JsonKeyType< K > T> | |
| const json_value & | operator[] (T &&key) const |
| Access to a property of a constant object by key. | |
| template<jt::JsonKeyType< K > T, typename... Args> | |
| const json_value & | operator() (T &&key, Args &&...args) const |
| Access to a property of a constant object by a set of keys. | |
| template<jt::JsonKeyType< K > T> | |
| json_value & | operator[] (T &&key) |
| Access to an object property by key. | |
| template<jt::JsonKeyType< K > Key, typename ... Args> | |
| json_value & | set (Key &&key, Args &&... args) |
| Setting the value of a json object property by key. | |
| const json_value & | at (size_t idx) const |
| const json_value & | operator[] (size_t idx) const |
| json_value & | operator[] (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. | |
Класс для представления json значения.
| K | - тип символов. |
Class to represent a json value.
| K | - character type. |
|
inline |
Creates an empty object of type Undefined.
| SIMJSON_API simjson::JsonValueTempl< K >::JsonValueTempl | ( | const JsonValueTempl< K > & | other | ) |
Copy constructor. Objects and arrays are copied by reference.
|
inlinenoexcept |
Move constructor.
| SIMJSON_API simjson::JsonValueTempl< K >::~JsonValueTempl | ( | ) |
Destructor.
|
inline |
Constructor from int8_t.
|
inline |
Constructor from int16_t.
|
inline |
Constructor from int32_t.
|
inline |
Constructor from int64_t.
|
inline |
Constructor from bool.
|
inline |
Constructor from double.
|
inline |
Constructor from sstring.
|
inline |
Constructor from the other strings.
|
inline |
Constructor from string expressions.
|
inline |
Constructor from string literals.
|
inline |
Constructor for creating json::null.
|
inline |
Constructor for creating an empty string.
|
inline |
Constructor for creating json::object.
|
inline |
Constructor for creating json::array.
| SIMJSON_API simjson::JsonValueTempl< K >::JsonValueTempl | ( | Type | type | ) |
Constructor for creating a default value with type type.
|
inline |
Constructor from any standard array container containing JSON values.
| obj | is the object. |
|
inline |
Constructor from any standard container with pairs containing a string key in first and a JSON value in second.
| obj | is the object. |
| SIMJSON_API simjson::JsonValueTempl< K >::JsonValueTempl | ( | const Clone & | clone | ) |
Clone constructor. In this case, "deep" copies are created for objects and arrays.
| clone | - cloned object. |
|
inline |
Get value as json Array. The debug version checks that the value is indeed an Array.
|
inline |
Get value as json Array. The debug version checks that the value is indeed an Array.
|
inline |
Get value as integer. The debug version checks that the value is really an integer
|
inline |
Get value as json Object. The debug version checks that the value is really Object.
|
inline |
Get value as json Object. The debug version checks that the value is indeed Object.
|
inline |
Get the value as double. The debug version checks that the value is really double
|
inline |
Get the value as text. The debug version checks that the value is actually text.
|
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.
|
inline |
Access to a property of a constant object by key.
| T | - key type. |
| key | - key. |
|
inline |
Get a boolean if a boolean is stored, or nothing.
Example: bool val = json.boolean().value_or(false);
|
inline |
Get boolean if boolean is stored, or throw an exception. Example: bool val = json.boolean_or_throw<std::runtime_error>("Need boolean value");
|
inline |
Clone value.
|
inline |
Get an integer if the stored value is an integer, or nothing. Example: auto val = json.integer().value_or(10);
|
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");
|
inline |
The value is array.
|
inline |
The value is boolean.
|
inline |
The value is integer.
|
inline |
The value is null.
|
inline |
The value is object.
|
inline |
The value is real (double).
|
inline |
The value is text.
|
inline |
The value is undefined.
| SIMJSON_API void simjson::JsonValueTempl< K >::merge | ( | const json_value & | other, |
| bool | replace = true, | ||
| bool | append_arrays = false ) |
Merge with other JSON.
| 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.
|
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");
| 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.
|
inline |
Get int64_t if stored int64_t or double that fits into int64_t, or throw an exception.
| SIMJSON_API std::optional< double > simjson::JsonValueTempl< K >::number_real | ( | ) | const |
Returns double if double or int64_t is stored, or nothing.
|
inline |
Get double if double or int64_t is stored, or throw an exception.
|
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.
| T | - key type. |
| key | - key. |
| ...args | - other keys. |
|
inlinenoexcept |
Assignment.
|
inline |
Access an array element by index.
| idx | - element index. |
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.
|
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.
|
inline |
Access to an object property by key.
| T | - type of the key. |
| key | - 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 an empty value to the json object.
|
inline |
Access to a property of a constant object by key.
| T | - key type. |
| key | - key. |
|
static |
Parse text to json.
| jsonString | - the text string to be parsed. |
|
inline |
Get double if double is stored, or nothing. Example: auto val = json.real().value_or(10.0);
|
inline |
Get double if double is stored, or throw an exception. Example: double val = json.real_or_throw<std::runtime_error>("Need double value");
|
inline |
Setting the value of a json object property by key.
| Key | - the type of the key. |
| Args | - types of arguments to create a json value for the specified key. |
| key | - key. |
| args | - arguments for creating a 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.
|
inline |
The number of elements of a json array or keys of a json object.
|
inline |
Serialize a json value to a string.
| 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. |
| 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.
| 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. |
|
inlinenoexcept |
Exchange values.
|
inline |
Get text if text is stored, or nothing. Example: auto val = json.text().value_or("default value");
|
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");
| 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();
|
inline |
Get the value from to_integer if it exists, or throw an exception.
| 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.
| 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.
|
inline |
Get value type.