TSDuck v3.40-3963
MPEG Transport Stream Toolkit
|
Abstract base class of a JSON value. More...
#include <tsjsonValue.h>
Public Member Functions | |
constexpr | Value ()=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 Value & | at (size_t index) |
Get an element of an array (modifiable version). | |
virtual const Value & | at (size_t index) const |
Get an element of an array (const version). | |
virtual void | clear () |
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) |
Extract a field from an object. | |
virtual ValuePtr | extractAt (size_t index) |
Extract an element from an array. | |
virtual void | getNames (UStringList &names) const |
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 |
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 =0 |
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 Value & | query (const UString &path) const |
Query a JSON value using a composite path. | |
virtual Value & | query (const UString &path, bool create=false, Type type=Type::Object) |
Query a JSON value using a composite path, creating intermediate objects when necessary. | |
virtual void | remove (const UString &name) |
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 |
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 =0 |
Get the JSON value type. | |
virtual const Value & | value (const UString &name) const |
Get the value of an object field (const version). | |
virtual Value & | value (const UString &name, bool create=false, Type type=Type::Object) |
Get the value of an object field (modifiable version). | |
virtual ValuePtr | valuePtr (const UString &name) |
Get the value of an object field (pointer version). | |
Protected Member Functions | |
virtual void | addFloat (const UString &name, double value) |
Add a double-precision floating-point number field into an object. | |
virtual void | addInteger (const UString &name, int64_t value) |
Add a 64-bit signed integer number field into an object. | |
virtual void | addString (const UString &name, const UString &value) |
Add a string field into an object. | |
virtual void | addValue (const UString &name, const ValuePtr &value) |
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. | |
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.
Get the JSON value type.
Implemented in ts::json::Array, ts::json::False, ts::json::Null, ts::json::Number, ts::json::Object, ts::json::String, and ts::json::True.
|
pure virtual |
Format the value as JSON text.
[in,out] | output | The output object to format. |
Implemented in ts::json::Array, ts::json::False, ts::json::Null, ts::json::Number, ts::json::Object, ts::json::String, and ts::json::True.
|
virtual |
Format the value as structured JSON text.
[in] | indent | Indentation width of each level. |
[in,out] | report | Where to report errors. |
|
virtual |
Format the value as a one-liner JSON text.
[in,out] | report | Where to report errors. |
|
virtual |
Save the value as a JSON file.
[in] | fileName | Name of the JSON file to save. |
[in] | indent | Indentation width of each level. |
[in] | stdOutputIfEmpty | If true and if fileName is empty or "-", writes to the standard output. |
[in,out] | report | Where to report errors. |
Check if this instance is a JSON null literal.
Reimplemented in ts::json::Null.
Check if this instance is a JSON true literal.
Reimplemented in ts::json::True.
Check if this instance is a JSON false literal.
Reimplemented in ts::json::False.
Check if this instance is a JSON number.
Reimplemented in ts::json::Number.
Check if this instance is a JSON number and an integer value (an integral value).
Reimplemented in ts::json::Number.
Check if this instance is a JSON string.
Reimplemented in ts::json::String.
Check if this instance is a JSON object.
Reimplemented in ts::json::Object.
Check if this instance is a JSON array.
Reimplemented in ts::json::Array.
Convert this instance to a boolean.
[in] | defaultValue | Default value to return when no conversion is possible. |
Reimplemented in ts::json::False, ts::json::Number, ts::json::String, and ts::json::True.
Convert this instance to an integer.
[in] | defaultValue | Default value to return when no conversion is possible. |
Reimplemented in ts::json::False, ts::json::Number, ts::json::String, and ts::json::True.
Convert this instance to a double-precision float.
[in] | defaultValue | Default value to return when no conversion is possible. |
Reimplemented in ts::json::False, ts::json::Number, ts::json::String, and ts::json::True.
Convert this instance to a string.
[in] | defaultValue | Default value to return when no conversion is possible. |
Reimplemented in ts::json::False, ts::json::Number, ts::json::String, and ts::json::True.
Clear the content of the value;.
Reimplemented in ts::json::Array, ts::json::Number, ts::json::Object, and ts::json::String.
Get the "size" of the value.
Reimplemented in ts::json::Array, ts::json::Object, and ts::json::String.
|
virtual |
Get all field names in an object.
[out] | names | Receive the list of field names. |
Reimplemented in ts::json::Object.
Get the value of an object field (const version).
[in] | name | Field name. |
Reimplemented in ts::json::Object.
|
virtual |
Get the value of an object field (modifiable version).
[in] | name | Field name. |
[in] | create | If true and the field does not exist, create it. |
[in] | type | When the field is created, provide the object type. |
Reimplemented in ts::json::Object.
Get the value of an object field (pointer version).
Must be a non-const object since we can potentially modify the field.
[in] | name | Field name. |
Reimplemented in ts::json::Object.
Extract a field from an object.
The field is removed but its previous content is returned.
[in] | name | Field name. |
Reimplemented in ts::json::Object.
Add a field into an object.
[in] | name | Field name. |
[in] | value | Smart pointer to a JSON value. The pointed object is shared. |
Add an integer number field into an object.
T | An integer type. |
[in] | name | Field name. |
[in] | value | Field value. |
Add a floating-point number field into an object.
T | A floating-point type. |
[in] | name | Field name. |
[in] | value | Field value. |
Add a string field into an object.
[in] | name | Field name. |
[in] | value | Field value. |
Get an element of an array (const version).
[in] | index | Index to fetch in the array. |
Reimplemented in ts::json::Array.
Get an element of an array (modifiable version).
[in] | index | Index to fetch in the array. |
Reimplemented in ts::json::Array.
|
inline |
Set an element of an array.
[in] | value | Smart pointer to a JSON value. The pointed object is shared. |
[in] | index | Index to fetch in the array. If out of bound, the value is added at the end of the array. |
|
inline |
Set an integer number element of an array.
T | An integer type. |
[in] | value | Value to set. |
[in] | index | Index in the array. If out of bound, the value is added at the end of the array. |
|
inline |
Set a floating-point number element of an array.
T | A floating-point type. |
[in] | value | Value to set. |
[in] | index | Index in the array. If out of bound, the value is added at the end of the array. |
|
inline |
Set a string element of an array.
[in] | value | Value to set. |
[in] | index | Index in the array. If out of bound, the value is added at the end of the array. |
Erase elements from an array.
[in] | index | Index to erase in the array. Ignored if out of bound. |
[in] | count | Number of elements to erase. |
Reimplemented in ts::json::Array.
Extract an element from an array.
The element is removed but its previous content is returned.
[in] | index | Index to erase in the array. Ignored if out of bound. |
Reimplemented in ts::json::Array.
Query a JSON value using a composite path.
[in] | path | Full 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. |
Reimplemented in ts::json::Array, and ts::json::Object.
|
virtual |
Query a JSON value using a composite path, creating intermediate objects when necessary.
[in] | path | Full 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] | create | If true create all missing intermediate objects. |
[in] | type | When the final field is created, provide the object type. |
Reimplemented in ts::json::Array, and ts::json::Object.
|
protectedvirtual |
Add a field into an object.
[in] | name | Field name. |
[in] | value | Smart pointer to a JSON value. The pointed object is shared. |
Reimplemented in ts::json::Object.
Add a 64-bit signed integer number field into an object.
[in] | name | Field name. |
[in] | value | Field value. |
Reimplemented in ts::json::Object.
Add a double-precision floating-point number field into an object.
[in] | name | Field name. |
[in] | value | Field value. |
Reimplemented in ts::json::Object.
|
protectedvirtual |
Add a string field into an object.
[in] | name | Field name. |
[in] | value | Field value. |
Reimplemented in ts::json::Object.
Set an element of an array.
[in] | value | Smart pointer to a JSON value. The pointed object is shared. |
[in] | index | Index to fetch in the array. If out of bound, the value is added at the end of the array. |
Reimplemented in ts::json::Array.
Set a 64-bit signed integer number element of an array.
[in] | value | Value to set. |
[in] | index | Index in the array. If out of bound, the value is added at the end of the array. |
Reimplemented in ts::json::Array.
Set a double-precision floating-point number element of an array.
[in] | value | Value to set. |
[in] | index | Index in the array. If out of bound, the value is added at the end of the array. |
Reimplemented in ts::json::Array.
Set a string element of an array.
[in] | value | Value to set. |
[in] | index | Index in the array. If out of bound, the value is added at the end of the array. |
Reimplemented in ts::json::Array.