xrpld
Loading...
Searching...
No Matches
json::Value Class Reference

Represents a JSON value. More...

#include <json_value.h>

Collaboration diagram for json::Value:

Classes

class  CZString
union  ValueHolder

Public Types

using Members = std::vector<std::string>
using iterator = ValueIterator
using const_iterator = ValueConstIterator
using UInt = json::UInt
using Int = json::Int
using ArrayIndex = UInt
using ObjectValues = std::map<CZString, Value>

Public Member Functions

 Value (ValueType type=ValueType::Null)
 Create a default Value of the given type.
 Value (Int value)
 Value (UInt value)
 Value (double value)
 Value (char const *value)
 Value (xrpl::Number const &value)
 Value (StaticString const &value)
 Constructs a value from a static string.
 Value (std::string const &value)
 Constructs a value from a string.
 Value (std::string_view value)
 Constructs a value from a string view.
 Value (bool value)
 Value (Value const &other)
 ~Value ()
Value & operator= (Value const &other)
Value & operator= (Value &&other)
 Value (Value &&other) noexcept
void swap (Value &other) noexcept
 Swap values.
ValueType type () const
char const * asCString () const
std::string asString () const
 Returns the unquoted string value.
Int asInt () const
UInt asUInt () const
double asDouble () const
bool asBool () const
UInt asAbsUInt () const
 Correct absolute value from int or unsigned int.
bool isNull () const
 isNull() tests to see if this field is null.
bool isBool () const
bool isInt () const
bool isUInt () const
bool isIntegral () const
bool isDouble () const
bool isNumeric () const
bool isString () const
bool isArray () const
bool isArrayOrNull () const
bool isObject () const
bool isObjectOrNull () const
bool isConvertibleTo (ValueType other) const
UInt size () const
 Number of values in array or object.
 operator bool () const
 Returns false if this is an empty array, empty object, empty string, or null.
void clear ()
 Remove all object members and array elements.
Value & operator[] (UInt index)
 Access an array element (zero based index ).
Value const & operator[] (UInt index) const
 Access an array element (zero based index ) (You may need to say 'value[0u]' to get your compiler to distinguish this from the operator[] which takes a string.).
Value get (UInt index, Value const &defaultValue) const
 If the array contains at least index+1 elements, returns the element value, otherwise returns defaultValue.
bool isValidIndex (UInt index) const
 Return true if index < size().
Value & append (Value const &value)
 Append value to array at the end.
Value & append (Value &&value)
Value & operator[] (char const *key)
 Access an object value by name, create a null member if it does not exist.
Value const & operator[] (char const *key) const
 Access an object value by name, returns null if there is no member with that name.
Value & operator[] (std::string const &key)
 Access an object value by name, create a null member if it does not exist.
Value const & operator[] (std::string const &key) const
 Access an object value by name, returns null if there is no member with that name.
Value & operator[] (StaticString const &key)
 Access an object value by name, create a null member if it does not exist.
Value const & operator[] (StaticString const &key) const
Value get (char const *key, Value const &defaultValue) const
 Return the member named key if it exist, defaultValue otherwise.
Value get (std::string const &key, Value const &defaultValue) const
 Return the member named key if it exist, defaultValue otherwise.
Value removeMember (char const *key)
 Remove and return the named member.
Value removeMember (std::string const &key)
 Same as removeMember(const char*).
bool isMember (char const *key) const
 Return true if the object has a member named key.
bool isMember (std::string const &key) const
 Return true if the object has a member named key.
bool isMember (StaticString const &key) const
 Return true if the object has a member named key.
Members getMemberNames () const
 Return a list of the member names.
std::string toStyledString () const
const_iterator begin () const
const_iterator end () const
iterator begin ()
iterator end ()

Static Public Attributes

static Value const kNull
static constexpr Int kMinInt = std::numeric_limits<Int>::min()
static constexpr Int kMaxInt = std::numeric_limits<Int>::max()
static constexpr UInt kMaxUInt = std::numeric_limits<UInt>::max()

Private Member Functions

Value & resolveReference (char const *key, bool isStatic)

Private Attributes

union json::Value::ValueHolder value_
ValueType type_: 8
int allocated_: 1 {}

Friends

class ValueIteratorBase
bool operator== (Value const &x, Value const &y)
bool operator< (Value const &x, Value const &y)

Detailed Description

Represents a JSON value.

This class is a discriminated union wrapper that can represent a:

The type of the held value is represented by a ValueType and can be obtained using type().

