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" {
34int JsonGramwrap(); // yywrap
35}
36
37namespace casacore {
38
39// # Forward Declarations
40class JsonValue;
41class 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 public:
93 // Parse the command in the given string and return the resulting map.
94 static JsonKVMap parse(const String& command);
95
96 // Parse the given file and return the resulting map.
97 // Comments are ignored; they can be indicated by // or # till eol
98 // or be enclosed in / * and * /.
99 static JsonKVMap parseFile(const String& fileName);
100
101 // Give the next chunk of input for the scanner.
102 static int input(char* buf, int max_size);
103
104 // Give the current position (for read or update).
105 static int& position() { return theirPosition; }
106
107 // Remove all possible escape characters and convert as needed (including <src>\uxxxx</src>).
108 static String removeEscapes(const String& in);
109
110 // Let the parser set the final KeyValueMap.
111 static void setMap(JsonKVMap* map) { 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.
121void JsonGramerror(const char*);
122
123// </group>
124
125} // namespace casacore
126
127#endif
int JsonGramwrap()
static JsonKVMap * theirJsonMap
Definition JsonParser.h:116
static int theirPosition
Definition JsonParser.h:114
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:115
static void setMap(JsonKVMap *map)
Let the parser set the final KeyValueMap.
Definition JsonParser.h:111
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:105
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:355
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
void JsonGramerror(const char *)
The global yyerror function for the parser.