casacore
|
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... | |
JsonValue & | operator= (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 String & | getString () const |
std::vector< Bool > | getVecBool () const |
As above, but get the value as a vector. More... | |
std::vector< Int64 > | getVecInt () const |
std::vector< double > | getVecDouble () const |
std::vector< DComplex > | getVecDComplex () const |
std::vector< String > | getVecString () const |
const std::vector< JsonValue > & | getVector () const |
const JsonKVMap & | getValueMap () const |
Get the value as a JsonKVMap (no conversion is possible). More... | |
Array< Bool > | getArrayBool () const |
Get the value as an Array. More... | |
Array< Int64 > | getArrayInt () const |
Array< double > | getArrayDouble () const |
Array< DComplex > | getArrayDComplex () const |
Array< String > | getArrayString () 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... | |
Class to hold any JSON value.
Public interface
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.
JSON is a commonly used interchange format.
Definition at line 91 of file JsonValue.h.
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 | ( | ) |
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().
|
private |
Remove the value.
Referenced by get().
|
private |
Copy the value from another one.
Referenced by get().
|
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 arrayDataType(), getArrayBool(), getArrayDComplex(), getArrayDouble(), getArrayInt(), getArrayString(), getBool(), getDComplex(), getDouble(), getInt(), getString(), getValueHolder(), getValueMap(), getVecBool(), getVecDComplex(), getVecDouble(), getVecInt(), getVecString(), getVector(), itsDataType, shape(), vectorDataType(), and vectorShape().
|
inlineprivate |
Fill an array from nested vector in a recursive way.
Definition at line 238 of file JsonValue.h.
References AlwaysAssert.
|
inline |
Get functions for templated purposes.
Definition at line 201 of file JsonValue.h.
References getBool(), and casacore::value().
|
inline |
Definition at line 203 of file JsonValue.h.
References getInt(), and casacore::value().
|
inline |
Definition at line 205 of file JsonValue.h.
References getDouble(), and casacore::value().
|
inline |
Definition at line 207 of file JsonValue.h.
References getDComplex(), and casacore::value().
|
inline |
Definition at line 209 of file JsonValue.h.
References getString(), and casacore::value().
|
inline |
Definition at line 211 of file JsonValue.h.
References getVecBool(), and casacore::value().
|
inline |
Definition at line 213 of file JsonValue.h.
References getVecInt(), and casacore::value().
|
inline |
Definition at line 215 of file JsonValue.h.
References getVecDouble(), and casacore::value().
|
inline |
Definition at line 217 of file JsonValue.h.
References getVecDComplex(), and casacore::value().
|
inline |
Definition at line 219 of file JsonValue.h.
References getVecString(), and casacore::value().
|
inline |
Definition at line 221 of file JsonValue.h.
References clear(), copyValue(), getVector(), operator<<, and casacore::value().
void casacore::JsonValue::get | ( | JsonKVMap & | value | ) | const |
Get the value as an Array.
The value must be a scalar or a regularly nested vector.
Referenced by dataType().
Array<DComplex> casacore::JsonValue::getArrayDComplex | ( | ) | const |
Referenced by dataType().
Array<double> casacore::JsonValue::getArrayDouble | ( | ) | const |
Referenced by dataType().
Referenced by dataType().
Referenced by dataType().
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 dataType(), and get().
DComplex casacore::JsonValue::getDComplex | ( | ) | const |
Referenced by dataType(), and get().
double casacore::JsonValue::getDouble | ( | ) | const |
Referenced by dataType(), and get().
Int64 casacore::JsonValue::getInt | ( | ) | const |
Referenced by dataType(), and get().
const String& casacore::JsonValue::getString | ( | ) | const |
Referenced by dataType(), and 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.
Referenced by dataType().
const JsonKVMap& casacore::JsonValue::getValueMap | ( | ) | const |
Get the value as a JsonKVMap (no conversion is possible).
Referenced by dataType().
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().
std::vector<DComplex> casacore::JsonValue::getVecDComplex | ( | ) | const |
Referenced by dataType(), and get().
std::vector<double> casacore::JsonValue::getVecDouble | ( | ) | const |
Referenced by dataType(), and get().
std::vector<Int64> casacore::JsonValue::getVecInt | ( | ) | const |
Referenced by dataType(), and get().
std::vector<String> casacore::JsonValue::getVecString | ( | ) | const |
Referenced by dataType(), and get().
const std::vector<JsonValue>& casacore::JsonValue::getVector | ( | ) | const |
Referenced by dataType(), and get().
|
inline |
|
inline |
Is the value a value map?
Definition at line 127 of file JsonValue.h.
References itsDataType, and size().
|
inline |
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_t casacore::JsonValue::size | ( | ) | const |
Return the size of a value vector or map (1 is returned for a scalar).
Referenced by isValueMap().
DataType casacore::JsonValue::vectorDataType | ( | const std::vector< JsonValue > & | vec | ) | const |
Referenced by dataType().
Referenced by dataType().
|
friend |
Show value on given ostream.
Referenced by get().
|
private |
Definition at line 254 of file JsonValue.h.
Referenced by dataType(), isValueMap(), and isVector().
|
private |
Definition at line 255 of file JsonValue.h.
Referenced by isNull().