values of an ValueType::Object or ValueType::Array can be accessed using operator[]() methods. Non const methods will automatically create the a ValueType::Null element if it does not exist. The sequence of an ValueType::Array will be automatically resize and initialized with ValueType::Null. resize() can be used to enlarge or truncate an ValueType::Array.

The get() methods can be used to obtain a default value in the case the required element does not exist.

It is possible to iterate over the list of a ValueType::Object values using the getMemberNames() method.

Definition at line 120 of file json_value.h.

Member Typedef Documentation

◆ Members

Definition at line 125 of file json_value.h.

◆ iterator

Definition at line 126 of file json_value.h.

◆ const_iterator

Definition at line 127 of file json_value.h.

◆ UInt

Definition at line 128 of file json_value.h.

◆ Int

Definition at line 129 of file json_value.h.

◆ ArrayIndex

Definition at line 130 of file json_value.h.

◆ ObjectValues

Definition at line 166 of file json_value.h.

Constructor & Destructor Documentation

◆ Value() [1/12]

json::Value::Value ( ValueType type = ValueType::Null)

Create a default Value of the given type.

This is a very useful constructor. To create an empty array, pass ValueType::Array. To create an empty object, pass ValueType::Object. Another Value can then be set to this one by assignment. This is useful since clear() and resize() will not alter types.

   Examples:
json::Value null_value; // null
Represents a JSON value.
Definition json_value.h:121
@ Array
array value (ordered list)
Definition json_value.h:32
@ Object
object value (collection of name/value pairs).
Definition json_value.h:33

Definition at line 183 of file json_value.cpp.

◆ Value() [2/12]

json::Value::Value ( Int value)

Definition at line 222 of file json_value.cpp.

◆ Value() [3/12]

json::Value::Value ( UInt value)

Definition at line 227 of file json_value.cpp.

◆ Value() [4/12]

json::Value::Value ( double value)

Definition at line 232 of file json_value.cpp.

◆ Value() [5/12]

json::Value::Value ( char const * value)

Definition at line 237 of file json_value.cpp.

◆ Value() [6/12]

json::Value::Value ( xrpl::Number const & value)

Definition at line 242 of file json_value.cpp.

◆ Value() [7/12]

json::Value::Value ( StaticString const & value)

Constructs a value from a static string.

Like other value string constructor but do not duplicate the string for internal storage. The given string must remain alive after the call to this constructor. Example of usage:

json::Value aValue( StaticString("some text") );
Lightweight wrapper to tag static string.
Definition json_value.h:52

Definition at line 258 of file json_value.cpp.

◆ Value() [8/12]

json::Value::Value ( std::string const & value)

Constructs a value from a string.

The characters are copied, so the string need not outlive the call.

Parameters
valueThe characters to copy.

Definition at line 248 of file json_value.cpp.

◆ Value() [9/12]

json::Value::Value ( std::string_view value)

Constructs a value from a string view.

The characters are copied, so the view need not outlive the call and need not end in a null character.

Parameters
valueThe characters to copy.

Definition at line 252 of file json_value.cpp.

◆ Value() [10/12]

json::Value::Value ( bool value)

Definition at line 264 of file json_value.cpp.

◆ Value() [11/12]

json::Value::Value ( Value const & other)

Definition at line 269 of file json_value.cpp.

◆ ~Value()

json::Value::~Value ( )

Definition at line 306 of file json_value.cpp.

◆ Value() [12/12]

json::Value::Value ( Value && other)
noexcept

Definition at line 343 of file json_value.cpp.

Member Function Documentation

◆ operator=() [1/2]

Value & json::Value::operator= ( Value const & other)

Definition at line 336 of file json_value.cpp.

◆ operator=() [2/2]

Value & json::Value::operator= ( Value && other)

Definition at line 351 of file json_value.cpp.

◆ swap()

void json::Value::swap ( Value & other)
noexcept

Swap values.

Definition at line 359 of file json_value.cpp.

◆ type()

ValueType json::Value::type ( ) const
nodiscard

Definition at line 373 of file json_value.cpp.

◆ asCString()

char const * json::Value::asCString ( ) const
nodiscard

Definition at line 495 of file json_value.cpp.

◆ asString()

std::string json::Value::asString ( ) const
nodiscard

Returns the unquoted string value.

Definition at line 502 of file json_value.cpp.

◆ asInt()

Value::Int json::Value::asInt ( ) const
nodiscard

Definition at line 538 of file json_value.cpp.

◆ asUInt()

Value::UInt json::Value::asUInt ( ) const
nodiscard

Definition at line 639 of file json_value.cpp.

◆ asDouble()

double json::Value::asDouble ( ) const
nodiscard

Definition at line 682 of file json_value.cpp.

◆ asBool()

bool json::Value::asBool ( ) const
nodiscard

Definition at line 716 of file json_value.cpp.

◆ asAbsUInt()

UInt json::Value::asAbsUInt ( ) const
nodiscard

Correct absolute value from int or unsigned int.

Definition at line 581 of file json_value.cpp.

◆ isNull()

bool json::Value::isNull ( ) const
nodiscard

isNull() tests to see if this field is null.

Don't use this method to test for emptiness: use empty().

Definition at line 1102 of file json_value.cpp.

◆ isBool()

bool json::Value::isBool ( ) const
nodiscard

Definition at line 1108 of file json_value.cpp.

◆ isInt()

bool json::Value::isInt ( ) const
nodiscard

Definition at line 1114 of file json_value.cpp.

◆ isUInt()

bool json::Value::isUInt ( ) const
nodiscard

Definition at line 1120 of file json_value.cpp.

◆ isIntegral()

bool json::Value::isIntegral ( ) const
nodiscard

Definition at line 1126 of file json_value.cpp.

◆ isDouble()

bool json::Value::isDouble ( ) const
nodiscard

Definition at line 1132 of file json_value.cpp.

◆ isNumeric()

bool json::Value::isNumeric ( ) const
nodiscard

Definition at line 1138 of file json_value.cpp.

◆ isString()

bool json::Value::isString ( ) const
nodiscard

Definition at line 1144 of file json_value.cpp.

◆ isArray()

bool json::Value::isArray ( ) const
nodiscard

Definition at line 1150 of file json_value.cpp.

◆ isArrayOrNull()

bool json::Value::isArrayOrNull ( ) const
nodiscard

Definition at line 1156 of file json_value.cpp.

◆ isObject()

bool json::Value::isObject ( ) const
nodiscard

Definition at line 1162 of file json_value.cpp.

◆ isObjectOrNull()

bool json::Value::isObjectOrNull ( ) const
nodiscard

Definition at line 1168 of file json_value.cpp.

◆ isConvertibleTo()

bool json::Value::isConvertibleTo ( ValueType other) const
nodiscard

Definition at line 752 of file json_value.cpp.

◆ size()

Value::UInt json::Value::size ( ) const
nodiscard

Number of values in array or object.

Definition at line 811 of file json_value.cpp.

◆ operator bool()

json::Value::operator bool ( ) const
explicit

Returns false if this is an empty array, empty object, empty string, or null.

Definition at line 845 of file json_value.cpp.

◆ clear()

void json::Value::clear ( )

Remove all object members and array elements.

Precondition
type() is ValueType::Array, ValueType::Object, or ValueType::Null
Postcondition
type() is unchanged

Definition at line 861 of file json_value.cpp.

◆ operator[]() [1/8]

Value & json::Value::operator[] ( UInt index)

Access an array element (zero based index ).

If the array contains less than index element, then null value are inserted in the array so that its size is index+1. (You may need to say 'value[0u]' to get your compiler to distinguish this from the operator[] which takes a string.)

Definition at line 880 of file json_value.cpp.

◆ operator[]() [2/8]

Value const & json::Value::operator[] ( UInt index) const

Access an array element (zero based index ) (You may need to say 'value[0u]' to get your compiler to distinguish this from the operator[] which takes a string.).

Definition at line 901 of file json_value.cpp.

◆ get() [1/3]

Value json::Value::get ( UInt index,
Value const & defaultValue ) const
nodiscard

If the array contains at least index+1 elements, returns the element value, otherwise returns defaultValue.

Definition at line 951 of file json_value.cpp.

◆ isValidIndex()

bool json::Value::isValidIndex ( UInt index) const
nodiscard

Return true if index < size().

Definition at line 958 of file json_value.cpp.

◆ append() [1/2]

Value & json::Value::append ( Value const & value)

Append value to array at the end.

Equivalent to jsonvalue[jsonvalue.size()] = value;

Definition at line 1007 of file json_value.cpp.

◆ append() [2/2]

Value & json::Value::append ( Value && value)

Definition at line 1013 of file json_value.cpp.

◆ operator[]() [3/8]

Value & json::Value::operator[] ( char const * key)

Access an object value by name, create a null member if it does not exist.

Definition at line 920 of file json_value.cpp.

◆ operator[]() [4/8]

Value const & json::Value::operator[] ( char const * key) const

Access an object value by name, returns null if there is no member with that name.

Definition at line 964 of file json_value.cpp.

◆ operator[]() [5/8]

Value & json::Value::operator[] ( std::string const & key)

Access an object value by name, create a null member if it does not exist.

