rippled
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Static Public Attributes | Private Member Functions | Private Attributes | Friends | List of all members
Json::Value Class Reference

Represents a JSON value. More...

#include <json_value.h>

Collaboration diagram for Json::Value:
Collaboration graph
[legend]

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=nullValue)
 Create a default Value of the given type. More...
 
 Value (Int value)
 
 Value (UInt value)
 
 Value (double value)
 
 Value (char const *value)
 
 Value (ripple::Number const &value)
 
 Value (StaticString const &value)
 Constructs a value from a static string. More...
 
 Value (std::string const &value)
 
 Value (bool value)
 
 Value (Value const &other)
 
 ~Value ()
 
Valueoperator= (Value const &other)
 
Valueoperator= (Value &&other)
 
 Value (Value &&other) noexcept
 
void swap (Value &other) noexcept
 Swap values. More...
 
ValueType type () const
 
char const * asCString () const
 
std::string asString () const
 Returns the unquoted string value. More...
 
Int asInt () const
 
UInt asUInt () const
 
double asDouble () const
 
bool asBool () const
 
bool isNull () const
 isNull() tests to see if this field is null. More...
 
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. More...
 
 operator bool () const
 Returns false if this is an empty array, empty object, empty string, or null. More...
 
void clear ()
 Remove all object members and array elements. More...
 
Valueoperator[] (UInt index)
 Access an array element (zero based index ). More...
 
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.) More...
 
Value get (UInt index, Value const &defaultValue) const
 If the array contains at least index+1 elements, returns the element value, otherwise returns defaultValue. More...
 
bool isValidIndex (UInt index) const
 Return true if index < size(). More...
 
Valueappend (Value const &value)
 Append value to array at the end. More...
 
Valueappend (Value &&value)
 
Valueoperator[] (char const *key)
 Access an object value by name, create a null member if it does not exist. More...
 
Value const & operator[] (char const *key) const
 Access an object value by name, returns null if there is no member with that name. More...
 
Valueoperator[] (std::string const &key)
 Access an object value by name, create a null member if it does not exist. More...
 
Value const & operator[] (std::string const &key) const
 Access an object value by name, returns null if there is no member with that name. More...
 
Valueoperator[] (StaticString const &key)
 Access an object value by name, create a null member if it does not exist. More...
 
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. More...
 
Value get (std::string const &key, Value const &defaultValue) const
 Return the member named key if it exist, defaultValue otherwise. More...
 
Value removeMember (char const *key)
 Remove and return the named member. More...
 
Value removeMember (std::string const &key)
 Same as removeMember(const char*) More...
 
bool isMember (char const *key) const
 Return true if the object has a member named key. More...
 
bool isMember (std::string const &key) const
 Return true if the object has a member named key. More...
 
Members getMemberNames () const
 Return a list of the member names. More...
 
std::string toStyledString () const
 
const_iterator begin () const
 
const_iterator end () const
 
iterator begin ()
 
iterator end ()
 

Static Public Attributes

static Value const null
 
static Int const minInt = Int(~(UInt(-1) / 2))
 
static Int const maxInt = Int(UInt(-1) / 2)
 
static UInt const maxUInt = UInt(-1)
 

Private Member Functions

ValueresolveReference (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 &, Value const &)
 
bool operator< (Value const &, Value const &)
 

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 objectValue or arrayValue can be accessed using operator[]() methods. Non const methods will automatically create the a nullValue element if it does not exist. The sequence of an arrayValue will be automatically resize and initialized with nullValue. resize() can be used to enlarge or truncate an arrayValue.

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 objectValue values using the getMemberNames() method.

Definition at line 149 of file json_value.h.

Member Typedef Documentation

◆ Members

Definition at line 154 of file json_value.h.

◆ iterator

Definition at line 155 of file json_value.h.

◆ const_iterator

Definition at line 156 of file json_value.h.

◆ UInt

Definition at line 157 of file json_value.h.

◆ Int

Definition at line 158 of file json_value.h.

◆ ArrayIndex

Definition at line 159 of file json_value.h.

◆ ObjectValues

Definition at line 198 of file json_value.h.

