casacore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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. 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

casacore::JsonValue::JsonValue ( )

The default constructor results in a null value.

casacore::JsonValue::JsonValue ( Bool  )

Construct value with given type.

casacore::JsonValue::JsonValue ( int  )
casacore::JsonValue::JsonValue ( Int64  )
casacore::JsonValue::JsonValue ( double  )
casacore::JsonValue::JsonValue ( const DComplex &  )
casacore::JsonValue::JsonValue ( const char *  )
casacore::JsonValue::JsonValue ( const String )
casacore::JsonValue::JsonValue ( const std::vector< JsonValue > &  )
casacore::JsonValue::JsonValue ( const JsonKVMap )
casacore::JsonValue::JsonValue ( const JsonValue )

Copy constructor (copy semantics).

casacore::JsonValue::~JsonValue ( )

Member Function Documentation

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.

void casacore::JsonValue::clear ( )
private

Remove the value.

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

Copy the value from another one.

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.

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.

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

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

Definition at line 203 of file JsonValue.h.

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

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

Definition at line 205 of file JsonValue.h.

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

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

Definition at line 207 of file JsonValue.h.

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

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

Definition at line 209 of file JsonValue.h.

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

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

Definition at line 211 of file JsonValue.h.

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

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

Definition at line 213 of file JsonValue.h.

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

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

Definition at line 215 of file JsonValue.h.

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

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

Definition at line 217 of file JsonValue.h.

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

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

Definition at line 219 of file JsonValue.h.

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

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

Definition at line 221 of file JsonValue.h.

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

void casacore::JsonValue::get ( JsonKVMap value) const
Array<Bool> casacore::JsonValue::getArrayBool ( ) const

Get the value as an Array.

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

Array<DComplex> casacore::JsonValue::getArrayDComplex ( ) const
Array<double> casacore::JsonValue::getArrayDouble ( ) const
Array<Int64> casacore::JsonValue::getArrayInt ( ) const
Array<String> casacore::JsonValue::getArrayString ( ) const
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().

DComplex casacore::JsonValue::getDComplex ( ) const

Referenced by get().

double casacore::JsonValue::getDouble ( ) const

Referenced by get().

Int64 casacore::JsonValue::getInt ( ) const

Referenced by get().

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

Referenced by get().

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.

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

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

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

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

Referenced by get().

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

Referenced by get().

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

Referenced by get().

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

Referenced by get().

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

Referenced by get().

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

Is the value a null value?

Definition at line 119 of file JsonValue.h.

References itsValuePtr.

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

Is the value a value map?

Definition at line 127 of file JsonValue.h.

References itsDataType.

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

Is the value a vector?

Definition at line 123 of file JsonValue.h.

References itsDataType.

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

Assignment (copy semantics).

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_t casacore::JsonValue::size ( ) const

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

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

Friends And Related Function Documentation

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

Show value on given ostream.

Member Data Documentation

DataType casacore::JsonValue::itsDataType
private

Definition at line 254 of file JsonValue.h.

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

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: