casacore
Public Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
casacore::JsonValue Class Reference

Class to hold any JSON value. More...

#include <JsonValue.h>

Public Member Functions

 JsonValue ()
 The default constructor results in a null value. More...
 
 JsonValue (Bool)
 Construct value with given type. More...
 
 JsonValue (int)
 
 JsonValue (Int64)
 
 JsonValue (double)
 
 JsonValue (const DComplex &)
 
 JsonValue (const char *)
 
 JsonValue (const String &)
 
 JsonValue (const std::vector< JsonValue > &)
 
 JsonValue (const JsonKVMap &)
 
 JsonValue (const JsonValue &)
 Copy constructor (copy semantics). More...
 
JsonValueoperator= (const JsonValue &)
 Assignment (copy semantics). More...
 
 ~JsonValue ()
 
Bool isNull () const
 Is the value a null value? More...
 
Bool isVector () const
 Is the value a vector? More...
 
Bool isValueMap () const
 Is the value a value map? More...
 
size_t size () const
 Return the size of a value vector or map (1 is returned for a scalar). More...
 
DataType dataType () const
 Get the data type of the value. More...
 
DataType arrayDataType () const
 Get the most common data type of the value inside a possibly nested vector. More...
 
DataType vectorDataType (const std::vector< JsonValue > &vec) const
 
IPosition shape () const
 Get the shape of an array (possibly nested vector). More...
 
IPosition vectorShape (const std::vector< JsonValue > &vec) const
 
ValueHolder getValueHolder () const
 Get the value as a ValueHolder. More...
 
Bool getBool () const
 Get the value in the given data type. More...
 
Int64 getInt () const
 
double getDouble () const
 
DComplex getDComplex () const
 
const StringgetString () const
 
std::vector< BoolgetVecBool () const
 As above, but get the value as a vector. More...
 
std::vector< Int64getVecInt () const
 
std::vector< double > getVecDouble () const
 
std::vector< DComplex > getVecDComplex () const
 
std::vector< StringgetVecString () const
 
const std::vector< JsonValue > & getVector () const
 
const JsonKVMapgetValueMap () const
 Get the value as a JsonKVMap (no conversion is possible). More...
 
Array< BoolgetArrayBool () const
 Get the value as an Array. More...
 
Array< Int64getArrayInt () const
 
Array< double > getArrayDouble () const
 
Array< DComplex > getArrayDComplex () const
 
Array< StringgetArrayString () const
 
void get (Bool &value) const
 Get functions for templated purposes. More...
 
void get (Int64 &value) const
 
void get (double &value) const
 
void get (DComplex &value) const
 
void get (String &value) const
 
void get (std::vector< Bool > &value) const
 
void get (std::vector< Int64 > &value) const
 
void get (std::vector< double > &value) const
 
void get (std::vector< DComplex > &value) const
 
void get (std::vector< String > &value) const
 
void get (std::vector< JsonValue > &value) const
 
void get (JsonKVMap &value) const
 

Private Member Functions

void clear ()
 Remove the value. More...
 
void copyValue (const JsonValue &that)
 Copy the value from another one. More...
 
template<typename T >
T * fillArray (T *data, const T *dataEnd, const std::vector< JsonValue > &vec) const
 Fill an array from nested vector in a recursive way. More...
 

Private Attributes

DataType itsDataType
 
void * itsValuePtr
 

Friends

ostream & operator<< (ostream &, const JsonValue &)
 Show value on given ostream. More...
 

Detailed Description

Class to hold any JSON value.

Intended use:

Public interface

Review Status

Test programs:
tJsonValue

Synopsis

Class JsonValue can hold an arbitrary JSON value which can be a scalar, a JsonKVMap object, or a vector of JsonValue objects. In this way JSON values can be nested in any way.

Internally scalar values are kept as Bool, Int64, Double, DComplex or String values, but the class has functions to obtain the value in any data type as long as it can be converted. Note that conversion from Int64 to Bool is supported. Null is also a valid JsonValue. A null value can be obtained as a floating point value resulting in a NaN. For other types an exception is thrown.

It is possible to obtain the value as a multi-dimensional Array object if the values are regular, thus if nested vectors have the same sizes. The data type of an Array is the 'highest' data type of a value in it.

Normally a JsonValue objects is created by JsonParser and is the interface to obtain a value of a filed in a parsed JSON file. However, users can create JsonValue objects as well.

Motivation

JSON is a commonly used interchange format.

Definition at line 90 of file JsonValue.h.

Constructor & Destructor Documentation

◆ JsonValue() [1/11]

