TSDuck v3.38-3696
MPEG Transport Stream Toolkit
Loading...
Searching...
No Matches
ts::json::Object Class Reference

Implementation of a JSON object. More...

#include <tsjsonObject.h>

Inheritance diagram for ts::json::Object:
Collaboration diagram for ts::json::Object:

Public Member Functions

 Object ()=default
 Default constructor.
 
void add (const UString &name, const UString &value)
 Add a string field into an object.
 
void add (const UString &name, const ValuePtr &value)
 Add a field into an object.
 
template<typename T , typename std::enable_if< std::is_integral< T >::value, int >::type = 0>
void add (const UString &name, T value)
 Add an integer number field into an object.
 
template<typename T , typename std::enable_if< std::is_floating_point< T >::value, int >::type = 0>
void add (const UString &name, T value)
 Add a floating-point number field into an object.
 
virtual Valueat (size_t index)
 Get an element of an array (modifiable version).
 
virtual const Valueat (size_t index) const
 Get an element of an array (const version).
 
virtual void clear () override
 Clear the content of the value;.
 
virtual void erase (size_t index, size_t count=1)
 Erase elements from an array.
 
virtual ValuePtr extract (const UString &name) override
 Extract a field from an object.
 
virtual ValuePtr extractAt (size_t index)
 Extract an element from an array.
 
virtual void getNames (UStringList &names) const override
 Get all field names in an object.
 
virtual bool isArray () const
 Check if this instance is a JSON array.
 
virtual bool isFalse () const
 Check if this instance is a JSON false literal.
 
virtual bool isInteger () const
 Check if this instance is a JSON number and an integer value (an integral value).
 
virtual bool isNull () const
 Check if this instance is a JSON null literal.
 
virtual bool isNumber () const
 Check if this instance is a JSON number.
 
virtual bool isObject () const override
 Check if this instance is a JSON object.
 
virtual bool isString () const
 Check if this instance is a JSON string.
 
virtual bool isTrue () const
 Check if this instance is a JSON true literal.
 
virtual UString oneLiner (Report &report=(ts::NullReport::Instance())) const
 Format the value as a one-liner JSON text.
 
virtual void print (TextFormatter &output) const override
 Format the value as JSON text.
 
virtual UString printed (size_t indent=2, Report &report=(ts::NullReport::Instance())) const
 Format the value as structured JSON text.
 
virtual const Valuequery (const UString &path) const override
 Query a JSON value using a composite path.
 
virtual Valuequery (const UString &path, bool create=false, Type type=Type::Object) override
 Query a JSON value using a composite path, creating intermediate objects when necessary.
 
virtual void remove (const UString &name) override
 Remove a field from an object.
 
virtual bool save (const fs::path &fileName, size_t indent=2, bool stdOutputIfEmpty=false, Report &report=(ts::NullReport::Instance()))
 Save the value as a JSON file.
 
size_t set (const UString &value, size_t index=std::numeric_limits< size_t >::max())
 Set a string element of an array.
 
size_t set (const ValuePtr &value, size_t index=std::numeric_limits< size_t >::max())
 Set an element of an array.
 
template<typename T , typename std::enable_if< std::is_integral< T >::value, int >::type = 0>
size_t set (T value, size_t index=std::numeric_limits< size_t >::max())
 Set an integer number element of an array.
 
template<typename T , typename std::enable_if< std::is_floating_point< T >::value, int >::type = 0>
size_t set (T value, size_t index=std::numeric_limits< size_t >::max())
 Set a floating-point number element of an array.
 
virtual size_t size () const override
 Get the "size" of the value.
 
virtual bool toBoolean (bool defaultValue=false) const
 Convert this instance to a boolean.
 
virtual double toFloat (double defaultValue=0.0) const
 Convert this instance to a double-precision float.
 
virtual int64_t toInteger (int64_t defaultValue=0) const
 Convert this instance to an integer.
 
virtual UString toString (const UString &defaultValue=UString()) const
 Convert this instance to a string.
 
virtual Type type () const override
 Get the JSON value type.
 
virtual const Valuevalue (const UString &name) const override
 Get the value of an object field (const version).
 
virtual Valuevalue (const UString &name, bool create=false, Type type=Type::Object) override
 Get the value of an object field (modifiable version).
 
virtual ValuePtr valuePtr (const UString &name) override
 Get the value of an object field (pointer version).
 

Protected Member Functions

virtual void addFloat (const UString &name, double value) override
 Add a double-precision floating-point number field into an object.
 
virtual void addInteger (const UString &name, int64_t value) override
 Add a 64-bit signed integer number field into an object.
 
virtual void addString (const UString &name, const UString &value) override
 Add a string field into an object.
 
virtual void addValue (const UString &name, const ValuePtr &value) override
 Add a field into an object.
 
virtual size_t setFloat (double value, size_t index)
 Set a double-precision floating-point number element of an array.
 
virtual size_t setInteger (int64_t value, size_t index)
 Set a 64-bit signed integer number element of an array.
 
virtual size_t setString (const UString &value, size_t index)
 Set a string element of an array.
 
