Free Electron
Public Types | Public Member Functions | Static 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 <value.h>

Public Types

using Members = std::vector< String >
 
using iterator = ValueIterator
 
using const_iterator = ValueConstIterator
 
using UInt = Json::UInt
 
using Int = Json::Int
 
using UInt64 = Json::UInt64
 
using Int64 = Json::Int64
 
using LargestInt = Json::LargestInt
 
using LargestUInt = Json::LargestUInt
 
using ArrayIndex = Json::ArrayIndex
 
using value_type = std::string
 
typedef std::map< CZString, ValueObjectValues
 

Public Member Functions

 Value (ValueType type=nullValue)
 Create a default Value of the given type. More...
 
 Value (Int value)
 
 Value (UInt value)
 
 Value (Int64 value)
 
 Value (UInt64 value)
 
 Value (double value)
 
 Value (const char *value)
 Copy til first 0. (NULL causes to seg-fault.) More...
 
 Value (const char *begin, const char *end)
 Copy all, incl zeroes. More...
 
 Value (const StaticString &value)
 Constructs a value from a static string. More...
 
 Value (const String &value)
 
 Value (bool value)
 
 Value (std::nullptr_t ptr)=delete
 
 Value (const Value &other)
 
 Value (Value &&other) noexcept
 
Valueoperator= (const Value &other)
 
Valueoperator= (Value &&other) noexcept
 
void swap (Value &other)
 Swap everything. More...
 
void swapPayload (Value &other)
 Swap values but leave comments and source offsets in place. More...
 
void copy (const Value &other)
 copy everything. More...
 
void copyPayload (const Value &other)
 copy values but leave comments and source offsets in place. More...
 
ValueType type () const
 
bool operator< (const Value &other) const
 Compare payload only, not comments etc. More...
 
bool operator<= (const Value &other) const
 
bool operator>= (const Value &other) const
 
bool operator> (const Value &other) const
 
bool operator== (const Value &other) const
 
bool operator!= (const Value &other) const
 
int compare (const Value &other) const
 
const char * asCString () const
 Embedded zeroes could cause you trouble! More...
 
String asString () const
 Embedded zeroes are possible. More...
 
bool getString (char const **begin, char const **end) const
 Get raw char* of string-value. More...
 
Int asInt () const
 
UInt asUInt () const
 
Int64 asInt64 () const
 
UInt64 asUInt64 () const
 
LargestInt asLargestInt () const
 
LargestUInt asLargestUInt () const
 
float asFloat () const
 
double asDouble () const
 
bool asBool () const
 
bool isNull () const
 
bool isBool () const
 
bool isInt () const
 
bool isInt64 () const
 
bool isUInt () const
 
bool isUInt64 () const
 
bool isIntegral () const
 
bool isDouble () const
 
bool isNumeric () const
 
bool isString () const
 
bool isArray () const
 
bool isObject () const
 
template<typename T >
as () const JSONCPP_TEMPLATE_DELETE
 The as<T> and is<T> member function templates and specializations. More...
 
template<typename T >
bool is () const JSONCPP_TEMPLATE_DELETE
 
bool isConvertibleTo (ValueType other) const
 
ArrayIndex size () const
 Number of values in array or object. More...
 
bool empty () const
 Return true if empty array, empty object, or null; otherwise, false. More...
 
 operator bool () const
 Return !isNull() More...
 
void clear ()
 Remove all object members and array elements. More...
 
void resize (ArrayIndex newSize)
 Resize the array to newSize elements. More...
 
Value get (ArrayIndex index, const Value &defaultValue) const
 If the array contains at least index+1 elements, returns the element value, otherwise returns defaultValue. More...
 
bool isValidIndex (ArrayIndex index) const
 Return true if index < size(). More...
 
Valueappend (const Value &value)
 Append value to array at the end. More...
 
Valueappend (Value &&value)
 
bool insert (ArrayIndex index, const Value &newValue)
 Insert value in array at specific index. More...
 
bool insert (ArrayIndex index, Value &&newValue)
 
Valueoperator[] (const char *key)
 Access an object value by name, create a null member if it does not exist. More...
 
const Valueoperator[] (const char *key) const
 Access an object value by name, returns null if there is no member with that name. More...
 
Valueoperator[] (const String &key)
 Access an object value by name, create a null member if it does not exist. More...
 
const Valueoperator[] (const String &key) const
 Access an object value by name, returns null if there is no member with that name. More...
 
Valueoperator[] (const StaticString &key)
 Access an object value by name, create a null member if it does not exist. More...
 
Value get (const char *key, const Value &defaultValue) const
 Return the member named key if it exist, defaultValue otherwise. More...
 
