casacore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JsonParser.h
Go to the documentation of this file.
1 //# JsonParser.h: Class for parsing Json-style key:value lines
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$
27 
28 #ifndef CASA_JSONPARSER_H
29 #define CASA_JSONPARSER_H
30 
31 //# Includes
34 
35 extern "C" {
36  int JsonGramwrap(); // yywrap
37 }
38 
39 namespace casacore {
40 
41  //# Forward Declarations
42  class JsonValue;
43  class JSonKVMap;
44 
45  // <summary>
46  // Class for parsing Json-style key:value lines.
47  // </summary>
48 
49  // <use visibility=export>
50  // <reviewed reviewer="" date="" tests="tJsonKVMap">
51  // </reviewed>
52 
53  //# <prerequisite>
54  //# </prerequisite>
55 
56  // <synopsis>
57  // JsonParser is a class for parsing JSON files. Its function 'parse'
58  // is the main function to do so.
59  // It can handle any JSON file (not only those generated by JsonOut).
60  // It supports (i.e., strips) possible comments in C, C++ and Python style.
61  // It also supports complex numbers (structs with fields "r" and "i").
62  // Escaped characters in a string value are translated into their ASCII counterparts.
63  //
64  // The result of the parser is a JsonKVMap object containing all fields
65  // and values (scalars, arrays and structs, possibly nested in any way).
66  // The values in the map are stored as JsonValue objects, which have functions to
67  // get the value with the proper type.
68  // </synopsis>
69 
70  // <example>
71  // The following example is the opposite of the one given for class JsonOut.
72  // <srcblock>
73  // // Parse the given JSON file.
74  // JsonKVMap jmap = JsonParser::parseFile (fileName);
75  // // Check if the version is correct.
76  // AlwaysAssert (jmap.getInt("Version", 1) == 1, AipsError);
77  // uInt axis = jmap.get("Axis").getInt();
78  // // Get the vector of names from the map and JsonValue.
79  // Vector<String> names(jmap.get("Images").getArrayString());
80  // </srcblock>
81  // </example>
82 
83  // <motivation>
84  // JSON is a commonly used interchange format.
85  // However, commonly available parsers do not support data type Complex. Also, comments
86  // are often not supported (alas standard Json does not allow comments).
87  // </motivation>
88 
89  //# <todo asof="1996/03/10">
90  //# <li>
91  //# </todo>
92 
93  class JsonParser
94  {
95  public:
96  // Parse the command in the given string and return the resulting map.
97  static JsonKVMap parse (const String& command);
98 
99  // Parse the given file and return the resulting map.
100  // Comments are ignored; they can be indicated by // or # till eol
101  // or be enclosed in / * and * /.
102  static JsonKVMap parseFile (const String& fileName);
103 
104  // Give the next chunk of input for the scanner.
105  static int input (char* buf, int max_size);
106 
107  // Give the current position (for read or update).
108  static int& position()
109  { return theirPosition; }
110 
111  // Remove all possible escape characters and convert as needed (including <src>\uxxxx</src>).
112  static String removeEscapes (const String& in);
113 
114  // Let the parser set the final KeyValueMap.
115  static void setMap (JsonKVMap* map)
116  { theirJsonMap = map; }
117 
118  private:
119  static int theirPosition;
120  static const char* theirCommand;
122  };
123 
124  // The global yyerror function for the parser.
125  // It throws an exception with the current token.
126  void JsonGramerror (const char*);
127 
128  // </group>
129 
130 } // end namespace
131 
132 #endif
static JsonKVMap parseFile(const String &fileName)
Parse the given file and return the resulting map.
static JsonKVMap parse(const String &command)
Parse the command in the given string and return the resulting map.
static String removeEscapes(const String &in)
Remove all possible escape characters and convert as needed (including ).
void JsonGramerror(const char *)
The global yyerror function for the parser.
static int & position()
Give the current position (for read or update).
Definition: JsonParser.h:108
Class for parsing Json-style key:value lines.
Definition: JsonParser.h:93
static JsonKVMap * theirJsonMap
Definition: JsonParser.h:121
static void setMap(JsonKVMap *map)
Let the parser set the final KeyValueMap.
Definition: JsonParser.h:115
Class to hold a collection of JSON key:value pairs.
Definition: JsonKVMap.h:72
int JsonGramwrap()
static int input(char *buf, int max_size)
Give the next chunk of input for the scanner.
static const char * theirCommand
Definition: JsonParser.h:120
String: the storage and methods of handling collections of characters.
Definition: String.h:225
static int theirPosition
Definition: JsonParser.h:119