casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Static Public Member Functions | Static Private Attributes | List of all members
casacore::JsonParser Class Reference

Class for parsing Json-style key:value lines. More...

#include <JsonParser.h>

Static Public Member Functions

static JsonKVMap parse (const String &command)
 Parse the command in the given string and return the resulting map. More...
 
static JsonKVMap parseFile (const String &fileName)
 Parse the given file and return the resulting map. More...
 
static int input (char *buf, int max_size)
 Give the next chunk of input for the scanner. More...
 
static int & position ()
 Give the current position (for read or update). More...
 
static String removeEscapes (const String &in)
 Remove all possible escape characters and convert as needed (including ). More...
 
static void setMap (JsonKVMap *map)
 Let the parser set the final KeyValueMap. More...
 

Static Private Attributes

static int theirPosition
 
static const char * theirCommand
 
static JsonKVMaptheirJsonMap
 

Detailed Description

Class for parsing Json-style key:value lines.

Intended use:

Public interface

Review Status

Test programs:
tJsonKVMap

Synopsis

JsonParser is a class for parsing JSON files. Its function 'parse' is the main function to do so. It can handle any JSON file (not only those generated by JsonOut). It supports (i.e., strips) possible comments in C, C++ and Python style. It also supports complex numbers (structs with fields "r" and "i"). Escaped characters in a string value are translated into their ASCII counterparts.

The result of the parser is a JsonKVMap object containing all fields and values (scalars, arrays and structs, possibly nested in any way). The values in the map are stored as JsonValue objects, which have functions to get the value with the proper type.

Example

The following example is the opposite of the one given for class JsonOut.

// Parse the given JSON file.
JsonKVMap jmap = JsonParser::parseFile (fileName);
// Check if the version is correct.
AlwaysAssert (jmap.getInt("Version", 1) == 1, AipsError);
uInt axis = jmap.get("Axis").getInt();
// Get the vector of names from the map and JsonValue.
Vector<String> names(jmap.get("Images").getArrayString());

Motivation

JSON is a commonly used interchange format. However, commonly available parsers do not support data type Complex. Also, comments are often not supported (alas standard Json does not allow comments).

Definition at line 93 of file JsonParser.h.

Member Function Documentation

static int casacore::JsonParser::input ( char *  buf,
int  max_size 
)
static

Give the next chunk of input for the scanner.

static JsonKVMap casacore::JsonParser::parse ( const String command)
static

Parse the command in the given string and return the resulting map.

static JsonKVMap casacore::JsonParser::parseFile ( const String fileName)
static

Parse the given file and return the resulting map.

Comments are ignored; they can be indicated by // or # till eol or be enclosed in / * and * /.

static int& casacore::JsonParser::position ( )
inlinestatic

Give the current position (for read or update).

Definition at line 108 of file JsonParser.h.

References theirPosition.

static String casacore::JsonParser::removeEscapes ( const String in)
static

Remove all possible escape characters and convert as needed (including ).

static void casacore::JsonParser::setMap ( JsonKVMap map)
inlinestatic

Let the parser set the final KeyValueMap.

Definition at line 115 of file JsonParser.h.

References theirJsonMap.

Member Data Documentation

const char* casacore::JsonParser::theirCommand
staticprivate

Definition at line 120 of file JsonParser.h.

JsonKVMap* casacore::JsonParser::theirJsonMap
staticprivate

Definition at line 121 of file JsonParser.h.

Referenced by setMap().

int casacore::JsonParser::theirPosition
staticprivate

Definition at line 119 of file JsonParser.h.

Referenced by position().


The documentation for this class was generated from the following file: