#include <genesis/utils/formats/json/document.hpp>
Store a Json value of any kind.
This class is a heavily altered adaption of the excellent JSON for Modern C++ library by Niels Lohmann, see also Acknowledgements.
Definition at line 114 of file json/document.hpp.
Public Member Functions | |
JsonDocument (ArrayType const &v) | |
Create an array, explicitly. More... | |
JsonDocument (BooleanType v) | |
Create a boolean, explicitly. More... | |
template<class CompatibleObjectType , typename std::enable_if< std::is_constructible< typename ObjectType::key_type, typename CompatibleObjectType::key_type >::value and std::is_constructible< JsonDocument, typename CompatibleObjectType::mapped_type >::value, int >::type = 0> | |
JsonDocument (CompatibleObjectType const &val) | |
Create an object (implicit). More... | |
template<class CompatibleArrayTypeT , typename std::enable_if< not std::is_same< CompatibleArrayTypeT, typename JsonDocument::iterator >::value and not std::is_same< CompatibleArrayTypeT, typename JsonDocument::const_iterator >::value and not std::is_same< CompatibleArrayTypeT, typename ArrayType::iterator >::value and not std::is_same< CompatibleArrayTypeT, typename ArrayType::const_iterator >::value and std::is_constructible< JsonDocument, typename CompatibleArrayTypeT::value_type >::value, int >::type = 0> | |
JsonDocument (const CompatibleArrayTypeT &val) | |
template<class CompatibleStringType , typename std::enable_if< std::is_constructible< StringType, CompatibleStringType >::value, int >::type = 0> | |
JsonDocument (const CompatibleStringType &val) | |
Create a string (implicit). More... | |
JsonDocument (const NumberFloatType val) | |
Create a floating-point number (explicit). More... | |
template<typename T , typename std::enable_if< not(std::is_same< T, int >::value) and std::is_same< T, NumberSignedType >::value, int >::type = 0> | |
JsonDocument (const NumberSignedType val) | |
Create a signed integer number (explicit). More... | |
template<typename T , typename std::enable_if< not(std::is_same< T, int >::value) and std::is_same< T, NumberUnsignedType >::value, int >::type = 0> | |
JsonDocument (const NumberUnsignedType val) | |
Create an unsigned integer number (explicit). More... | |
JsonDocument (int const val) | |
Create a signed integer number from an enum type (explicit). More... | |
JsonDocument (JsonDocument &&other) | |
Move constructor. More... | |
JsonDocument (JsonDocument const &other) | |
Copy constructor. More... | |
JsonDocument (ObjectType const &v) | |
Create an object, explicitly. More... | |
JsonDocument (size_t n, JsonDocument const &val) | |
Construct an array with n many copies of val . More... | |
JsonDocument (std::initializer_list< JsonDocument > init, bool type_deduction=true, ValueType manual_type=ValueType::kArray) | |
Create a container (array or object) from an initializer list. More... | |
JsonDocument (std::nullptr_t=nullptr) | |
Create a null object. More... | |
JsonDocument (StringType const &v) | |
Create a string, explicitly. More... | |
JsonDocument (typename StringType::value_type const *v) | |
Create a string, explicitly, using a char pointer. More... | |
JsonDocument (ValueType type) | |
Create an empty value of the given type . More... | |
~JsonDocument () | |
Destructor. More... | |
JsonDocument & | at (size_t index) |
JsonDocument const & | at (size_t index) const |
JsonDocument & | at (typename ObjectType::key_type const &key) |
JsonDocument const & | at (typename ObjectType::key_type const &key) const |
iterator | begin () |
Return an iterator to the first element. More... | |
const_iterator | begin () const |
Return a const iterator to the first element. More... | |
const_iterator | cbegin () const |
Return a const iterator to the first element. More... | |
const_iterator | cend () const |
Return a const iterator to one past the last element. More... | |
void | clear () |
Clears the content of a JSON value and resets it to the default value as if JsonDocument(ValueType) would have been called. More... | |
size_type | count (typename ObjectType::key_type key) const |
Return the number of occurrences of a key in a JSON object. More... | |
template<class... Args> | |
void | emplace_back (Args &&... args) |
bool | empty () const |
iterator | end () |
Return an iterator to one past the last element. More... | |
const_iterator | end () const |
Return a const iterator to one past the last element. More... | |
iterator | find (typename JsonDocument::ObjectType::key_type key) |
Find an element in a JSON object. More... | |
const_iterator | find (typename JsonDocument::ObjectType::key_type key) const |
Find an element in a JSON object. More... | |
ArrayType & | get_array () |
ArrayType const & | get_array () const |
BooleanType & | get_boolean () |
BooleanType | get_boolean () const |
template<typename T > | |
T | get_number () const |
NumberFloatType & | get_number_float () |
NumberFloatType | get_number_float () const |
NumberSignedType & | get_number_signed () |
NumberSignedType | get_number_signed () const |
NumberUnsignedType & | get_number_unsigned () |
NumberUnsignedType | get_number_unsigned () const |
ObjectType & | get_object () |
ObjectType const & | get_object () const |
StringType & | get_string () |
StringType const & | get_string () const |
bool | is_array () const |
Return true iff the JSON value is an array. More... | |
bool | is_boolean () const |
Return true iff the JSON value is a boolean. More... | |
bool | is_null () const |
Return true iff the JSON value is null. More... | |
bool | is_number () const |
Return true iff the JSON value is a number, i.e., a float or signed or unsigned integer. More... | |
bool | is_number_float () const |
Return true iff the JSON value is a float number. More... | |
bool | is_number_integer () const |
Return true iff the JSON value is a signed or unsigned integer number. More... | |
bool | is_number_signed () const |
Return true iff the JSON value is a signed integer number. More... | |
bool | is_number_unsigned () const |
Return true iff the JSON value is an unsigned integer number. More... | |
bool | is_object () const |
Return true iff the JSON value is an object. More... | |
bool | is_primitive () const |
Return true iff the JSON type is primitive (string, number, boolean, or null). More... | |
bool | is_string () const |
Return true iff the JSON value is a string. More... | |
bool | is_structured () const |
Return true iff the JSON type is structured (array or object). More... | |
size_t | max_size () const |
JsonDocument & | operator= (JsonDocument other) |
Copy assignment. More... | |
JsonDocument & | operator[] (size_t index) |
JsonDocument const & | operator[] (size_t index) const |
JsonDocument & | operator[] (typename ObjectType::key_type const &key) |
JsonDocument const & | operator[] (typename ObjectType::key_type const &key) const |
void | push_back (JsonDocument &&val) |
Add a Json value to an array. More... | |
void | push_back (JsonDocument const &val) |
Add a Json value to an array. More... | |
void | push_back (typename ObjectType::value_type const &val) |
Add an object to an object. More... | |
size_t | size () const |
ValueType | type () const |
Return the type of the JSON value. More... | |
std::string | type_name () const |
Static Public Member Functions | |
static JsonDocument | array (std::initializer_list< JsonDocument > init=std::initializer_list< JsonDocument >()) |
Explicitly create an array from an initializer list. More... | |
static JsonDocument | boolean (BooleanType value) |
Explicitly create a boolean. More... | |
static JsonDocument | number_float (NumberFloatType value) |
Explicitly create a float number. More... | |
static JsonDocument | number_signed (NumberSignedType value) |
Explicitly create a signed number. More... | |
static JsonDocument | number_unsigned (NumberUnsignedType value) |
Explicitly create an unsigned number. More... | |
static JsonDocument | object (std::initializer_list< JsonDocument > init=std::initializer_list< JsonDocument >()) |
Explicitly create an object from an initializer list. More... | |
static JsonDocument | string (std::string const &init) |
Explicitly create a string. More... | |
Public Types | |
using | ArrayType = std::vector< JsonDocument > |
using | BooleanType = bool |
using | const_iterator = JsonIterator< JsonDocument const > |
using | const_pointer = JsonDocument const * |
using | const_reference = JsonDocument const & |
using | difference_type = std::ptrdiff_t |
using | iterator = JsonIterator< JsonDocument > |
using | NumberFloatType = double |
using | NumberSignedType = std::int64_t |
using | NumberUnsignedType = std::uint64_t |
using | ObjectType = std::map< std::string, JsonDocument > |
using | pointer = JsonDocument * |
using | reference = JsonDocument & |
using | size_type = std::size_t |
using | StringType = std::string |
using | value_type = JsonDocument |
enum | ValueType : uint8_t { kNull, kArray, kObject, kString, kBoolean, kNumberFloat, kNumberSigned, kNumberUnsigned } |
Friends | |
class | JsonIterator< JsonDocument > |
class | JsonIterator< JsonDocument const > |
bool | operator!= (const_reference lhs, const_reference rhs) |
Compare not equal. More... | |
bool | operator!= (const_reference v, std::nullptr_t) |
Compare not equal. More... | |
bool | operator!= (std::nullptr_t, const_reference v) |
Compare not equal. More... | |
bool | operator< (const ValueType lhs, const ValueType rhs) |
Comparison of Json types (instead of values). More... | |
bool | operator< (const_reference lhs, const_reference rhs) |
Compare less than. More... | |
bool | operator<= (const_reference lhs, const_reference rhs) |
Compare less than or equal. More... | |
bool | operator== (const_reference lhs, const_reference rhs) |
Compare equal. More... | |
bool | operator== (const_reference v, std::nullptr_t) |
Compare equal. More... | |
bool | operator== (std::nullptr_t, const_reference v) |
Compare equal. More... | |
bool | operator> (const_reference lhs, const_reference rhs) |
Compare greater than. More... | |
bool | operator>= (const_reference lhs, const_reference rhs) |
Compare greater than or equal. More... | |
|
inline |
Create a null object.
Create a null
JSON value. It either takes a null pointer as parameter (explicitly creating null
) or no parameter (implicitly creating null
).
Definition at line 269 of file json/document.hpp.
|
inline |
Create an empty value of the given type
.
Definition at line 278 of file json/document.hpp.
|
inline |
Create an array, explicitly.
Definition at line 289 of file json/document.hpp.
|
inline |
Create an object, explicitly.
Definition at line 299 of file json/document.hpp.
|
inline |
Create a string, explicitly.
Definition at line 309 of file json/document.hpp.
|
inline |
Create a string, explicitly, using a char pointer.
Definition at line 319 of file json/document.hpp.
|
inline |
Create a boolean, explicitly.
Definition at line 329 of file json/document.hpp.
|
inline |
Create a signed integer number (explicit).
Definition at line 343 of file json/document.hpp.
|
inline |
Create a signed integer number from an enum type (explicit).
Definition at line 353 of file json/document.hpp.
|
inline |
Create an unsigned integer number (explicit).
Definition at line 367 of file json/document.hpp.
|
inline |
Create a floating-point number (explicit).
RFC 7159, section 6 disallows NaN values:
Numeric values that cannot be represented in the grammar below (such as Infinity and NaN) are not permitted.
In case the parameter val is not a number, a JSON null value is created instead.
Definition at line 381 of file json/document.hpp.
JsonDocument | ( | std::initializer_list< JsonDocument > | init, |
bool | type_deduction = true , |
||
ValueType | manual_type = ValueType::kArray |
||
) |
Create a container (array or object) from an initializer list.
Definition at line 86 of file json/document.cpp.
|
inline |
Construct an array with n
many copies of val
.
Definition at line 410 of file json/document.hpp.
|
inline |
Definition at line 427 of file json/document.hpp.
|
inline |
Create an object (implicit).
Create an object JSON value with a given content. This constructor allows any type CompatibleObjectType that can be used to construct values of type ObjectType.
Definition at line 453 of file json/document.hpp.
|
inline |
Create a string (implicit).
Create a string JSON value with a given content with a type which is compatible to StringType, for instance std::string
.
Definition at line 471 of file json/document.hpp.
JsonDocument | ( | JsonDocument const & | other | ) |
Copy constructor.
Definition at line 140 of file json/document.cpp.
JsonDocument | ( | JsonDocument && | other | ) |
Move constructor.
Definition at line 187 of file json/document.cpp.
~JsonDocument | ( | ) |
Destructor.
Definition at line 223 of file json/document.cpp.
|
inlinestatic |
Explicitly create an array from an initializer list.
Definition at line 508 of file json/document.hpp.
JsonDocument & at | ( | size_t | index | ) |
Definition at line 433 of file json/document.cpp.
JsonDocument const & at | ( | size_t | index | ) | const |
Definition at line 449 of file json/document.cpp.
JsonDocument & at | ( | typename ObjectType::key_type const & | key | ) |
Definition at line 465 of file json/document.cpp.
JsonDocument const & at | ( | typename ObjectType::key_type const & | key | ) | const |
Definition at line 481 of file json/document.cpp.
JsonDocument::const_iterator begin | ( | ) |
Return an iterator to the first element.
Definition at line 591 of file json/document.cpp.
const_iterator begin | ( | ) | const |
Return a const iterator to the first element.
|
inlinestatic |
Explicitly create a boolean.
Definition at line 535 of file json/document.hpp.
JsonDocument::const_iterator cbegin | ( | ) | const |
Return a const iterator to the first element.
Definition at line 603 of file json/document.cpp.
JsonDocument::const_iterator cend | ( | ) | const |
Return a const iterator to one past the last element.
Definition at line 622 of file json/document.cpp.
void clear | ( | ) |
Clears the content of a JSON value and resets it to the default value as if JsonDocument(ValueType) would have been called.
The values are cleared as follows:
Value type | initial value |
---|---|
null | null |
boolean | false |
string | "" |
number | 0 |
object | {} |
array | [] |
In order to complete eliminate a Json value, set it to nullptr
instead.
Definition at line 633 of file json/document.cpp.
JsonDocument::size_type count | ( | typename ObjectType::key_type | key | ) | const |
Return the number of occurrences of a key in a JSON object.
As we use a std::map for objects, this value is either 0 (key not found) or 1 (key found). For non-object types, the return value is always 0.
Definition at line 581 of file json/document.cpp.
|
inline |
Definition at line 865 of file json/document.hpp.
bool empty | ( | ) | const |
Definition at line 251 of file json/document.cpp.
JsonDocument::const_iterator end | ( | ) |
Return an iterator to one past the last element.
Definition at line 610 of file json/document.cpp.
const_iterator end | ( | ) | const |
Return a const iterator to one past the last element.
JsonDocument::iterator find | ( | typename JsonDocument::ObjectType::key_type | key | ) |
Find an element in a JSON object.
Finds an element in a JSON object with key equivalent to key. If the element is not found or the JSON value is not an object, end() is returned.
Definition at line 563 of file json/document.cpp.
JsonDocument::const_iterator find | ( | typename JsonDocument::ObjectType::key_type | key | ) | const |
Find an element in a JSON object.
Finds an element in a JSON object with key equivalent to key. If the element is not found or the JSON value is not an object, end() is returned.
Definition at line 572 of file json/document.cpp.
JsonDocument::ArrayType const & get_array | ( | ) |
Definition at line 317 of file json/document.cpp.
ArrayType const& get_array | ( | ) | const |
JsonDocument::BooleanType get_boolean | ( | ) |
Definition at line 365 of file json/document.cpp.
BooleanType get_boolean | ( | ) | const |
|
inline |
Definition at line 725 of file json/document.hpp.
JsonDocument::NumberFloatType get_number_float | ( | ) |
Definition at line 381 of file json/document.cpp.
NumberFloatType get_number_float | ( | ) | const |
JsonDocument::NumberSignedType get_number_signed | ( | ) |
Definition at line 397 of file json/document.cpp.
NumberSignedType get_number_signed | ( | ) | const |
JsonDocument::NumberUnsignedType get_number_unsigned | ( | ) |
Definition at line 413 of file json/document.cpp.
NumberUnsignedType get_number_unsigned | ( | ) | const |
JsonDocument::ObjectType const & get_object | ( | ) |
Definition at line 333 of file json/document.cpp.
ObjectType const& get_object | ( | ) | const |
JsonDocument::StringType const & get_string | ( | ) |
Definition at line 349 of file json/document.cpp.
StringType const& get_string | ( | ) | const |
|
inline |
Return true iff the JSON value is an array.
Definition at line 617 of file json/document.hpp.
|
inline |
Return true iff the JSON value is a boolean.
Definition at line 641 of file json/document.hpp.
|
inline |
Return true iff the JSON value is null.
Definition at line 609 of file json/document.hpp.
|
inline |
Return true iff the JSON value is a number, i.e., a float or signed or unsigned integer.
Definition at line 650 of file json/document.hpp.
|
inline |
Return true iff the JSON value is a float number.
Definition at line 658 of file json/document.hpp.
|
inline |
Return true iff the JSON value is a signed or unsigned integer number.
Definition at line 666 of file json/document.hpp.
|
inline |
Return true iff the JSON value is a signed integer number.
Definition at line 674 of file json/document.hpp.
|
inline |
Return true iff the JSON value is an unsigned integer number.
Definition at line 682 of file json/document.hpp.
|
inline |
Return true iff the JSON value is an object.
Definition at line 625 of file json/document.hpp.
|
inline |
Return true iff the JSON type is primitive (string, number, boolean, or null).
Definition at line 593 of file json/document.hpp.
|
inline |
Return true iff the JSON value is a string.
Definition at line 633 of file json/document.hpp.
|
inline |
Return true iff the JSON type is structured (array or object).
Definition at line 601 of file json/document.hpp.
size_t max_size | ( | ) | const |
Definition at line 295 of file json/document.cpp.
|
inlinestatic |
Explicitly create a float number.
Definition at line 546 of file json/document.hpp.
|
inlinestatic |
Explicitly create a signed number.
Definition at line 557 of file json/document.hpp.
|
inlinestatic |
Explicitly create an unsigned number.
Definition at line 568 of file json/document.hpp.
|
inlinestatic |
Explicitly create an object from an initializer list.
Definition at line 517 of file json/document.hpp.
JsonDocument & operator= | ( | JsonDocument | other | ) |
Copy assignment.
Definition at line 205 of file json/document.cpp.
JsonDocument & operator[] | ( | size_t | index | ) |
Definition at line 497 of file json/document.cpp.
JsonDocument const & operator[] | ( | size_t | index | ) | const |
Definition at line 522 of file json/document.cpp.
JsonDocument & operator[] | ( | typename ObjectType::key_type const & | key | ) |
Definition at line 531 of file json/document.cpp.
JsonDocument const & operator[] | ( | typename ObjectType::key_type const & | key | ) | const |
Definition at line 548 of file json/document.cpp.
void push_back | ( | JsonDocument && | val | ) |
Add a Json value to an array.
Appends the given element val to the end of the JSON value. If the function is called on a JSON null value, an empty array is created before appending val.void push_back( JsonDocument&& val );
Definition at line 670 of file json/document.cpp.
void push_back | ( | JsonDocument const & | val | ) |
Add a Json value to an array.
Appends the given element val to the end of the JSON value. If the function is called on a JSON null value, an empty array is created before appending val.void push_back( JsonDocument&& val );
Definition at line 690 of file json/document.cpp.
void push_back | ( | typename ObjectType::value_type const & | val | ) |
Add an object to an object.
Inserts the given element val to the JSON object. If the function is called on a JSON null value, an empty object is created before inserting val.
Definition at line 708 of file json/document.cpp.
size_t size | ( | ) | const |
Definition at line 273 of file json/document.cpp.
|
inlinestatic |
Explicitly create a string.
Definition at line 526 of file json/document.hpp.
|
inline |
Return the type of the JSON value.
Definition at line 585 of file json/document.hpp.
std::string type_name | ( | ) | const |
Definition at line 880 of file json/document.cpp.
|
friend |
Definition at line 120 of file json/document.hpp.
|
friend |
Definition at line 121 of file json/document.hpp.
|
friend |
Compare not equal.
See operator==(const_reference lhs, const_reference rhs) for details.
Definition at line 929 of file json/document.hpp.
|
friend |
Compare not equal.
See operator==(const_reference v, std::nullptr_t) for details.
Definition at line 939 of file json/document.hpp.
|
friend |
Compare not equal.
See operator==(const_reference v, std::nullptr_t) for details.
Definition at line 949 of file json/document.hpp.
Comparison of Json types (instead of values).
Returns an ordering that is similar to Python:
Definition at line 859 of file json/document.cpp.
|
friend |
Compare less than.
Compares whether one JSON value lhs is less than another JSON value rhs according to the following rules:
<
operator.Definition at line 793 of file json/document.cpp.
|
friend |
Compare less than or equal.
Compares whether one JSON value lhs is less than or equal to another JSON value by calculating not (rhs < lhs)
.
Definition at line 975 of file json/document.hpp.
|
friend |
Compare equal.
Compares two JSON values for equality according to the following rules:
f1
and f2
are considered equal if neither f1 > f2
nor f2 > f1
holds.Definition at line 730 of file json/document.cpp.
|
friend |
Compare equal.
The functions compares the given JSON value against a null pointer. As the null pointer can be used to initialize a JSON value to null, a comparison of JSON value v with a null pointer should be equivalent to call v.is_null()
.
Definition at line 911 of file json/document.hpp.
|
friend |
Compare equal.
The functions compares the given JSON value against a null pointer. As the null pointer can be used to initialize a JSON value to null, a comparison of JSON value v with a null pointer should be equivalent to call v.is_null()
.
Definition at line 919 of file json/document.hpp.
|
friend |
Compare greater than.
Compares whether one JSON value lhs is greater than another JSON value by calculating not (lhs <= rhs)
.
Definition at line 986 of file json/document.hpp.
|
friend |
Compare greater than or equal.
Compares whether one JSON value lhs is greater than or equal to another JSON value by calculating not (lhs < rhs)
.
Definition at line 997 of file json/document.hpp.
using ArrayType = std::vector< JsonDocument > |
Definition at line 142 of file json/document.hpp.
using BooleanType = bool |
Definition at line 146 of file json/document.hpp.
using const_iterator = JsonIterator<JsonDocument const> |
Definition at line 137 of file json/document.hpp.
using const_pointer = JsonDocument const* |
Definition at line 134 of file json/document.hpp.
using const_reference = JsonDocument const& |
Definition at line 131 of file json/document.hpp.
using difference_type = std::ptrdiff_t |
Definition at line 139 of file json/document.hpp.
using iterator = JsonIterator<JsonDocument> |
Definition at line 136 of file json/document.hpp.
using NumberFloatType = double |
Definition at line 147 of file json/document.hpp.
using NumberSignedType = std::int64_t |
Definition at line 148 of file json/document.hpp.
using NumberUnsignedType = std::uint64_t |
Definition at line 149 of file json/document.hpp.
using ObjectType = std::map< std::string, JsonDocument > |
Definition at line 143 of file json/document.hpp.
using pointer = JsonDocument* |
Definition at line 133 of file json/document.hpp.
using reference = JsonDocument& |
Definition at line 130 of file json/document.hpp.
using size_type = std::size_t |
Definition at line 140 of file json/document.hpp.
using StringType = std::string |
Definition at line 144 of file json/document.hpp.
using value_type = JsonDocument |
Definition at line 129 of file json/document.hpp.
|
strong |
Enumerator | |
---|---|
kNull | |
kArray | |
kObject | |
kString | |
kBoolean | |
kNumberFloat | |
kNumberSigned | |
kNumberUnsigned |
Definition at line 151 of file json/document.hpp.