casacore
Loading...
Searching...
No Matches
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: 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_JSONPARSER_H
27#define CASA_JSONPARSER_H
28
29//# Includes
30#include <casacore/casa/BasicSL/String.h>
31#include <casacore/casa/Exceptions/Error.h>
32
33extern "C" {
34 int JsonGramwrap(); // yywrap
35}
36
37namespace casacore {
38
39 //# Forward Declarations
40 class JsonValue;
41 class JSonKVMap;
42
43 // <summary>
44 // Class for parsing Json-style key:value lines.
45 // </summary>
46
47 // <use visibility=export>
48 // <reviewed reviewer="" date="" tests="tJsonKVMap">
49 // </reviewed>
50
51 //# <prerequisite>
52 //# </prerequisite>
53
54 // <synopsis>
55 // JsonParser is a class for parsing JSON files. Its function 'parse'
56 // is the main function to do so.
57 // It can handle any JSON file (not only those generated by JsonOut).
58 // It supports (i.e., strips) possible comments in C, C++ and Python style.
59 // It also supports complex numbers (structs with fields "r" and "i").
60 // Escaped characters in a string value are translated into their ASCII counterparts.
61 //
62 // The result of the parser is a JsonKVMap object containing all fields
63 // and values (scalars, arrays and structs, possibly nested in any way).
64 // The values in the map are stored as JsonValue objects, which have functions to
65 // get the value with the proper type.
66 // </synopsis>
67
68 // <example>
69 // The following example is the opposite of the one given for class JsonOut.
70 // <srcblock>
71 // // Parse the given JSON file.
72 // JsonKVMap jmap = JsonParser::parseFile (fileName);
73 // // Check if the version is correct.
74 // AlwaysAssert (jmap.getInt("Version", 1) == 1, AipsError);
75 // uInt axis = jmap.get("Axis").getInt();
76 // // Get the vector of names from the map and JsonValue.
77 // Vector<String> names(jmap.get("Images").getArrayString());
78 // </srcblock>
79 // </example>
80
81 // <motivation>
82 // JSON is a commonly used interchange format.
83 // However, commonly available parsers do not support data type Complex. Also, comments
84 // are often not supported (alas standard Json does not allow comments).
85 // </motivation>
86
87 //# <todo asof="1996/03/10">
88 //# <li>
89 //# </todo>
90
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 <src>\uxxxx</src>).
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
int JsonGramwrap()
static JsonKVMap * theirJsonMap
Definition JsonParser.h:119
static int theirPosition
Definition JsonParser.h:117
static JsonKVMap parse(const String &command)
Parse the command in the given string and return the resulting map.
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
static void setMap(JsonKVMap *map)
Let the parser set the final KeyValueMap.
Definition JsonParser.h:113
static String removeEscapes(const String &in)
Remove all possible escape characters and convert as needed (including \uxxxx).
static int & position()
Give the current position (for read or update).
Definition JsonParser.h:106
static JsonKVMap parseFile(const String &fileName)
Parse the given file and return the resulting map.
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
void JsonGramerror(const char *)
The global yyerror function for the parser.