casacore
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
32 #include <casacore/casa/BasicSL/String.h>
33 #include <casacore/casa/Exceptions/Error.h>
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  // </motivation>
86 
87  //# <todo asof="1996/03/10">
88  //# <li>
89  //# </todo>
90 
91  class JsonParser
92  {
93  public:
94  // Parse the command in the given string and return the resulting map.
95  static JsonKVMap parse (const String& command);
96 
97  // Parse the given file and return the resulting map.
98  // Comments are ignored; they can be indicated by // or # till eol
99  // or be enclosed in / * and * /.
100  static JsonKVMap parseFile (const String& fileName);
101 
102  // Give the next chunk of input for the scanner.
103  static int input (char* buf, int max_size);
104 
105  // Give the current position (for read or update).
106  static int& position()
107  { return theirPosition; }
108 
109  // Remove all possible escape characters and convert as needed (including \uxxxx).
110  static String removeEscapes (const String& in);
111 
112  // Let the parser set the final KeyValueMap.
113  static void setMap (JsonKVMap* map)
114  { theirJsonMap = map; }
115 
116  private:
117  static int theirPosition;
118  static const char* theirCommand;
120  };
121 
122  // The global yyerror function for the parser.
123  // It throws an exception with the current token.
124  void JsonGramerror (const char*);
125 
126  // </group>
127 
128 } // end namespace
129 
130 #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:106
Class for parsing Json-style key:value lines.
Definition: JsonParser.h:91
static JsonKVMap * theirJsonMap
Definition: JsonParser.h:119
static void setMap(JsonKVMap *map)
Let the parser set the final KeyValueMap.
Definition: JsonParser.h:113
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:118
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
static int theirPosition
Definition: JsonParser.h:117