Value get (const char *begin, const char *end, const Value &defaultValue) const
 Return the member named key if it exist, defaultValue otherwise. More...
 
Value get (const String &key, const Value &defaultValue) const
 Return the member named key if it exist, defaultValue otherwise. More...
 
Value const * find (char const *begin, char const *end) const
 Most general and efficient version of isMember()const, get()const, and operator[]const. More...
 
Valuedemand (char const *begin, char const *end)
 Most general and efficient version of object-mutators. More...
 
void removeMember (const char *key)
 Remove and return the named member. More...
 
void removeMember (const String &key)
 Same as removeMember(const char*) More...
 
bool removeMember (const char *key, Value *removed)
 Same as removeMember(const char* begin, const char* end, Value* removed), but 'key' is null-terminated. More...
 
bool removeMember (String const &key, Value *removed)
 Remove the named map member. More...
 
bool removeMember (const char *begin, const char *end, Value *removed)
 Same as removeMember(String const& key, Value* removed) More...
 
bool removeIndex (ArrayIndex index, Value *removed)
 Remove the indexed array element. More...
 
bool isMember (const char *key) const
 Return true if the object has a member named key. More...
 
bool isMember (const String &key) const
 Return true if the object has a member named key. More...
 
bool isMember (const char *begin, const char *end) const
 Same as isMember(String const& key)const. More...
 
Members getMemberNames () const
 Return a list of the member names. More...
 
void setComment (const char *comment, CommentPlacement placement)
 
void setComment (const char *comment, size_t len, CommentPlacement placement)
 Comments must be //... or /* ... */. More...
 
void setComment (String comment, CommentPlacement placement)
 Comments must be //... or /* ... */. More...
 
bool hasComment (CommentPlacement placement) const
 
String getComment (CommentPlacement placement) const
 Include delimiters and embedded newlines. More...
 
String toStyledString () const
 
const_iterator begin () const
 
const_iterator end () const
 
iterator begin ()
 
iterator end ()
 
void setOffsetStart (ptrdiff_t start)
 
void setOffsetLimit (ptrdiff_t limit)
 
ptrdiff_t getOffsetStart () const
 
ptrdiff_t getOffsetLimit () const
 
template<>
bool as () const
 
template<>
bool is () const
 
template<>
Int as () const
 
template<>
bool is () const
 
template<>
UInt as () const
 
template<>
bool is () const
 
template<>
Int64 as () const
 
template<>
bool is () const
 
template<>
UInt64 as () const
 
template<>
bool is () const
 
template<>
double as () const
 
template<>
bool is () const
 
template<>
String as () const
 
template<>
bool is () const
 
template<>
float as () const
 These as specializations are type conversions, and do not have a corresponding is. More...
 
template<>
const char * as () const
 
Valueoperator[] (ArrayIndex index)
 
Valueoperator[] (int index)
 
const Valueoperator[] (ArrayIndex index) const
 
const Valueoperator[] (int index) const
 

Static Public Member Functions

static Value const & nullSingleton ()
 

Static Public Attributes

static const Valuenull
 
static const ValuenullRef
 
static constexpr LargestInt minLargestInt
 Minimum signed integer value that can be stored in a Json::Value. More...
 
static constexpr LargestInt maxLargestInt = LargestInt(LargestUInt(-1) / 2)
 Maximum signed integer value that can be stored in a Json::Value. More...
 
static constexpr LargestUInt maxLargestUInt = LargestUInt(-1)
 Maximum unsigned integer value that can be stored in a Json::Value. More...
 
static constexpr Int minInt = Int(~(UInt(-1) / 2))
 Minimum signed int value that can be stored in a Json::Value. More...
 
static constexpr Int maxInt = Int(UInt(-1) / 2)
 Maximum signed int value that can be stored in a Json::Value. More...
 
static constexpr UInt maxUInt = UInt(-1)
 Maximum unsigned int value that can be stored in a Json::Value. More...
 
static constexpr Int64 minInt64 = Int64(~(UInt64(-1) / 2))
 Minimum signed 64 bits int value that can be stored in a Json::Value. More...
 
static constexpr Int64 maxInt64 = Int64(UInt64(-1) / 2)
 Maximum signed 64 bits int value that can be stored in a Json::Value. More...
 
static constexpr UInt64 maxUInt64 = UInt64(-1)
 Maximum unsigned 64 bits int value that can be stored in a Json::Value. More...
 
static constexpr UInt defaultRealPrecision = 17
 Default precision for real value for string representation. More...
 
static constexpr double maxUInt64AsDouble = 18446744073709551615.0
 

Private Member Functions

void setType (ValueType v)
 
bool isAllocated () const
 
