TSDuck Version 3.15-955 (TSDuck - The MPEG Transport Stream Toolkit)
ts::json::Value Class Referenceabstract

Abstract base class of a JSON value. More...

Inheritance diagram for ts::json::Value:

Public Member Functions

 Value ()=default
 Default constructor.
 
virtual ~Value ()
 Virtual destructor.
 
virtual void add (const UString &name, const ValuePtr &value)
 Add a field into an object. More...
 
virtual const Valueat (size_t index) const
 Get an element of an array. More...
 
virtual void clear ()
 Clear the content of the value;.
 
virtual void erase (size_t index, size_t count=1)
 Erase elements from an array. More...
 
virtual ValuePtr extract (const UString &name)
 Extract a field from an object. More...
 
virtual ValuePtr extractAt (size_t index)
 Extract an element from an array. More...
 
virtual void getNames (UStringList &names) const
 Get all field names in an object. More...
 
virtual bool isArray () const
 Check if this instance a is JSON array. More...
 
virtual bool isFalse () const
 Check if this instance a is JSON false literal. More...
 
virtual bool isNull () const
 Check if this instance a is JSON null literal. More...
 
virtual bool isNumber () const
 Check if this instance a is JSON number. More...
 
virtual bool isObject () const
 Check if this instance a is JSON object. More...
 
virtual bool isString () const
 Check if this instance a is JSON string. More...
 
virtual bool isTrue () const
 Check if this instance a is JSON true literal. More...
 
virtual void print (TextFormatter &output) const =0
 Format the value as JSON text. More...
 
virtual UString printed (size_t indent=2, Report &report=(*ts::NullReport::Instance())) const
 Format the value as JSON text. More...
 
virtual void remove (const UString &name)
 Remove a field from an object. More...
 
virtual size_t set (const ValuePtr &value, size_t index=std::numeric_limits< size_t >::max())
 Set an element of an array. More...
 
virtual size_t size () const
 Get the "size" of the value. More...
 
virtual bool toBoolean (bool defaultValue=false) const
 Convert this instance to a boolean. More...
 
virtual int64_t toInteger (int64_t defaultValue=0) const
 Convert this instance to an integer. More...
 
virtual UString toString (const UString &defaultValue=UString()) const
 Convert this instance to a string. More...
 
virtual Type type () const =0
 Get the JSON value type. More...
 
virtual const Valuevalue (const UString &name) const
 Get the value of an object field. More...
 

Detailed Description

Abstract base class of a JSON value.

A JSON document is composed of one value (usually of type "object" or "array"). JSON is defined by RFC 8259.

This implementation is simple and basic.

Fetching a non-existent element, field or whatever always fails silently by returning a reference to a "null" value. Thus, it is possible to access a complex chain of references without caring about intermediate errors. At the end, a "null" value will be returned.

Floating-point values are not yet implemented. Reading a number always give a null object.

See also
http://json.org/
https://tools.ietf.org/html/rfc8259

Member Function Documentation

◆ type()

virtual Type ts::json::Value::type ( ) const
pure virtual

Get the JSON value type.

Returns
The JSON value type.

Implemented in ts::json::Array, ts::json::Object, ts::json::String, ts::json::Number, ts::json::False, ts::json::True, and ts::json::Null.

◆ print()

virtual void ts::json::Value::print ( TextFormatter output) const
pure virtual

Format the value as JSON text.

Parameters
[in,out]outputThe output object to format.

Implemented in ts::json::Array, ts::json::Object, ts::json::String, ts::json::Number, ts::json::False, ts::json::True, and ts::json::Null.

◆ printed()

virtual UString ts::json::Value::printed ( size_t  indent = 2,
Report report = (*ts::NullReport::Instance()) 
) const
virtual

Format the value as JSON text.

Parameters
[in]indentIndentation width of each level.
[in,out]reportWhere to report errors.
Returns
The formatted JSON text.

◆ isNull()

virtual bool ts::json::Value::isNull ( ) const
inlinevirtual

Check if this instance a is JSON null literal.

Returns
True if this instance a is JSON null literal.

Reimplemented in ts::json::Null.

◆ isTrue()

virtual bool ts::json::Value::isTrue ( ) const
inlinevirtual

Check if this instance a is JSON true literal.

Returns
True if this instance a is JSON true literal.

Reimplemented in ts::json::True.

◆ isFalse()

virtual bool ts::json::Value::isFalse ( ) const
inlinevirtual

Check if this instance a is JSON false literal.

Returns
True if this instance a is JSON false literal.

Reimplemented in ts::json::False.

◆ isNumber()

virtual bool ts::json::Value::isNumber ( ) const
inlinevirtual

Check if this instance a is JSON number.

Returns
True if this instance a is JSON number.

Reimplemented in ts::json::Number.

◆ isString()

virtual bool ts::json::Value::isString ( ) const
inlinevirtual

Check if this instance a is JSON string.

Returns
True if this instance a is JSON string.

