spirit2json
A JSON parser/generator written with boost spirit
Classes | Typedefs | Enumerations | Functions
spirit2json Namespace Reference

Contains everything related spirit2json JSON parser/generator. More...

Classes

struct  json_grammar
 Spirit2 qi grammar for parsing json strings to JSONValue AST representations. More...
class  prettyPrinter
 Static visitor for pretty printing a JSONValue variant to an std::wostream. More...
class  Exception
 Baseclass for all exceptions in simple2json. More...
class  ParsingFailed
 Thrown on parser failure. More...

Typedefs

typedef std::nullptr_t JSONNull
 Typedef to std::nullptr_t for compilers that support it. Emulate otherwise.
typedef std::wstring JSONString
 Typedef to std::wstring.
typedef double JSONNumber
 Typedef to double.
typedef bool JSONBool
 Typedef to bool.
typedef
boost::make_recursive_variant
< JSONString, JSONNumber,
JSONBool, JSONNull,
std::vector
< boost::recursive_variant_ >
, std::map< JSONString,
boost::recursive_variant_ >
>::type 
JSONValue
 JSON-AST value type. Used to store the abstract JSON representation generated by the parse function and as the input to the generate and stream generation functionality.
typedef std::vector< JSONValueJSONArray
 JSON-AST array type. A vector storing JSONValue objects.
typedef std::map< JSONString,
JSONValue
JSONObject
 JSON-AST object type. A map storing string, JSONValue pairs.
typedef JSONObject::value_type JSONObjectPair
 Value type for JSONObject.

Enumerations

enum  JSONValueTypes {
  JSON_STRING, JSON_NUMBER, JSON_BOOL, JSON_NULL,
  JSON_ARRAY, JSON_OBJECT
}
 which() value enumeration enum for all types that can be contained in a JSONValue. More...

Functions

JSONValue parse (JSONString str)
 Parse a given JSON string and return its JSONValue representation.
JSONString generate (JSONValue &val)
 Generate a JSON string representation from a given JSONValue.

Detailed Description

Contains everything related spirit2json JSON parser/generator.


Typedef Documentation

typedef std::vector<JSONValue> spirit2json::JSONArray

JSON-AST array type. A vector storing JSONValue objects.

Usage:

 JSONArray array;
 array.push_back(JSONValue(L"Hello World"));
 array.push_back(JSONValue(42));
 array.push_back(JSONArray());
 ...
 wstring json = generate(JSONValue(array));
 ...
 wcout << JSONValue(array) << endl;

JSON-AST object type. A map storing string, JSONValue pairs.

Usage:

 JSONObject object;
 object.insert(JSONObjectPair(L"key", "value"));
 object.insert(JSONObjectPair(L"the answer to life the universe and everything", 42);
 object.insert(JSONObjectPair(L"an array", JSONArray());
 ...
 wstring json = generate(JSONValue(object));
 ...
 wcout << JSONValue(object) << endl;
typedef boost::make_recursive_variant< JSONString, JSONNumber, JSONBool, JSONNull, std::vector<boost::recursive_variant_ >, std::map<JSONString, boost::recursive_variant_ > >::type spirit2json::JSONValue

JSON-AST value type. Used to store the abstract JSON representation generated by the parse function and as the input to the generate and stream generation functionality.

Note:
The recommended way to walk an JSONValue AST is with a boost visitor.

Usage:

 wstring answer = generate(JSONValue(42));
 wstring hello = generate(JSONValue(L"world"));
 wstring array = generate(JSONValue(JSONArray()));
 ...
 JSONValue val(parse(L"{\"hello\":\"world\", \"array\":[4,2]}"));
 if (val.type() == JSON_OBJECT) {
     JSONObject object(boost::get<JSONObject>(val));
                ...

Enumeration Type Documentation

which() value enumeration enum for all types that can be contained in a JSONValue.

Note:
Not to confuse with RTTI typeinfo related data. This is boost::variant specific.
Enumerator:
JSON_STRING 

JSONValue(JSONString()).which() value.

JSON_NUMBER 

JSONValue(JSONNumber(0)).which() value.

JSON_BOOL 

JSONValue(JSONBool(false)).which() value.

JSON_NULL 

JSONValue(JSONNull()).which() value.

JSON_ARRAY 

JSONValue(JSONArray()).which() value.

JSON_OBJECT 

JSONValue(JSONObject()).which() value.


Function Documentation

JSONString spirit2json::generate ( JSONValue &  val)

Generate a JSON string representation from a given JSONValue.

Parameters:
valJSONValue representation
Returns:
String representation
JSONValue spirit2json::parse ( JSONString  str)

Parse a given JSON string and return its JSONValue representation.

Parameters:
strJSON string
Exceptions:
ParsingFailedParser failure
Returns:
JSONValue representation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator