Loading...
Searching...
No Matches
casacore::JsonParser Class Reference
casa package (libcasa_casa) » Json_module

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.
static JsonKVMap | parseFile (const String &fileName)
| Parse the given file and return the resulting map.
static int | input (char *buf, int max_size)
| Give the next chunk of input for the scanner.
static int & | position ()
| Give the current position (for read or update).
static String | removeEscapes (const String &in)
| Remove all possible escape characters and convert as needed (including \uxxxx).
static void | setMap (JsonKVMap *map)
| Let the parser set the final KeyValueMap.

## Static Private Attributes

static int | theirPosition
static const char * | theirCommand
static JsonKVMap * | theirJsonMap

## 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());
AlwaysAssert
#define AlwaysAssert(expr, exception)
These marcos are provided for use instead of simply using the assert_ function directly.
Definition Assert.h:134
casacore::AipsError
Definition Error.h:133
casacore::JsonKVMap
Definition JsonKVMap.h:71
casacore::JsonKVMap::get
const JsonValue & get(const String &name) const
Get the value of a key.
casacore::JsonKVMap::getInt
Int64 getInt(const String &name, Int64 defVal) const
casacore::JsonParser::parseFile
static JsonKVMap parseFile(const String &fileName)
Parse the given file and return the resulting map.
casacore::JsonValue::getArrayString
Array< String > getArrayString() const
casacore::JsonValue::getInt
Int64 getInt() const
casacore::Vector
Definition Vector.h:82
casacore::uInt
unsigned int uInt
Definition aipstype.h:49

### 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 91 of file JsonParser.h.

## Member Function Documentation

## input()

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

static

Give the next chunk of input for the scanner.

## parse()

JsonKVMap casacore::JsonParser::parse | ( | const String & | command | ) |

static

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

## parseFile()

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()

int & casacore::JsonParser::position | ( |  | ) |

inline static

Give the current position (for read or update).

Definition at line 106 of file JsonParser.h.

References theirPosition.

## removeEscapes()

String casacore::JsonParser::removeEscapes | ( | const String & | in | ) |

static

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

## setMap()

void casacore::JsonParser::setMap | ( | JsonKVMap * | map | ) |

inline static

Let the parser set the final KeyValueMap.

Definition at line 113 of file JsonParser.h.

References theirJsonMap.

## Member Data Documentation

## theirCommand

const char* casacore::JsonParser::theirCommand

static private

Definition at line 118 of file JsonParser.h.

## theirJsonMap

JsonKVMap* casacore::JsonParser::theirJsonMap

static private

Definition at line 119 of file JsonParser.h.

Referenced by setMap().

## theirPosition

int casacore::JsonParser::theirPosition

static private

Definition at line 117 of file JsonParser.h.

Referenced by position().

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

- casa/Json/JsonParser.h