casacore::JsonValue::JsonValue ( )

The default constructor results in a null value.

◆ JsonValue() [2/11]

casacore::JsonValue::JsonValue ( Bool  )

Construct value with given type.

◆ JsonValue() [3/11]

casacore::JsonValue::JsonValue ( int  )

◆ JsonValue() [4/11]

casacore::JsonValue::JsonValue ( Int64  )

◆ JsonValue() [5/11]

casacore::JsonValue::JsonValue ( double  )

◆ JsonValue() [6/11]

casacore::JsonValue::JsonValue ( const DComplex &  )

◆ JsonValue() [7/11]

casacore::JsonValue::JsonValue ( const char *  )

◆ JsonValue() [8/11]

casacore::JsonValue::JsonValue ( const String )

◆ JsonValue() [9/11]

casacore::JsonValue::JsonValue ( const std::vector< JsonValue > &  )

◆ JsonValue() [10/11]

casacore::JsonValue::JsonValue ( const JsonKVMap )

◆ JsonValue() [11/11]

casacore::JsonValue::JsonValue ( const JsonValue )

Copy constructor (copy semantics).

◆ ~JsonValue()

casacore::JsonValue::~JsonValue ( )

Member Function Documentation

◆ arrayDataType()

DataType casacore::JsonValue::arrayDataType ( ) const

Get the most common data type of the value inside a possibly nested vector.


- If the value is a single value, that type is returned.
- If any vector value is a ValueMap, TpRecord is returned.
- If any vector contains non-matching data types, TpOther is returned.
- Otherwise the 'highest' data type is returned.

Referenced by dataType().

◆ clear()

void casacore::JsonValue::clear ( )
private

Remove the value.

Referenced by get().

◆ copyValue()

void casacore::JsonValue::copyValue ( const JsonValue that)
private

Copy the value from another one.

Referenced by get().

◆ dataType()

DataType casacore::JsonValue::dataType ( ) const
inline

◆ fillArray()

template<typename T >
T* casacore::JsonValue::fillArray ( T *  data,
const T *  dataEnd,
const std::vector< JsonValue > &  vec 
) const
inlineprivate

Fill an array from nested vector in a recursive way.

Definition at line 235 of file JsonValue.h.

References AlwaysAssert.

◆ get() [1/12]

void casacore::JsonValue::get ( Bool value) const
inline

Get functions for templated purposes.

Definition at line 198 of file JsonValue.h.

References getBool(), and casacore::value().

◆ get() [2/12]

void casacore::JsonValue::get ( Int64 value) const
inline

Definition at line 200 of file JsonValue.h.

References getInt(), and casacore::value().

◆ get() [3/12]

void casacore::JsonValue::get ( double &  value) const
inline

Definition at line 202 of file JsonValue.h.

References getDouble(), and casacore::value().

◆ get() [4/12]

void casacore::JsonValue::get ( DComplex &  value) const
inline

Definition at line 204 of file JsonValue.h.

References getDComplex(), and casacore::value().

◆ get() [5/12]

void casacore::JsonValue::get ( String value) const
inline

Definition at line 206 of file JsonValue.h.

References getString(), and casacore::value().

◆ get() [6/12]

void casacore::JsonValue::get ( std::vector< Bool > &  value) const
inline

Definition at line 208 of file JsonValue.h.

References getVecBool(), and casacore::value().

◆ get() [7/12]

void casacore::JsonValue::get ( std::vector< Int64 > &  value) const
inline

Definition at line 210 of file JsonValue.h.

References getVecInt(), and casacore::value().

◆ get() [8/12]

void casacore::JsonValue::get ( std::vector< double > &  value) const
inline

Definition at line 212 of file JsonValue.h.

References getVecDouble(), and casacore::value().

◆ get() [9/12]

void casacore::JsonValue::get ( std::vector< DComplex > &  value) const
inline

Definition at line 214 of file JsonValue.h.

References getVecDComplex(), and casacore::value().

◆ get() [10/12]

void casacore::JsonValue::get ( std::vector< String > &  value) const
inline

Definition at line 216 of file JsonValue.h.

References getVecString(), and casacore::value().

◆ get() [11/12]

void casacore::JsonValue::get ( std::vector< JsonValue > &  value) const
inline

Definition at line 218 of file JsonValue.h.

References clear(), copyValue(), getVector(), operator<<, and casacore::value().

◆ get() [12/12]

void casacore::JsonValue::get ( JsonKVMap value) const

◆ getArrayBool()

Array<Bool> casacore::JsonValue::getArrayBool ( ) const

Get the value as an Array.

