casacore
Loading...
Searching...
No Matches
JsonKVMap.h
Go to the documentation of this file.
1// # JsonKVMap.h: Class to hold a collection of JSON key:value pairs
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_JSONKVMAP_H
27#define CASA_JSONKVMAP_H
28
29#include <casacore/casa/Json/JsonValue.h>
30#include <map>
31#include <iosfwd>
32
33namespace casacore {
34
35// # Forward Declarations
36class ValueHolder;
37
38// <summary>
39// Class to hold a collection of JSON key:value pairs.
40// </summary>
41
42// <use visibility=export>
43// <reviewed reviewer="" date="" tests="tJsonKVMap">
44// </reviewed>
45
46// # <prerequisite>
47// # </prerequisite>
48
49// <synopsis>
50// A JsonKVMap object is the result of a JSON file parsed by JsonParser.
51// It is a map of name to a JsonValue object holding an arbitrary value
52// (including a JsonKVMap for nested structs).
53//
54// JsonKVMap has functions to test if a given field is present and to
55// get its JsonValue. It also has functions to get a scalar value
56// where a default value is used if the key is undefined.
57//
58// JsonKVMap is derived from std::map, so all its functions are available.
59// Iterators make standard iteration possible.
60// </synopsis>
61
62// <motivation>
63// JSON is a commonly used interchange format.
64// </motivation>
65
66// # <todo asof="1996/03/10">
67// # <li>
68// # </todo>
69
70class JsonKVMap : public std::map<String, JsonValue> {
71 public:
72 // Define the iterator types.
73 typedef std::map<String, JsonValue>::const_iterator const_iterator;
74 typedef std::map<String, JsonValue>::iterator iterator;
75
76 // Construct an empty map.
78
79 // Copy constructor (copy semantics)
80 JsonKVMap(const JsonKVMap& that);
81
83
84 // Assignment (copy semantics)
86
87 // Is a key defined?
88 Bool isDefined(const String& name) const { return find(name) != end(); }
89
90 // Get the value of a key. An exception is thrown if undefined.
91 const JsonValue& get(const String& name) const;
92
93 // \name Get the typed value of a key
94 // Use the default if not existing.
95 // <group>
96 Bool getBool(const String& name, Bool defVal) const;
97 Int64 getInt(const String& name, Int64 defVal) const;
98 double getDouble(const String& name, double defVal) const;
99 DComplex getDComplex(const String& name, const DComplex& defVal) const;
100 const String& getString(const String& name, const String& defVal) const;
101 // </group>
102
103 // Convert the map to a Record.
105
106 // \name Show the contents of the object
107 // <group>
108 void show(ostream&) const;
109 friend ostream& operator<<(ostream&, const JsonKVMap&);
110 // </group>
111};
112
113} // namespace casacore
114
115#endif
JsonKVMap(const JsonKVMap &that)
Copy constructor (copy semantics).
JsonKVMap()
Construct an empty map.
void show(ostream &) const
Bool getBool(const String &name, Bool defVal) const
friend ostream & operator<<(ostream &, const JsonKVMap &)
std::map< String, JsonValue >::iterator iterator
Definition JsonKVMap.h:74
DComplex getDComplex(const String &name, const DComplex &defVal) const
std::map< String, JsonValue >::const_iterator const_iterator
Define the iterator types.
Definition JsonKVMap.h:73
const JsonValue & get(const String &name) const
Get the value of a key.
double getDouble(const String &name, double defVal) const
Bool isDefined(const String &name) const
Is a key defined?
Definition JsonKVMap.h:88
JsonKVMap & operator=(const JsonKVMap &that)
Assignment (copy semantics).
const String & getString(const String &name, const String &defVal) const
Record toRecord() const
Convert the map to a Record.
Int64 getInt(const String &name, Int64 defVal) 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
String name() const
Return the name of the field.
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
iterator end()
Definition Block.h:601