casacore
Static Public Member Functions | Static Private Attributes | List of all members
casacore::JsonParser Class Reference

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 \uxxxx). 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());
#define AlwaysAssert(expr, exception)
These marcos are provided for use instead of simply using the constructors of assert_ to allow additi...
Definition: Assert.h:157
static JsonKVMap parseFile(const String &fileName)
Parse the given file and return the resulting map.
unsigned int uInt
Definition: aipstype.h:51

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

◆ input()

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

Give the next chunk of input for the scanner.

◆ parse()

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

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

◆ parseFile()

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 * /.

◆ position()

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

Give the current position (for read or update).

Definition at line 108 of file JsonParser.h.

References theirPosition.

◆ removeEscapes()

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

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

◆ setMap()

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

◆ theirCommand

const char* casacore::JsonParser::theirCommand
staticprivate

Definition at line 120 of file JsonParser.h.

◆ theirJsonMap

JsonKVMap* casacore::JsonParser::theirJsonMap
staticprivate

Definition at line 121 of file JsonParser.h.

Referenced by setMap().

◆ theirPosition

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: