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
42 class JsonKVMap;
43 class ValueHolder;
44 class IPosition;
45
46
47 // <summary>
48 // Class to hold any JSON value
49 // </summary>
50
51 // <use visibility=export>
52 // <reviewed reviewer="" date="" tests="tJsonValue">
53 // </reviewed>
54
55 //# <prerequisite>
56 //# </prerequisite>
57
58 // <synopsis>
59 // Class JsonValue can hold an arbitrary JSON value which can be a scalar,
60 // a JsonKVMap object, or a vector of JsonValue objects. In this way
61 // JSON values can be nested in any way.
62 //
63 // Internally scalar values are kept as Bool, Int64, Double, DComplex or
64 // String values. The functions to obtain the value convert if possible.
65 // Note that conversion from Int64 to Bool is supported.
66 // The value can also be obtained as a ValueHolder object making it easier
67 // to use in other Casacore code.
68 // Null is also a valid JsonValue. A null value can be obtained as a
69 // floating point value resulting in a NaN. It can also be obtained as a
70 // null ValueHolder. Getting it for other types results in an exception.
71 //
72 // It is possible to obtain the value as a multi-dimensional Array object
73 // if the values are regular, thus if nested vectors have the same sizes.
74 // The data type of an Array is the 'highest' data type of a value in it.
75 //
76 // Normally a JsonValue object is created by JsonParser and is the
77 // interface to obtain a value of a field in a parsed JSON file.
78 // However, users can create JsonValue objects as well.
79 // </synopsis>
80
81 // <motivation>
82 // JSON is a commonly used interchange format.
83 // </motivation>
84
85 //# <todo asof="1996/03/10">
86 //# <li>
87 //# </todo>
88
90 {
91 public:
92 // The default constructor results in a null value.
94
95 // Construct value with given type.
96 // <group>
98 JsonValue (int);
100 JsonValue (double);
101 JsonValue (const DComplex&);
102 JsonValue (const char*);
104 JsonValue (const std::vector<JsonValue>&);
106 // </group>
107
108 // Copy constructor (copy semantics).
110
111 // Assignment (copy semantics).
113
115
116 // Is the value a null value?
117 Bool isNull() const
118 { return itsValuePtr == 0; }
119
120 // Is the value a vector?
122 { return itsDataType == TpOther; }
123
124 // Is the value a value map?
126 { return itsDataType == TpRecord; }
127
128 // Return the size of a value vector or map (1 is returned for a scalar).
129 size_t size() const;
130
131 // Get the data type of the value.
132 // A ValueMap is returned as TpRecord, a vector as TpOther.
133 DataType dataType() const
134 { return itsDataType; }
135
136 // Get the most common data type of the value inside a possibly
137 // nested vector.
138 // <br>- If the value is a single value, that type is returned.
139 // <br>- If any vector value is a ValueMap, TpRecord is returned.
140 // <br>- If any vector contains non-matching data types, TpOther is
141 // returned.
142 // <br>- Otherwise the 'highest' data type is returned.
143 // <group>
144 DataType arrayDataType() const;
145 DataType vectorDataType (const std::vector<JsonValue>& vec) const;
146 // </group>
147
148 // Get the shape of an array (possibly nested vector).
149 // An exception is thrown if a vector contains a ValueMap or if
150 // the array shape is irregular (nested vectors have different sizes).
152 IPosition vectorShape (const std::vector<JsonValue>& vec) const;
153
154 // Get the value as a ValueHolder.
155 // A null value results in a null (empty) ValueHolder.
156 // An exception is thrown if the value cannot be represented as such,
157 // because it is a vector of differently typed values or nested vectors.
159
160 // Get the value in the given data type.
161 // Numeric data type promotion can be done as well as conversion of
162 // integer to bool (0=False, other=True). An exception is thrown if
163 // a mismatching data type is used.
164 // Note that a null value can only be obtained as double (giving NaN).
165 // <group>
166 Bool getBool() const;
167 Int64 getInt() const;
168 double getDouble() const;
169 DComplex getDComplex() const;
170 const String& getString() const;
171 // </group>
172
173 // As above, but get the value as a vector.
174 // If the value is a scalar, a vector with length 1 is returned.
175 // <group>
176 std::vector<Bool> getVecBool() const;
177 std::vector<Int64> getVecInt() const;
178 std::vector<double> getVecDouble() const;
179 std::vector<DComplex> getVecDComplex() const;
180 std::vector<String> getVecString() const;
181 const std::vector<JsonValue>& getVector() const;
182 // </group>
183
184 // Get the value as a JsonKVMap (no conversion is possible).
185 const JsonKVMap& getValueMap() const;
186
187 // Get the value as an Array. The value must be a scalar or a
188 // regularly nested vector.
189 // <group>
195 // </group>
196
197 // Get functions for templated purposes
198 // <group>
199 void get (Bool& value) const
200 { value = getBool(); }
201 void get (Int64& value) const
202 { value = getInt(); }
203 void get (double& value) const
204 { value = getDouble(); }
205 void get (DComplex& value) const
206 { value = getDComplex(); }
207 void get (String& value) const
208 { value = getString(); }
209 void get (std::vector<Bool>& value) const
210 { value = getVecBool(); }
211 void get (std::vector<Int64>& value) const
212 { value = getVecInt(); }
213 void get (std::vector<double>& value) const
214 { value = getVecDouble(); }
215 void get (std::vector<DComplex>& value) const
216 { value = getVecDComplex(); }
217 void get (std::vector<String>& value) const
218 { value = getVecString(); }
219 void get (std::vector<JsonValue>& value) const
220 { value = getVector(); }
221 void get (JsonKVMap& value) const;
222 // </group>
223
224 // Show value on given ostream.
225 friend ostream& operator<< (ostream&, const JsonValue&);
226
227 private:
228 // Remove the value.
229 void clear();
230
231 // Copy the value from another one.
232 void copyValue (const JsonValue& that);
233
234 // Fill an array from nested vector in a recursive way.
235 template<typename T>
236 T* fillArray (T* data, const T* dataEnd,
237 const std::vector<JsonValue>& vec) const
238 {
239 for (std::vector<JsonValue>::const_iterator iter=vec.begin();
240 iter!=vec.end(); ++iter) {
241 if (iter->dataType() == TpOther) {
242 data = fillArray (data, dataEnd, iter->getVector());
243 } else {
244 AlwaysAssert (data<dataEnd, AipsError);
245 iter->get (*data);
246 data++;
247 }
248 }
249 return data;
250 }
251
252 DataType itsDataType;
254 };
255
256
257} // end namespace
258
259#endif
#define AlwaysAssert(expr, exception)
These marcos are provided for use instead of simply using the constructors of assert_ to allow additi...
Definition Assert.h:155
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:207
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:133
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:211
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:236
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:209
Bool isVector() const
Is the value a vector?
Definition JsonValue.h:121
const JsonKVMap & getValueMap() const
Get the value as a JsonKVMap (no conversion is possible).
void get(Int64 &value) const
Definition JsonValue.h:201
JsonValue(const String &)
void get(Bool &value) const
Get functions for templated purposes.
Definition JsonValue.h:199
double getDouble() const
void get(std::vector< DComplex > &value) const
Definition JsonValue.h:215
void get(double &value) const
Definition JsonValue.h:203
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:213
std::vector< String > getVecString() const
Bool isValueMap() const
Is the value a value map?
Definition JsonValue.h:125
JsonValue()
The default constructor results in a null value.
JsonValue(const JsonKVMap &)
Bool isNull() const
Is the value a null value?
Definition JsonValue.h:117
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:219
void clear()
Remove the value.
JsonValue(Bool)
Construct value with given type.
void get(DComplex &value) const
Definition JsonValue.h:205
Int64 getInt() const
IPosition vectorShape(const std::vector< JsonValue > &vec) const
void get(std::vector< String > &value) const
Definition JsonValue.h:217
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:223
this file contains all the compiler specific defines
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
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.