Definition at line 983 of file json_value.cpp.

◆ operator[]() [6/8]

Value const & json::Value::operator[] ( std::string const & key) const

Access an object value by name, returns null if there is no member with that name.

Definition at line 989 of file json_value.cpp.

◆ operator[]() [7/8]

Value & json::Value::operator[] ( StaticString const & key)

Access an object value by name, create a null member if it does not exist.

If the object as no entry for that name, then the member name used to store the new entry is not duplicated. Example of use:

json::Value object;
static const StaticString code("code");
object[code] = 1234;

Definition at line 995 of file json_value.cpp.

◆ operator[]() [8/8]

Value const & json::Value::operator[] ( StaticString const & key) const

Definition at line 1001 of file json_value.cpp.

◆ get() [2/3]

Value json::Value::get ( char const * key,
Value const & defaultValue ) const

Return the member named key if it exist, defaultValue otherwise.

Definition at line 1019 of file json_value.cpp.

◆ get() [3/3]

Value json::Value::get ( std::string const & key,
Value const & defaultValue ) const
nodiscard

Return the member named key if it exist, defaultValue otherwise.

Definition at line 1026 of file json_value.cpp.

◆ removeMember() [1/2]

Value json::Value::removeMember ( char const * key)

Remove and return the named member.

Do nothing if it did not exist.

Returns
the removed Value, or null.
Precondition
type() is ValueType::Object or ValueType::Null
Postcondition
type() is unchanged

Definition at line 1032 of file json_value.cpp.

◆ removeMember() [2/2]

Value json::Value::removeMember ( std::string const & key)

Same as removeMember(const char*).

Definition at line 1053 of file json_value.cpp.

◆ isMember() [1/3]

bool json::Value::isMember ( char const * key) const

Return true if the object has a member named key.

Definition at line 1059 of file json_value.cpp.

◆ isMember() [2/3]

bool json::Value::isMember ( std::string const & key) const
nodiscard

Return true if the object has a member named key.

Definition at line 1069 of file json_value.cpp.

◆ isMember() [3/3]

bool json::Value::isMember ( StaticString const & key) const
nodiscard

Return true if the object has a member named key.

Definition at line 1075 of file json_value.cpp.

◆ getMemberNames()

Value::Members json::Value::getMemberNames ( ) const
nodiscard

Return a list of the member names.

If null, return an empty list.

Precondition
type() is ValueType::Object or ValueType::Null
Postcondition
if type() was ValueType::Null, it remains ValueType::Null

Definition at line 1081 of file json_value.cpp.

◆ toStyledString()

std::string json::Value::toStyledString ( ) const
nodiscard

Definition at line 1174 of file json_value.cpp.

◆ begin() [1/2]

Value::const_iterator json::Value::begin ( ) const
nodiscard

Definition at line 1181 of file json_value.cpp.

◆ end() [1/2]

Value::const_iterator json::Value::end ( ) const
nodiscard

Definition at line 1199 of file json_value.cpp.

◆ begin() [2/2]

Value::iterator json::Value::begin ( )

Definition at line 1217 of file json_value.cpp.

◆ end() [2/2]

Value::iterator json::Value::end ( )

Definition at line 1234 of file json_value.cpp.

◆ resolveReference()

Value & json::Value::resolveReference ( char const * key,
bool isStatic )
private

Definition at line 926 of file json_value.cpp.

◆ ValueIteratorBase

friend class ValueIteratorBase
friend

Definition at line 122 of file json_value.h.

◆ operator==

bool operator== ( Value const & x,
Value const & y )
friend

Definition at line 447 of file json_value.cpp.

◆ operator<

bool operator< ( Value const & x,
Value const & y )
friend

Definition at line 391 of file json_value.cpp.

Member Data Documentation

◆ kNull

Value const json::Value::kNull
static

Definition at line 132 of file json_value.h.

◆ kMinInt

Int json::Value::kMinInt = std::numeric_limits<Int>::min()
staticconstexpr

Definition at line 133 of file json_value.h.

◆ kMaxInt

Int json::Value::kMaxInt = std::numeric_limits<Int>::max()
staticconstexpr

Definition at line 134 of file json_value.h.

◆ kMaxUInt

UInt json::Value::kMaxUInt = std::numeric_limits<UInt>::max()
staticconstexpr

Definition at line 135 of file json_value.h.

◆ value_

union json::Value::ValueHolder json::Value::value_
private

◆ type_

ValueType json::Value::type_
private

Definition at line 486 of file json_value.h.

◆ allocated_

int json::Value::allocated_
private

Definition at line 487 of file json_value.h.