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