MIRA
Classes | Typedefs | Functions
mira::json Namespace Reference

Classes

class  JSONDefaultPrecision
 JSONDefaultPrecision is a singleton that provides a mechanism to control the default precision for output of floating-point JSON values. More...
 
struct  QueryNumberResult
 A struct used for return value of getNumberElementIfExists() More...
 
struct  QueryValueResult
 A struct used for return value of getElementIfExists() More...
 

Typedefs

typedef json_spirit::mValue Value
 A value is an abstract description of data in JSON (underlying data can either be one of the JSON base data types (int, double, string) or an json::Object, json::Array or null. More...
 
typedef json_spirit::mObject Object
 A representation of an object (class, struct) in JSON. More...
 
typedef json_spirit::mArray Array
 A representation of an array (vector) in JSON. More...
 

Functions

MIRA_BASE_EXPORT void write (const Value &value, std::ostream &ioStream, bool formatted=false, int precision=-1)
 Writes a json::Value into a given stream using the JSON format. More...
 
MIRA_BASE_EXPORT std::string write (const Value &value, bool formatted=false, int precision=-1)
 Writes a json::Value into a string using the JSON format. More...
 
MIRA_BASE_EXPORT void read (const std::string &s, Value &oValue)
 Read a json::Value from a string that contains JSON format. More...
 
MIRA_BASE_EXPORT void read (std::istream &ioStream, Value &oValue)
 Read a json::Value from a stream that contains JSON format. More...
 
MIRA_BASE_EXPORT void read (std::string::const_iterator &begin, std::string::const_iterator &end, Value &oValue)
 Read a json::Value from a string iterator range that contains JSON format. More...
 
MIRA_BASE_EXPORT Value getElement (const Value &iValue, const std::string &elementName)
 Get a json::Value element/member from a json::Value Syntax: More...
 
MIRA_BASE_EXPORT bool hasElement (const Value &iValue, const std::string &element)
 Query existence of element/member in a json::Value Use this variant to only query whether the element exists, avoiding exceptions. More...
 
MIRA_BASE_EXPORT QueryValueResult getElementIfExists (const Value &iValue, const std::string &elementName)
 Get a json::Value element/member from a json::Value if it exists, otherwise the returned result will contain the error string. More...
 
MIRA_BASE_EXPORT double getNumberElement (const Value &iValue, const std::string &elementName)
 Get a number element/member from a json::Value See getElement() for basic syntax. More...
 
MIRA_BASE_EXPORT bool hasNumberElement (const Value &iValue, const std::string &element)
 Query existence of number element/member in a json::Value Use this variant to only query whether the element exists and is a number, avoiding exceptions. More...
 
MIRA_BASE_EXPORT QueryNumberResult getNumberElementIfExists (const Value &iValue, const std::string &elementName)
 Get a number element/member from a json::Value if it exists, otherwise the returned result will contain the error string. More...
 
template<>
std::string cast< UUID > (const UUID &value)
 
template<>
UUID reverse_cast< UUID > (const std::string &value)
 

Typedef Documentation

◆ Value

typedef json_spirit::mValue Value

A value is an abstract description of data in JSON (underlying data can either be one of the JSON base data types (int, double, string) or an json::Object, json::Array or null.

◆ Object

typedef json_spirit::mObject Object

A representation of an object (class, struct) in JSON.

An object can have multiple members that are stored as named json::Value in a map.

◆ Array

typedef json_spirit::mArray Array

A representation of an array (vector) in JSON.

An array can have multiple items that are represented as json::Value in a vector.

Function Documentation

◆ write() [1/2]

MIRA_BASE_EXPORT void mira::json::write ( const Value value,
std::ostream &  ioStream,
bool  formatted = false,
int  precision = -1 
)

Writes a json::Value into a given stream using the JSON format.

Parameters
valueThe JSON value
[in,out]ioStreamThe stream
formattedIf true indentations, spaces and line breaks are inserted
precisionDecimal precision for output of floating-point values when writing value to stream (argument to std::setprecision). If < 0, query JSONDefaultPrecision.

◆ write() [2/2]

MIRA_BASE_EXPORT std::string mira::json::write ( const Value value,
bool  formatted = false,
int  precision = -1 
)

Writes a json::Value into a string using the JSON format.

Parameters
valueThe JSON value
formattedIf true indentations, spaces and line breaks are inserted when writing value to stream.
precisionDecimal precision for output of floating-point values when writing value to intermediate stream (argument to std::setprecision). If < 0, query JSONDefaultPrecision.
Returns
string containing JSON text

◆ read() [1/3]

MIRA_BASE_EXPORT void mira::json::read ( const std::string &  s,
Value oValue 
)

Read a json::Value from a string that contains JSON format.

Exceptions
XIOwhen string does not contain valid JSON format.
Parameters
sThe string to convert
[out]oValueThe returned value as reference
Examples:
tutorials/Point2Visualization.C, and tutorials/Point3Visualization.C.

◆ read() [2/3]

MIRA_BASE_EXPORT void mira::json::read ( std::istream &  ioStream,
Value oValue 
)

Read a json::Value from a stream that contains JSON format.

Exceptions
XIOwhen stream does not contain valid JSON format.
Parameters
[in,out]ioStreamThe stream to convert
[out]oValueThe returned value as reference

◆ read() [3/3]

MIRA_BASE_EXPORT void mira::json::read ( std::string::const_iterator &  begin,
std::string::const_iterator &  end,
Value oValue 
)
inline

Read a json::Value from a string iterator range that contains JSON format.

Exceptions
XIOwhen the range does not contain valid JSON format.
Parameters
beginThe iterator pointing to the start of the range
endThe iterator pointing to the end of the range
[out]oValueThe returned value as reference

◆ getElement()

MIRA_BASE_EXPORT Value mira::json::getElement ( const Value iValue,
const std::string &  elementName 
)

Get a json::Value element/member from a json::Value Syntax:

'Member' -> select element named 'Member' in value (must be a json::Object)
'[i]' -> select ith array element in value (must be a json::Array)
'Member[i]' -> select array element in member element
'.' -> nested selection, e.g. 'Member.SubMember'
'[i].[j]' -> select array element in array element
example:
'A[0].[1].X.Y' -> member 'Y' of member 'X' of 2nd array element
                  of 1st array element of member 'A' of iValue (count starts at 0)
iValue = {"A":[[1.000,{"X":{"Y":1}}],[2.500,3]]} --> return json::Value(1)
Exceptions
XInvalidConfigif the element could not be found or value is not an Object/Array.
Parameters
[in]iValueThe value to select the element from
[in]elementNameThe name of the element
Returns
The selected element

◆ hasElement()

MIRA_BASE_EXPORT bool mira::json::hasElement ( const Value iValue,
const std::string &  element 
)

Query existence of element/member in a json::Value Use this variant to only query whether the element exists, avoiding exceptions.

See getElement() for syntax.

Parameters
[in]iValueThe value to select the element from
[in]elementNameThe name of the element
Returns
Whether selected element exists

◆ getElementIfExists()

MIRA_BASE_EXPORT QueryValueResult mira::json::getElementIfExists ( const Value iValue,
const std::string &  elementName 
)

Get a json::Value element/member from a json::Value if it exists, otherwise the returned result will contain the error string.

Use this variant to avoid exceptions if the structure of iValue is unknown/not guaranteed. See getElement() for syntax.

Parameters
[in]iValueThe value to select the element from
[in]elementNameThe name of the element
Returns
A structure containing the result value or error message.

◆ getNumberElement()

MIRA_BASE_EXPORT double mira::json::getNumberElement ( const Value iValue,
const std::string &  elementName 
)

Get a number element/member from a json::Value See getElement() for basic syntax.

Additional syntax:

'[r][c]' -> select the specific matrix element at row/column
            (must be a string value representing a 2D matrix in Eigen-like format)
            This matrix element can only be a number, so there is no further nesting
example:
'M[1][0]' -> select row 2/column 1 from member 'M' (count starts at 0)
iValue = {"M":"[1,2,3;4,5,6]"} --> return 4
Exceptions
XInvalidConfigif the element could not be found, value is not an Object/Array/matrix representation or selected element is not a numerical/bool value.
Parameters
[in]iValueThe value to select the element from
[in]elementNameThe name of the element
Returns
The value of the selected element

◆ hasNumberElement()

MIRA_BASE_EXPORT bool mira::json::hasNumberElement ( const Value iValue,
const std::string &  element 
)

Query existence of number element/member in a json::Value Use this variant to only query whether the element exists and is a number, avoiding exceptions.

See getNumberElement() for syntax.

Parameters
[in]iValueThe value to select the element from
[in]elementNameThe name of the element
Returns
Whether selected element exists and is a number

◆ getNumberElementIfExists()

MIRA_BASE_EXPORT QueryNumberResult mira::json::getNumberElementIfExists ( const Value iValue,
const std::string &  elementName 
)

Get a number element/member from a json::Value if it exists, otherwise the returned result will contain the error string.

Use this variant to avoid exceptions if the structure of iValue is unknown/not guaranteed. See getNumberElement() for syntax.

Parameters
[in]iValueThe value to select the element from
[in]elementNameThe name of the element
Returns
A structure containing the result value or error message.

◆ cast< UUID >()

std::string mira::json::cast< UUID > ( const UUID value)
inline

◆ reverse_cast< UUID >()

UUID mira::json::reverse_cast< UUID > ( const std::string &  value)
inline