Constructor & Destructor Documentation

◆ Value() [1/11]

Json::Value::Value ( ValueType  type = nullValue)

Create a default Value of the given type.

This is a very useful constructor. To create an empty array, pass arrayValue. To create an empty object, pass objectValue. 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
Json::Value arr_value(Json::arrayValue); // []
Json::Value obj_value(Json::objectValue); // {}
Represents a JSON value.
Definition: json_value.h:150
@ arrayValue
array value (ordered list)
Definition: json_value.h:45
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:46

Definition at line 186 of file json_value.cpp.

◆ Value() [2/11]

Json::Value::Value ( Int  value)

Definition at line 220 of file json_value.cpp.

◆ Value() [3/11]

Json::Value::Value ( UInt  value)

Definition at line 225 of file json_value.cpp.

◆ Value() [4/11]

Json::Value::Value ( double  value)

Definition at line 230 of file json_value.cpp.

◆ Value() [5/11]

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

Definition at line 235 of file json_value.cpp.

◆ Value() [6/11]

Json::Value::Value ( ripple::Number const &  value)

Definition at line 240 of file json_value.cpp.

◆ Value() [7/11]

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:64

Definition at line 253 of file json_value.cpp.

◆ Value() [8/11]

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

Definition at line 247 of file json_value.cpp.

◆ Value() [9/11]

Json::Value::Value ( bool  value)

Definition at line 258 of file json_value.cpp.

◆ Value() [10/11]

Json::Value::Value ( Value const &  other)

Definition at line 263 of file json_value.cpp.

◆ ~Value()

Json::Value::~Value ( )

Definition at line 297 of file json_value.cpp.

◆ Value() [11/11]

Json::Value::Value ( Value &&  other)
noexcept

Definition at line 333 of file json_value.cpp.

Member Function Documentation

◆ operator=() [1/2]

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

Definition at line 326 of file json_value.cpp.

◆ operator=() [2/2]

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

Definition at line 341 of file json_value.cpp.

◆ swap()

void Json::Value::swap ( Value other)
noexcept

Swap values.

Definition at line 349 of file json_value.cpp.

◆ type()

ValueType Json::Value::type ( ) const

Definition at line 363 of file json_value.cpp.

◆ asCString()

char const * Json::Value::asCString ( ) const

Definition at line 475 of file json_value.cpp.

◆ asString()

std::string Json::Value::asString ( ) const

Returns the unquoted string value.

Definition at line 482 of file json_value.cpp.

◆ asInt()

Value::Int Json::Value::asInt ( ) const

Definition at line 516 of file json_value.cpp.

◆ asUInt()

Value::UInt Json::Value::asUInt ( ) const

Definition at line 558 of file json_value.cpp.

◆ asDouble()

double Json::Value::asDouble ( ) const

Definition at line 600 of file json_value.cpp.

◆ asBool()

bool Json::Value::asBool ( ) const

Definition at line 632 of file json_value.cpp.

◆ isNull()

bool Json::Value::isNull ( ) const

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

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

Definition at line 999 of file json_value.cpp.

◆ isBool()

bool Json::Value::isBool ( ) const

Definition at line 1005 of file json_value.cpp.

◆ isInt()

bool Json::Value::isInt ( ) const

Definition at line 1011 of file json_value.cpp.

◆ isUInt()

bool Json::Value::isUInt ( ) const

Definition at line 1017 of file json_value.cpp.

◆ isIntegral()

bool Json::Value::isIntegral ( ) const

Definition at line 1023 of file json_value.cpp.

◆ isDouble()

bool Json::Value::isDouble ( ) const

Definition at line 1029 of file json_value.cpp.

◆ isNumeric()

bool Json::Value::isNumeric ( ) const

Definition at line 1035 of file json_value.cpp.

◆ isString()

bool Json::Value::isString ( ) const

Definition at line 1041 of file json_value.cpp.

◆ isArray()

bool Json::Value::isArray ( ) const

Definition at line 1047 of file json_value.cpp.

◆ isArrayOrNull()

bool Json::Value::isArrayOrNull ( ) const

Definition at line 1053 of file json_value.cpp.

◆ isObject()

bool Json::Value::isObject ( ) const

Definition at line 1059 of file json_value.cpp.

◆ isObjectOrNull()

bool Json::Value::isObjectOrNull ( ) const

Definition at line 1065 of file json_value.cpp.

◆ isConvertibleTo()

bool Json::Value::isConvertibleTo ( ValueType  other) const

Definition at line 664 of file json_value.cpp.

◆ size()

Value::UInt Json::Value::size ( ) const

Number of values in array or object.

Definition at line 719 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 751 of file json_value.cpp.

◆ clear()

void Json::Value::clear ( )

Remove all object members and array elements.

Precondition
type() is arrayValue, objectValue, or nullValue
Postcondition
type() is unchanged

Definition at line 766 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 785 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 806 of file json_value.cpp.

◆ get() [1/3]

Value Json::Value::get ( UInt  index,
Value const &  defaultValue 
) const

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

Definition at line 854 of file json_value.cpp.

◆ isValidIndex()

bool Json::Value::isValidIndex ( UInt  index) const

Return true if index < size().

Definition at line 861 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 910 of file json_value.cpp.

◆ append() [2/2]

Value & Json::Value::append ( Value &&  value)

Definition at line 916 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 825 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 867 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 886 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 892 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 898 of file json_value.cpp.

◆ operator[]() [8/8]

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

Definition at line 904 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 922 of file json_value.cpp.

◆ get() [3/3]

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

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

Definition at line 929 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 objectValue or nullValue
Postcondition
type() is unchanged

Definition at line 935 of file json_value.cpp.

◆ removeMember() [2/2]

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

Same as removeMember(const char*)

Definition at line 956 of file json_value.cpp.

◆ isMember() [1/2]

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

Return true if the object has a member named key.

Definition at line 962 of file json_value.cpp.

◆ isMember() [2/2]

bool Json::Value::isMember ( std::string const &  key) const

Return true if the object has a member named key.

Definition at line 972 of file json_value.cpp.

◆ getMemberNames()

Value::Members Json::Value::getMemberNames ( ) const

Return a list of the member names.

If null, return an empty list.

Precondition
type() is objectValue or nullValue
Postcondition
if type() was nullValue, it remains nullValue

Definition at line 978 of file json_value.cpp.

◆ toStyledString()

std::string Json::Value::toStyledString ( ) const

Definition at line 1071 of file json_value.cpp.

◆ begin() [1/2]

Value::const_iterator Json::Value::begin ( ) const

Definition at line 1078 of file json_value.cpp.

◆ end() [1/2]

Value::const_iterator Json::Value::end ( ) const

Definition at line 1096 of file json_value.cpp.

◆ begin() [2/2]

Value::iterator Json::Value::begin ( )

Definition at line 1114 of file json_value.cpp.

◆ end() [2/2]

Value::iterator Json::Value::end ( )

Definition at line 1131 of file json_value.cpp.

◆ resolveReference()

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

Definition at line 831 of file json_value.cpp.

Friends And Related Function Documentation

◆ ValueIteratorBase

friend class ValueIteratorBase
friend

Definition at line 151 of file json_value.h.

◆ operator==

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

Definition at line 429 of file json_value.cpp.

◆ operator<

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

Definition at line 379 of file json_value.cpp.

Member Data Documentation

◆ null

Value const Json::Value::null
static

Definition at line 161 of file json_value.h.

◆ minInt

Int const Json::Value::minInt = Int(~(UInt(-1) / 2))
static

Definition at line 162 of file json_value.h.

◆ maxInt

Int const Json::Value::maxInt = Int(UInt(-1) / 2)
static

Definition at line 163 of file json_value.h.

◆ maxUInt

UInt const Json::Value::maxUInt = UInt(-1)
static

Definition at line 164 of file json_value.h.

◆ value_

union Json::Value::ValueHolder Json::Value::value_
private

◆ type_

ValueType Json::Value::type_
private

Definition at line 440 of file json_value.h.

◆ allocated_

int Json::Value::allocated_
private

Definition at line 441 of file json_value.h.