void setIsAllocated (bool v)
 
void initBasic (ValueType type, bool allocated=false)
 
void dupPayload (const Value &other)
 
void releasePayload ()
 
void dupMeta (const Value &other)
 
ValueresolveReference (const char *key)
 
ValueresolveReference (const char *key, const char *end)
 

Private Attributes

union Json::Value::ValueHolder value_
 
struct {
   unsigned int   value_type_: 8
 
   unsigned int   allocated_: 1
 
bits_
 
Comments comments_
 
ptrdiff_t start_
 
ptrdiff_t limit_
 

Friends

class ValueIteratorBase
 

Detailed Description

Represents a JSON value.

This class is a discriminated union wrapper that can represents 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 resized and initialized with nullValue. resize() can be used to enlarge or truncate an arrayValue.

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

It is possible to iterate over the list of member keys of an object using the getMemberNames() method.

Note
Value string-length fit in size_t, but keys must be < 2^30. (The reason is an implementation detail.) A #CharReader will raise an exception if a bound is exceeded to avoid security holes in your app, but the Value API does not check bounds. That is the responsibility of the caller.

Constructor & Destructor Documentation

◆ Value() [1/4]

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); // {}

◆ Value() [2/4]

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

Copy til first 0. (NULL causes to seg-fault.)

◆ Value() [3/4]

Json::Value::Value ( const char *  begin,
const char *  end 
)

Copy all, incl zeroes.

◆ Value() [4/4]

Json::Value::Value ( const StaticString 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.

Note
This works only for null-terminated strings. (We cannot change the size of this class, so we have nowhere to store the length, which might be computed later for various operations.)

Example of usage:

static StaticString foo("some text");
Json::Value aValue(foo);

Member Function Documentation

◆ append()

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

Append value to array at the end.

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

◆ as() [1/2]

template<typename T >
T Json::Value::as ( ) const

The as<T> and is<T> member function templates and specializations.

◆ as() [2/2]

template<>
float Json::Value::as ( void  ) const
inline

These as specializations are type conversions, and do not have a corresponding is.

◆ asCString()

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

Embedded zeroes could cause you trouble!

◆ asString()

String Json::Value::asString ( ) const

Embedded zeroes are possible.

◆ clear()

void Json::Value::clear ( )

Remove all object members and array elements.

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

◆ copy()

void Json::Value::copy ( const Value other)

copy everything.

◆ copyPayload()

void Json::Value::copyPayload ( const Value other)

copy values but leave comments and source offsets in place.

◆ demand()

Value* Json::Value::demand ( char const *  begin,
char const *  end 
)

Most general and efficient version of object-mutators.

Note
As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
Returns
non-zero, but JSON_ASSERT if this is neither object nor nullValue.

◆ empty()

bool Json::Value::empty ( ) const

Return true if empty array, empty object, or null; otherwise, false.

◆ find()

Value const* Json::Value::find ( char const *  begin,
char const *  end 
) const

Most general and efficient version of isMember()const, get()const, and operator[]const.

Note
As stated elsewhere, behavior is undefined if (end-begin) >= 2^30

◆ get() [1/4]

Value Json::Value::get ( ArrayIndex  index,
const Value defaultValue 
) const

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

◆ get() [2/4]

Value Json::Value::get ( const char *  key,
const Value defaultValue 
) const

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

Note
deep copy

◆ get() [3/4]

Value Json::Value::get ( const char *  begin,
const char *  end,
const Value defaultValue 
) const

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

Note
deep copy
key may contain embedded nulls.

◆ get() [4/4]

Value Json::Value::get ( const String &  key,
const Value defaultValue 
) const

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

Note
deep copy
Parameters
keymay contain embedded nulls.

◆ getComment()

String Json::Value::getComment ( CommentPlacement  placement) const

Include delimiters and embedded newlines.

◆ getMemberNames()

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

◆ getString()

bool Json::Value::getString ( char const **  begin,
char const **  end 
) const

Get raw char* of string-value.

Returns
false if !string. (Seg-fault if str or end are NULL.)

◆ insert()

bool Json::Value::insert ( ArrayIndex  index,
const Value newValue 
)

Insert value in array at specific index.

◆ isMember() [1/3]

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

Return true if the object has a member named key.

Note
'key' must be null-terminated.

◆ isMember() [2/3]

bool Json::Value::isMember ( const String &  key) const

Return true if the object has a member named key.

Parameters
keymay contain embedded nulls.

◆ isMember() [3/3]

bool Json::Value::isMember ( const char *  begin,
const char *  end 
) const

◆ isValidIndex()

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

Return true if index < size().

◆ operator bool()

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

Return !isNull()

◆ operator<()

bool Json::Value::operator< ( const Value other) const

Compare payload only, not comments etc.

◆ operator=()

Value& Json::Value::operator= ( const Value other)
Note
Overwrite existing comments. To preserve comments, use swapPayload().

◆ operator[]() [1/9]

Value& Json::Value::operator[] ( ArrayIndex  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.)

◆ operator[]() [2/9]

Value& Json::Value::operator[] ( int  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.)

◆ operator[]() [3/9]

const Value& Json::Value::operator[] ( ArrayIndex  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.)

◆ operator[]() [4/9]

const Value& Json::Value::operator[] ( int  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.)

◆ operator[]() [5/9]

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

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

Note
Because of our implementation, keys are limited to 2^30 -1 chars. Exceeding that will cause an exception.

◆ operator[]() [6/9]

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

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

◆ operator[]() [7/9]

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

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

Parameters
keymay contain embedded nulls.

◆ operator[]() [8/9]

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

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

Parameters
keymay contain embedded nulls.

◆ operator[]() [9/9]

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

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

If the object has 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;

◆ removeIndex()

bool Json::Value::removeIndex ( ArrayIndex  index,
Value removed 
)

Remove the indexed array element.

O(n) expensive operations. Update 'removed' iff removed.

Returns
true if removed (no exceptions)

◆ removeMember() [1/5]

void Json::Value::removeMember ( const char *  key)

Remove and return the named member.

Do nothing if it did not exist.

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

◆ removeMember() [2/5]

void Json::Value::removeMember ( const String &  key)

Same as removeMember(const char*)

Parameters
keymay contain embedded nulls.

◆ removeMember() [3/5]

bool Json::Value::removeMember ( const char *  key,
Value removed 
)

◆ removeMember() [4/5]

bool Json::Value::removeMember ( String const &  key,
Value removed 
)

Remove the named map member.

Update 'removed' iff removed.

Parameters
keymay contain embedded nulls.
Returns
true iff removed (no exceptions)

◆ removeMember() [5/5]

bool Json::Value::removeMember ( const char *  begin,
const char *  end,
Value removed 
)

◆ resize()

void Json::Value::resize ( ArrayIndex  newSize)

Resize the array to newSize elements.

New elements are initialized to null. May only be called on nullValue or arrayValue.

Precondition
type() is arrayValue or nullValue
Postcondition
type() is arrayValue

◆ setComment() [1/3]

void Json::Value::setComment ( const char *  comment,
CommentPlacement  placement 
)
inline
Deprecated:
Always pass len.

◆ setComment() [2/3]

void Json::Value::setComment ( const char *  comment,
size_t  len,
CommentPlacement  placement 
)
inline

Comments must be //... or /* ... */.

◆ setComment() [3/3]

void Json::Value::setComment ( String  comment,
CommentPlacement  placement 
)

Comments must be //... or /* ... */.

◆ size()

ArrayIndex Json::Value::size ( ) const

Number of values in array or object.

◆ swap()

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

Swap everything.

◆ swapPayload()

void Json::Value::swapPayload ( Value other)

Swap values but leave comments and source offsets in place.

Member Data Documentation

◆ defaultRealPrecision

constexpr UInt Json::Value::defaultRealPrecision = 17
static

Default precision for real value for string representation.

◆ maxInt

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

Maximum signed int value that can be stored in a Json::Value.

◆ maxInt64

constexpr Int64 Json::Value::maxInt64 = Int64(UInt64(-1) / 2)
static

Maximum signed 64 bits int value that can be stored in a Json::Value.

◆ maxLargestInt

constexpr LargestInt Json::Value::maxLargestInt = LargestInt(LargestUInt(-1) / 2)
static

Maximum signed integer value that can be stored in a Json::Value.

◆ maxLargestUInt

constexpr LargestUInt Json::Value::maxLargestUInt = LargestUInt(-1)
static

Maximum unsigned integer value that can be stored in a Json::Value.

◆ maxUInt

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

Maximum unsigned int value that can be stored in a Json::Value.

◆ maxUInt64

constexpr UInt64 Json::Value::maxUInt64 = UInt64(-1)
static

Maximum unsigned 64 bits int value that can be stored in a Json::Value.

◆ minInt

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

Minimum signed int value that can be stored in a Json::Value.

◆ minInt64

constexpr Int64 Json::Value::minInt64 = Int64(~(UInt64(-1) / 2))
static

Minimum signed 64 bits int value that can be stored in a Json::Value.

◆ minLargestInt

constexpr LargestInt Json::Value::minLargestInt
static
Initial value:
=
LargestInt(~(LargestUInt(-1) / 2))

Minimum signed integer value that can be stored in a Json::Value.


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