Reimplemented in ts::json::String.

◆ isObject()

virtual bool ts::json::Value::isObject ( ) const
inlinevirtual

Check if this instance a is JSON object.

Returns
True if this instance a is JSON object.

Reimplemented in ts::json::Object.

◆ isArray()

virtual bool ts::json::Value::isArray ( ) const
inlinevirtual

Check if this instance a is JSON array.

Returns
True if this instance a is JSON array.

Reimplemented in ts::json::Array.

◆ toBoolean()

virtual bool ts::json::Value::toBoolean ( bool  defaultValue = false) const
inlinevirtual

Convert this instance to a boolean.

Parameters
[in]defaultValueDefault value to return when no conversion is possible.
Returns
The boolean value of this instance or defaultValue if no conversion is possible. For numbers, zero is false, all other values are true. For string, "yes", "true", "on" and non-zero integers are true; "no", "false", "off" and zero integers are false.

Reimplemented in ts::json::String, ts::json::Number, ts::json::False, and ts::json::True.

◆ toInteger()

virtual int64_t ts::json::Value::toInteger ( int64_t  defaultValue = 0) const
inlinevirtual

Convert this instance to an integer.

Parameters
[in]defaultValueDefault value to return when no conversion is possible.
Returns
The integer value of this instance or defaultValue if no conversion is possible. Strings containing integers are converted.

Reimplemented in ts::json::String, ts::json::Number, ts::json::False, and ts::json::True.

◆ toString()

virtual UString ts::json::Value::toString ( const UString defaultValue = UString()) const
inlinevirtual

Convert this instance to a string.

Parameters
[in]defaultValueDefault value to return when no conversion is possible.
Returns
The string value of this instance or defaultValue if no conversion is possible.

Reimplemented in ts::json::String, ts::json::Number, ts::json::False, and ts::json::True.

◆ size()

virtual size_t ts::json::Value::size ( ) const
inlinevirtual

Get the "size" of the value.

Returns
The size of the value: the number of characters for strings, of fields for objects, of elements for arrays, zero for other types.

Reimplemented in ts::json::Array, ts::json::Object, and ts::json::String.

◆ getNames()

virtual void ts::json::Value::getNames ( UStringList names) const
inlinevirtual

Get all field names in an object.

Parameters
[out]namesReceive the list of field names.

Reimplemented in ts::json::Object.

◆ value()

virtual const Value& ts::json::Value::value ( const UString name) const
virtual

Get the value of an object field.

Parameters
[in]nameField name.
Returns
For a JSON object, return a constant reference to the given element. When the field does not exist or for other types of JSON values, return a reference to a null JSON.

Reimplemented in ts::json::Object.

◆ remove()

virtual void ts::json::Value::remove ( const UString name)
inlinevirtual

Remove a field from an object.

Parameters
[in]nameField name.

Reimplemented in ts::json::Object.

◆ extract()

virtual ValuePtr ts::json::Value::extract ( const UString name)
inlinevirtual

Extract a field from an object.

The field is removed but its previous content is returned.

Parameters
[in]nameField name.
Returns
A smart pointer to the extracted content or a null pointer if the field does not exist.

Reimplemented in ts::json::Object.

◆ add()

virtual void ts::json::Value::add ( const UString name,
const ValuePtr value 
)
inlinevirtual

Add a field into an object.

Parameters
[in]nameField name.
[in]valueSmart pointer to a JSON value. The pointed object is shared.

Reimplemented in ts::json::Object.

◆ at()

virtual const Value& ts::json::Value::at ( size_t  index) const
virtual

Get an element of an array.

Parameters
[in]indexIndex to fetch in the array.
Returns
For a JSON array, return a constant reference to the given element. When out of bound or for other types of JSON values, return a reference to a null JSON.

Reimplemented in ts::json::Array.

◆ set()

virtual size_t ts::json::Value::set ( const ValuePtr value,
size_t  index = std::numeric_limits<size_t>::max() 
)
inlinevirtual

Set an element of an array.

Parameters
[in]valueSmart pointer to a JSON value. The pointed object is shared.
[in]indexIndex to fetch in the array. If out of bound, the value is added at the end of the array.
Returns
The actual index of the added value.

Reimplemented in ts::json::Array.

◆ erase()

virtual void ts::json::Value::erase ( size_t  index,
size_t  count = 1 
)
inlinevirtual

Erase elements from an array.

Parameters
[in]indexIndex to erase in the array. Ignored if out of bound.
[in]countNumber of elements to erase.

Reimplemented in ts::json::Array.

◆ extractAt()

virtual ValuePtr ts::json::Value::extractAt ( size_t  index)
inlinevirtual

Extract an element from an array.

The element is removed but its previous content is returned.

Parameters
[in]indexIndex to erase in the array. Ignored if out of bound.
Returns
A smart pointer to the extracted content or a null pointer if the element does not exist.

Reimplemented in ts::json::Array.


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