virtual size_t setValue (const ValuePtr &value, size_t index)
 Set an element of an array.
 

Detailed Description

Implementation of a JSON object.

Member Function Documentation

◆ type()

virtual Type ts::json::Object::type ( ) const
overridevirtual

Get the JSON value type.

Returns
The JSON value type.

Implements ts::json::Value.

◆ isObject()

virtual bool ts::json::Object::isObject ( ) const
overridevirtual

Check if this instance is a JSON object.

Returns
True if this instance is a JSON object.

Reimplemented from ts::json::Value.

◆ print()

virtual void ts::json::Object::print ( TextFormatter output) const
overridevirtual

Format the value as JSON text.

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

Implements ts::json::Value.

◆ size()

virtual size_t ts::json::Object::size ( ) const
overridevirtual

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 from ts::json::Value.

◆ value() [1/2]

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

Get the value of an object field (const version).

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 from ts::json::Value.

◆ value() [2/2]

virtual Value & ts::json::Object::value ( const UString name,
bool  create = false,
Type  type = Type::Object 
)
overridevirtual

Get the value of an object field (modifiable version).

Parameters
[in]nameField name.
[in]createIf true and the field does not exist, create it.
[in]typeWhen the field is created, provide the object type.
Returns
For a JSON object, return a 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 from ts::json::Value.

◆ valuePtr()

virtual ValuePtr ts::json::Object::valuePtr ( const UString name)
overridevirtual

Get the value of an object field (pointer version).

Must be a non-const object since we can potentially modify the field.

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

Reimplemented from ts::json::Value.

◆ remove()

virtual void ts::json::Object::remove ( const UString name)
overridevirtual

Remove a field from an object.

Parameters
[in]nameField name.

Reimplemented from ts::json::Value.

◆ extract()

virtual ValuePtr ts::json::Object::extract ( const UString name)
overridevirtual

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 from ts::json::Value.

◆ clear()

virtual void ts::json::Object::clear ( )
overridevirtual

Clear the content of the value;.

Reimplemented from ts::json::Value.

◆ getNames()

virtual void ts::json::Object::getNames ( UStringList names) const
overridevirtual

Get all field names in an object.

Parameters
[out]namesReceive the list of field names.

Reimplemented from ts::json::Value.

◆ query() [1/2]

virtual const Value & ts::json::Object::query ( const UString path) const
overridevirtual

Query a JSON value using a composite path.

Parameters
[in]pathFull path from this object to final object. May contain object field names and array indexes. Example: "obj1.arr[3].obj2". If this object is an array, the path must start with a index (example: "[3].obj2"). If the path is empty, this object is returned.
Returns
A reference to the final object or a reference to a null JSON if not found.

Reimplemented from ts::json::Value.

◆ query() [2/2]

virtual Value & ts::json::Object::query ( const UString path,
bool  create = false,
Type  type = Type::Object 
)
overridevirtual

Query a JSON value using a composite path, creating intermediate objects when necessary.

Parameters
[in]pathFull path from this object to final object. May contain object field names and array indexes. Example: "obj1.arr[3].obj2". If this object is an array, the path must start with a index (example: "[3].obj2"). When an array index is empty and create is true, a new element is created at the end of the array (example: "obj1.arr[].obj2"). If the path is empty, this object is returned.
[in]createIf true create all missing intermediate objects.
[in]typeWhen the final field is created, provide the object type.
Returns
A reference to the final object. When the field does not exist or could not be created, return a reference to a null JSON.

Reimplemented from ts::json::Value.

◆ addValue()

virtual void ts::json::Object::addValue ( const UString name,
const ValuePtr value 
)
overrideprotectedvirtual

Add a field into an object.

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

Reimplemented from ts::json::Value.

◆ addInteger()

virtual void ts::json::Object::addInteger ( const UString name,
int64_t  value 
)
overrideprotectedvirtual

Add a 64-bit signed integer number field into an object.

Parameters
[in]nameField name.
[in]valueField value.

Reimplemented from ts::json::Value.

◆ addFloat()

virtual void ts::json::Object::addFloat ( const UString name,
double  value 
)
overrideprotectedvirtual

Add a double-precision floating-point number field into an object.

Parameters
[in]nameField name.
[in]valueField value.

Reimplemented from ts::json::Value.

◆ addString()

virtual void ts::json::Object::addString ( const UString name,
const UString value 
)
overrideprotectedvirtual

Add a string field into an object.

Parameters
[in]nameField name.
[in]valueField value.

Reimplemented from ts::json::Value.

◆ printed()

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

Format the value as structured JSON text.

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

◆ oneLiner()

virtual UString ts::json::Value::oneLiner ( Report report = (ts::NullReport::Instance())) const
virtualinherited

Format the value as a one-liner JSON text.

Parameters
[in,out]reportWhere to report errors.
Returns
The formatted one-line JSON text.

◆ save()

virtual bool ts::json::Value::save ( const fs::path &  fileName,
size_t  indent = 2,
bool  stdOutputIfEmpty = false,
Report report = (ts::NullReport::Instance()) 
)
virtualinherited

