casacore
Loading...
Searching...
No Matches
JsonValue.h
Go to the documentation of this file.
1// # JsonValue.h: Class to hold any JSON value
2// # Copyright (C) 2016
3// # Associated Universities, Inc. Washington DC, USA.
4// #
5// # This library is free software; you can redistribute it and/or modify it
6// # under the terms of the GNU Library General Public License as published by
7// # the Free Software Foundation; either version 2 of the License, or (at your
8// # option) any later version.
9// #
10// # This library is distributed in the hope that it will be useful, but WITHOUT
11// # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12// # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13// # License for more details.
14// #
15// # You should have received a copy of the GNU Library General Public License
16// # along with this library; if not, write to the Free Software Foundation,
17// # Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18// #
19// # Correspondence concerning AIPS++ should be addressed as follows:
20// # Internet email: casa-feedback@nrao.edu.
21// # Postal address: AIPS++ Project Office
22// # National Radio Astronomy Observatory
23// # 520 Edgemont Road
24// # Charlottesville, VA 22903-2475 USA
25
26#ifndef CASA_JSONVALUE_H
27#define CASA_JSONVALUE_H
28
29// # Includes
30#include <casacore/casa/Arrays/ArrayFwd.h>
31#include <casacore/casa/BasicSL/String.h>
32#include <casacore/casa/BasicSL/Complex.h>
33#include <casacore/casa/Utilities/DataType.h>
34#include <casacore/casa/Utilities/Assert.h>
35#include <casacore/casa/Exceptions/Error.h>
36#include <vector>
37#include <iosfwd>
38
39namespace casacore {
40
41// # Forward Declarations
42class JsonKVMap;
43class ValueHolder;
44class IPosition;
45
46// <summary>
47// Class to hold any JSON value
48// </summary>
49
50// <use visibility=export>
51// <reviewed reviewer="" date="" tests="tJsonValue">
52// </reviewed>
53
54// # <prerequisite>
55// # </prerequisite>
56
57// <synopsis>
58// Class JsonValue can hold an arbitrary JSON value which can be a scalar,
59// a JsonKVMap object, or a vector of JsonValue objects. In this way
60// JSON values can be nested in any way.
61//
62// Internally scalar values are kept as Bool, Int64, Double, DComplex or
63// String values. The functions to obtain the value convert if possible.
64// Note that conversion from Int64 to Bool is supported.
65// The value can also be obtained as a ValueHolder object making it easier
66// to use in other Casacore code.
67// Null is also a valid JsonValue. A null value can be obtained as a
68// floating point value resulting in a NaN. It can also be obtained as a
69// null ValueHolder. Getting it for other types results in an exception.
70//
71// It is possible to obtain the value as a multi-dimensional Array object
72// if the values are regular, thus if nested vectors have the same sizes.
73// The data type of an Array is the 'highest' data type of a value in it.
74//
75// Normally a JsonValue object is created by JsonParser and is the
76// interface to obtain a value of a field in a parsed JSON file.
77// However, users can create JsonValue objects as well.
78// </synopsis>
79
80// <motivation>
81// JSON is a commonly used interchange format.
82// </motivation>
83
84// # <todo asof="1996/03/10">
85// # <li>
86// # </todo>
87
88class JsonValue {
89 public:
90 // The default constructor results in a null value.
92
93 // Construct value with given type.
94 // <group>
98 JsonValue(double);
99 JsonValue(const DComplex&);
100 JsonValue(const char*);
102 JsonValue(const std::vector<JsonValue>&);
104 // </group>
105
106 // Copy constructor (copy semantics).
108
109 // Assignment (copy semantics).
111
113
114 // Is the value a null value?
115 Bool isNull() const { return itsValuePtr == 0; }
116
117 // Is the value a vector?
118 Bool isVector() const { return itsDataType == TpOther; }
119
120 // Is the value a value map?
121 Bool isValueMap() const { return itsDataType == TpRecord; }
122
123 // Return the size of a value vector or map (1 is returned for a scalar).
124 size_t size() const;
125
126 // Get the data type of the value.
127 // A ValueMap is returned as TpRecord, a vector as TpOther.
128 DataType dataType() const { return itsDataType; }
129
130 // Get the most common data type of the value inside a possibly
131 // nested vector.
132 // <br>- If the value is a single value, that type is returned.
133 // <br>- If any vector value is a ValueMap, TpRecord is returned.
134 // <br>- If any vector contains non-matching data types, TpOther is
135 // returned.
136 // <br>- Otherwise the 'highest' data type is returned.
137 // <group>
138 DataType arrayDataType() const;
139 DataType vectorDataType(const std::vector<JsonValue>& vec) const;
140 // </group>
141
142 // Get the shape of an array (possibly nested vector).
143 // An exception is thrown if a vector contains a ValueMap or if
144 // the array shape is irregular (nested vectors have different sizes).
146 IPosition vectorShape(const std::vector<JsonValue>& vec) const;
147
148 // Get the value as a ValueHolder.
149 // A null value results in a null (empty) ValueHolder.
150 // An exception is thrown if the value cannot be represented as such,
151 // because it is a vector of differently typed values or nested vectors.
153
154 // Get the value in the given data type.
155 // Numeric data type promotion can be done as well as conversion of
156 // integer to bool (0=False, other=True). An exception is thrown if
157 // a mismatching data type is used.
158 // Note that a null value can only be obtained as double (giving NaN).
159 // <group>
160 Bool getBool() const;
161 Int64 getInt() const;
162 double getDouble() const;
163 DComplex getDComplex() const;
164 const String& getString() const;
165 // </group>
166
167 // As above, but get the value as a vector.
168 // If the value is a scalar, a vector with length 1 is returned.
169 // <group>
170 std::vector<Bool> getVecBool() const;
171 std::vector<Int64> getVecInt() const;
172 std::vector<double> getVecDouble() const;
173 std::vector<DComplex> getVecDComplex() const;
174 std::vector<String> getVecString() const;
175 const std::vector<JsonValue>& getVector() const;
176 // </group>
177
178 // Get the value as a JsonKVMap (no conversion is possible).
179 const JsonKVMap& getValueMap() const;
180
181 // Get the value as an Array. The value must be a scalar or a
182 // regularly nested vector.
183 // <group>
189 // </group>
190
191 // Get functions for templated purposes
192 // <group>
193 void get(Bool& value) const { value = getBool(); }
194 void get(Int64& value) const { value = getInt(); }
195 void get(double& value) const { value = getDouble(); }
196 void get(DComplex& value) const { value = getDComplex(); }
197 void get(String& value) const { value = getString(); }
198 void get(std::vector<Bool>& value) const { value = getVecBool(); }
199 void get(std::vector<Int64>& value) const { value = getVecInt(); }
200 void get(std::vector<double>& value) const { value = getVecDouble(); }
201 void get(std::vector<DComplex>& value) const { value = getVecDComplex(); }
202 void get(std::vector<String>& value) const { value = getVecString(); }
203 void get(std::vector<JsonValue>& value) const { value = getVector(); }
204 void get(JsonKVMap& value) const;
205 // </group>
206
207 // Show value on given ostream.
208 friend ostream& operator<<(ostream&, const JsonValue&);
209
210 private:
211 // Remove the value.
212 void clear();
213
214 // Copy the value from another one.
215 void copyValue(const JsonValue& that);
216
217 // Fill an array from nested vector in a recursive way.
218 template <typename T>
219 T* fillArray(T* data, const T* dataEnd, const std::vector<JsonValue>& vec) const {
220 for (std::vector<JsonValue>::const_iterator iter = vec.begin(); iter != vec.end(); ++iter) {
221 if (iter->dataType() == TpOther) {
222 data = fillArray(data, dataEnd, iter->getVector());
223 } else {
224 AlwaysAssert(data < dataEnd, AipsError);
225 iter->get(*data);
226 data++;
227 }
228 }
229 return data;
230 }
231
232 DataType itsDataType;
234};
235
236} // namespace casacore
237
238#endif
#define AlwaysAssert(expr, exception)
These marcos are provided for use instead of simply using the assert_ function directly.
Definition Assert.h:133
Array< String > getArrayString() const
Array< double > getArrayDouble() const
JsonValue(const std::vector< JsonValue > &)
std::vector< Int64 > getVecInt() const
DataType arrayDataType() const
Get the most common data type of the value inside a possibly nested vector.
void get(String &value) const
Definition JsonValue.h:197
std::vector< Bool > getVecBool() const
As above, but get the value as a vector.
JsonValue & operator=(const JsonValue &)
Assignment (copy semantics).
DataType dataType() const
Get the data type of the value.
Definition JsonValue.h:128
DataType vectorDataType(const std::vector< JsonValue > &vec) const
friend ostream & operator<<(ostream &, const JsonValue &)
Show value on given ostream.
Array< Int64 > getArrayInt() const
JsonValue(const JsonValue &)
Copy constructor (copy semantics).
ValueHolder getValueHolder() const
Get the value as a ValueHolder.
void get(std::vector< Int64 > &value) const
Definition JsonValue.h:199
const std::vector< JsonValue > & getVector() const
void get(JsonKVMap &value) const
T * fillArray(T *data, const T *dataEnd, const std::vector< JsonValue > &vec) const
Fill an array from nested vector in a recursive way.
Definition JsonValue.h:219
JsonValue(const DComplex &)
JsonValue(const char *)
IPosition shape() const
Get the shape of an array (possibly nested vector).
void get(std::vector< Bool > &value) const
Definition JsonValue.h:198
Bool isVector() const
Is the value a vector?
Definition JsonValue.h:118
const JsonKVMap & getValueMap() const
Get the value as a JsonKVMap (no conversion is possible).
void get(Int64 &value) const
Definition JsonValue.h:194
JsonValue(const String &)
void get(Bool &value) const
Get functions for templated purposes.
Definition JsonValue.h:193
double getDouble() const
void get(std::vector< DComplex > &value) const
Definition JsonValue.h:201
void get(double &value) const
Definition JsonValue.h:195
Bool getBool() const
Get the value in the given data type.
size_t size() const
Return the size of a value vector or map (1 is returned for a scalar).
DComplex getDComplex() const
void get(std::vector< double > &value) const
Definition JsonValue.h:200
std::vector< String > getVecString() const
Bool isValueMap() const
Is the value a value map?
Definition JsonValue.h:121
JsonValue()
The default constructor results in a null value.
JsonValue(const JsonKVMap &)
Bool isNull() const
Is the value a null value?
Definition JsonValue.h:115
void copyValue(const JsonValue &that)
Copy the value from another one.
std::vector< double > getVecDouble() const
void get(std::vector< JsonValue > &value) const
Definition JsonValue.h:203
void clear()
Remove the value.
JsonValue(Bool)
Construct value with given type.
void get(DComplex &value) const
Definition JsonValue.h:196
Int64 getInt() const
IPosition vectorShape(const std::vector< JsonValue > &vec) const
void get(std::vector< String > &value) const
Definition JsonValue.h:202
const String & getString() const
Array< Bool > getArrayBool() const
Get the value as an Array.
Array< DComplex > getArrayDComplex() const
std::vector< DComplex > getVecDComplex() const
String: the storage and methods of handling collections of characters.
Definition String.h:355
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size).
Definition aipsxtype.h:36
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
NewDelAllocator< T > NewDelAllocator< T >::value
Definition Allocator.h:360