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  //
63  // The result of the parser is a JsonKVMap object containing all fields
64  // and values (scalars, arrays and structs, possibly nested in any way).
65  // </synopsis>
66 
67  // <example>
68  // The following example is the opposite of the one given for class JsonOut.
69  // <srcblock>
70  // // Parse the given JSON file.
71  // JsonKVMap jmap = JsonParser::parseFile (fileName);
72  // // Check if the version is correct.
73  // AlwaysAssert (jmap.getInt("Version", 1) == 1, AipsError);
74  // uInt axis = jmap.get("Axis").getInt();
75  // // Get the vector of names from the map and JsonValue.
76  // Vector<String> names(jmap.get("Images").getArrayString());
77  // </srcblock>
78  // </example>
79 
80  // <motivation>
81  // JSON is a commonly used interchange format.
82  // </motivation>
83 
84  //# <todo asof="1996/03/10">
85  //# <li>
86  //# </todo>
87 
88  class JsonParser
89  {
90  public:
91  // Parse the command in the given string and return the resulting map.
92  static JsonKVMap parse (const String& command);
93 
94  // Parse the given file and return the resulting map.
95  // Comments are ignored; they can be indicated by // or # till eol
96  // or be enclosed in / * and * /.
97  static JsonKVMap parseFile (const String& fileName);
98 
99  // Give the next chunk of input for the scanner.
100  static int input (char* buf, int max_size);
101 
102  // Give the current position (for read or update).
103  static int& position()
104  { return theirPosition; }
105 
106  // A function to remove escape characters.
107  static String removeEscapes (const String& in);
108 
109  // Let the parser set the final KeyValueMap.
110  static void setMap (JsonKVMap* map)
111  { theirJsonMap = map; }
112 
113  private:
114  static int theirPosition;
115  static const char* theirCommand;
117  };
118 
119  // The global yyerror function for the parser.
120  // It throws an exception with the current token.
121  void JsonGramerror (const char*);
122 
123  // </group>
124 
125 } // end namespace
126 
127 #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)
A function to remove escape characters.
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:103
Class for parsing Json-style key:value lines.
Definition: JsonParser.h:88
static JsonKVMap * theirJsonMap
Definition: JsonParser.h:116
static void setMap(JsonKVMap *map)
Let the parser set the final KeyValueMap.
Definition: JsonParser.h:110
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:115
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:114