The value must be a scalar or a regularly nested vector.

Referenced by dataType().

◆ getArrayDComplex()

Array<DComplex> casacore::JsonValue::getArrayDComplex ( ) const

Referenced by dataType().

◆ getArrayDouble()

Array<double> casacore::JsonValue::getArrayDouble ( ) const

Referenced by dataType().

◆ getArrayInt()

Array<Int64> casacore::JsonValue::getArrayInt ( ) const

Referenced by dataType().

◆ getArrayString()

Array<String> casacore::JsonValue::getArrayString ( ) const

Referenced by dataType().

◆ getBool()

Bool casacore::JsonValue::getBool ( ) const

Get the value in the given data type.

Numeric data type promotion can be done as well as conversion of integer to bool (0=False, other=True). An exception is thrown if a mismatching data type is used.

Referenced by dataType(), and get().

◆ getDComplex()

DComplex casacore::JsonValue::getDComplex ( ) const

Referenced by dataType(), and get().

◆ getDouble()

double casacore::JsonValue::getDouble ( ) const

Referenced by dataType(), and get().

◆ getInt()

Int64 casacore::JsonValue::getInt ( ) const

Referenced by dataType(), and get().

◆ getString()

const String& casacore::JsonValue::getString ( ) const

Referenced by dataType(), and get().

◆ getValueHolder()

ValueHolder casacore::JsonValue::getValueHolder ( ) const

Get the value as a ValueHolder.

An exception is thrown if the value cannot be represented as such, because it a vector of differently typed values or nested vectors.

Referenced by dataType().

◆ getValueMap()

const JsonKVMap& casacore::JsonValue::getValueMap ( ) const

Get the value as a JsonKVMap (no conversion is possible).

Referenced by dataType().

◆ getVecBool()

std::vector<Bool> casacore::JsonValue::getVecBool ( ) const

As above, but get the value as a vector.

If the value is a scalar, a vector with length 1 is returned.

Referenced by dataType(), and get().

◆ getVecDComplex()

std::vector<DComplex> casacore::JsonValue::getVecDComplex ( ) const

Referenced by dataType(), and get().

◆ getVecDouble()

std::vector<double> casacore::JsonValue::getVecDouble ( ) const

Referenced by dataType(), and get().

◆ getVecInt()

std::vector<Int64> casacore::JsonValue::getVecInt ( ) const

Referenced by dataType(), and get().

◆ getVecString()

std::vector<String> casacore::JsonValue::getVecString ( ) const

Referenced by dataType(), and get().

◆ getVector()

const std::vector<JsonValue>& casacore::JsonValue::getVector ( ) const

Referenced by dataType(), and get().

◆ isNull()

Bool casacore::JsonValue::isNull ( ) const
inline

Is the value a null value?

Definition at line 118 of file JsonValue.h.

References itsValuePtr.

◆ isValueMap()

Bool casacore::JsonValue::isValueMap ( ) const
inline

Is the value a value map?

Definition at line 126 of file JsonValue.h.

References itsDataType, and size().

◆ isVector()

Bool casacore::JsonValue::isVector ( ) const
inline

Is the value a vector?

Definition at line 122 of file JsonValue.h.

References itsDataType.

◆ operator=()

JsonValue& casacore::JsonValue::operator= ( const JsonValue )

Assignment (copy semantics).

◆ shape()

IPosition casacore::JsonValue::shape ( ) const

Get the shape of an array (possibly nested vector).

An exception is thrown if a vector contains a ValueMap or if the array shape is irregular (nested vectors have different sizes).

Referenced by dataType().

◆ size()

size_t casacore::JsonValue::size ( ) const

Return the size of a value vector or map (1 is returned for a scalar).

Referenced by isValueMap().

◆ vectorDataType()

DataType casacore::JsonValue::vectorDataType ( const std::vector< JsonValue > &  vec) const

Referenced by dataType().

◆ vectorShape()

IPosition casacore::JsonValue::vectorShape ( const std::vector< JsonValue > &  vec) const

Referenced by dataType().

Friends And Related Function Documentation

◆ operator<<

ostream& operator<< ( ostream &  ,
const JsonValue  
)
friend

Show value on given ostream.

Referenced by get().

Member Data Documentation

◆ itsDataType

DataType casacore::JsonValue::itsDataType
private

Definition at line 251 of file JsonValue.h.

Referenced by dataType(), isValueMap(), and isVector().

◆ itsValuePtr

void* casacore::JsonValue::itsValuePtr
private

Definition at line 252 of file JsonValue.h.

Referenced by isNull().


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