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

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. The functions to obtain the value convert if possible. Note that conversion from Int64 to Bool is supported. The value can also be obtained as a ValueHolder object making it easier to use in other Casacore code. Null is also a valid JsonValue. A null value can be obtained as a floating point value resulting in a NaN. It can also be obtained as a null ValueHolder. Getting it for other types results in an exception.

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 object is created by JsonParser and is the interface to obtain a value of a field in a parsed JSON file. However, users can create JsonValue objects as well.

Motivation

JSON is a commonly used interchange format.

Definition at line 91 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.

◆ clear()

void casacore::JsonValue::clear ( )
private

Remove the value.

◆ copyValue()

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

Copy the value from another one.

◆ dataType()

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

Get the data type of the value.

A ValueMap is returned as TpRecord, a vector as TpOther.

Definition at line 135 of file JsonValue.h.

References itsDataType.

◆ 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 238 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 201 of file JsonValue.h.

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

◆ get() [2/12]

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

Definition at line 207 of file JsonValue.h.

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

◆ get() [3/12]

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

Definition at line 205 of file JsonValue.h.

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

◆ get() [4/12]

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

Definition at line 203 of file JsonValue.h.

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

◆ get() [5/12]

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

◆ get() [6/12]

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

Definition at line 211 of file JsonValue.h.

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

◆ get() [7/12]

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

Definition at line 217 of file JsonValue.h.

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

◆ get() [8/12]

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

Definition at line 215 of file JsonValue.h.

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

◆ get() [9/12]

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

Definition at line 213 of file JsonValue.h.

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

◆ get() [10/12]

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

Definition at line 221 of file JsonValue.h.

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

◆ get() [11/12]

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

Definition at line 219 of file JsonValue.h.

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

◆ get() [12/12]

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

Definition at line 209 of file JsonValue.h.

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

◆ getArrayBool()

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

Get the value as an Array.

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

◆ getArrayDComplex()

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

◆ getArrayDouble()

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

◆ getArrayInt()

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

◆ getArrayString()

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

◆ 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. Note that a null value can only be obtained as double (giving NaN).

Referenced by get().

◆ getDComplex()

DComplex casacore::JsonValue::getDComplex ( ) const

Referenced by get().

◆ getDouble()

double casacore::JsonValue::getDouble ( ) const

Referenced by get().

◆ getInt()

Int64 casacore::JsonValue::getInt ( ) const

Referenced by get().

◆ getString()

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

Referenced by get().

◆ getValueHolder()

ValueHolder casacore::JsonValue::getValueHolder ( ) const

Get the value as a ValueHolder.

A null value results in a null (empty) ValueHolder. An exception is thrown if the value cannot be represented as such, because it is a vector of differently typed values or nested vectors.

◆ getValueMap()

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

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

◆ 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 get().

◆ getVecDComplex()

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

Referenced by get().

◆ getVecDouble()

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

Referenced by get().

◆ getVecInt()

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

Referenced by get().

◆ getVecString()

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

Referenced by get().

◆ getVector()

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

Referenced by get().

◆ isNull()

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

Is the value a null value?

Definition at line 119 of file JsonValue.h.

References itsValuePtr.

◆ isValueMap()

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

Is the value a value map?

Definition at line 127 of file JsonValue.h.

References itsDataType.

◆ isVector()

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

Is the value a vector?

Definition at line 123 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).

◆ size()

size_t casacore::JsonValue::size ( ) const

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

◆ vectorDataType()

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

◆ vectorShape()

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

Friends And Related Function Documentation

◆ operator<<

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

Show value on given ostream.

Member Data Documentation

◆ itsDataType

DataType casacore::JsonValue::itsDataType
private

Definition at line 254 of file JsonValue.h.

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

◆ itsValuePtr

void* casacore::JsonValue::itsValuePtr
private

Definition at line 255 of file JsonValue.h.

Referenced by isNull().


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