Save the value as a JSON file.

Parameters
[in]fileNameName of the JSON file to save.
[in]indentIndentation width of each level.
[in]stdOutputIfEmptyIf true and if fileName is empty or "-", writes to the standard output.
[in,out]reportWhere to report errors.
Returns
True on success, false on error.

◆ isNull()

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

Check if this instance is a JSON null literal.

Returns
True if this instance is a JSON null literal.

Reimplemented in ts::json::Null.

◆ isTrue()

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

Check if this instance is a JSON true literal.

Returns
True if this instance is a JSON true literal.

Reimplemented in ts::json::True.

◆ isFalse()

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

Check if this instance is a JSON false literal.

Returns
True if this instance is a JSON false literal.

Reimplemented in ts::json::False.

◆ isNumber()

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

Check if this instance is a JSON number.

Returns
True if this instance is a JSON number.

Reimplemented in ts::json::Number.

◆ isInteger()

virtual bool ts::json::Value::isInteger ( ) const
virtualinherited

Check if this instance is a JSON number and an integer value (an integral value).

Returns
True if this instance is a JSON number and an integer value.

Reimplemented in ts::json::Number.

◆ isString()

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

Check if this instance is a JSON string.

Returns
True if this instance is a JSON string.

Reimplemented in ts::json::String.

◆ isArray()

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

Check if this instance is a JSON array.

Returns
True if this instance is a JSON array.

Reimplemented in ts::json::Array.

◆ toBoolean()

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

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::False, ts::json::Number, ts::json::String, and ts::json::True.

◆ toInteger()

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

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::False, ts::json::Number, ts::json::String, and ts::json::True.

◆ toFloat()

virtual double ts::json::Value::toFloat ( double  defaultValue = 0.0) const
virtualinherited

Convert this instance to a double-precision float.

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

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

◆ toString()

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

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::False, ts::json::Number, ts::json::String, and ts::json::True.

◆ add() [1/4]

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

Add a field into an object.

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

◆ add() [2/4]

template<typename T , typename std::enable_if< std::is_integral< T >::value, int >::type = 0>
void ts::json::Value::add ( const UString name,
T  value 
)
inlineinherited

Add an integer number field into an object.

Template Parameters
TAn integer type.
Parameters
[in]nameField name.
[in]valueField value.

◆ add() [3/4]

template<typename T , typename std::enable_if< std::is_floating_point< T >::value, int >::type = 0>
void ts::json::Value::add ( const UString name,
T  value 
)
inlineinherited

Add a floating-point number field into an object.

Template Parameters
TA floating-point type.
Parameters
[in]nameField name.
[in]valueField value.

◆ add() [4/4]

void ts::json::Value::add ( const UString name,
const UString value 
)
inlineinherited

Add a string field into an object.

Parameters
[in]nameField name.
[in]valueField value.

◆ at() [1/2]

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

Get an element of an array (const version).

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.

◆ at() [2/2]

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

Get an element of an array (modifiable version).

Parameters
[in]indexIndex to fetch in the array.
Returns
For a JSON array, return a 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() [1/4]

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

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.

◆ set() [2/4]

template<typename T , typename std::enable_if< std::is_integral< T >::value, int >::type = 0>
size_t ts::json::Value::set ( value,
size_t  index = std::numeric_limits<size_t>::max() 
)
inlineinherited

Set an integer number element of an array.

Template Parameters
TAn integer type.
Parameters
[in]valueValue to set.
[in]indexIndex 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.

◆ set() [3/4]

template<typename T , typename std::enable_if< std::is_floating_point< T >::value, int >::type = 0>
size_t ts::json::Value::set ( value,
size_t  index = std::numeric_limits<size_t>::max() 
)
inlineinherited

Set a floating-point number element of an array.

Template Parameters
TA floating-point type.
Parameters
[in]valueValue to set.
[in]indexIndex 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.

◆ set() [4/4]

size_t ts::json::Value::set ( const UString value,
size_t  index = std::numeric_limits<size_t>::max() 
)
inlineinherited

Set a string element of an array.

Parameters
[in]valueValue to set.
[in]indexIndex 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.

◆ erase()

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

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)
virtualinherited

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.

◆ setValue()

virtual size_t ts::json::Value::setValue ( const ValuePtr value,
size_t  index 
)
protectedvirtualinherited

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.

◆ setInteger()

virtual size_t ts::json::Value::setInteger ( int64_t  value,
size_t  index 
)
protectedvirtualinherited

Set a 64-bit signed integer number element of an array.

Parameters
[in]valueValue to set.
[in]indexIndex 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.

◆ setFloat()

virtual size_t ts::json::Value::setFloat ( double  value,
size_t  index 
)
protectedvirtualinherited

Set a double-precision floating-point number element of an array.

Parameters
[in]valueValue to set.
[in]indexIndex 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.

◆ setString()

virtual size_t ts::json::Value::setString ( const UString value,
size_t  index 
)
protectedvirtualinherited

Set a string element of an array.

Parameters
[in]valueValue to set.
[in]indexIndex 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.


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