h1. Introduction

JSON++ is a light-weight JSON parser, writer and reader written in C++.
JSON++ can also convert JSON documents into lossless XML documents.

h1. Contributors

http://github.com/hjiang
http://github.com/elanthis
http://github.com/r-lyeh

h1. Why another JSON parser?

Perhaps because web service clients are usually written in dynamic languages these days, none of the existing C++ JSON parsers suit my needs very well, so I wrote one that I used in another project. My goals for JSON++ are:

* Efficient in both memory and speed.
* No third party dependencies. JSON++ only depends on the standard C++ library.
* Cross platform.
* Robust.
* Small and convenient API. Most of the time, you only need to call one function and two function templates.
* Easy to integrate. JSON++ only has one source file and one header file. Just compile the source file and link with your program.
* Able to construct documents dinamically.
* JSON writer: write documents in JSON format.
* XML writer: convert documents to JSONx format. See http://goo.gl/I3cxs for details.
* XML writer: convert documents to JXML format. See https://github.com/r-lyeh/JXML for details.
* XML writer: convert documents to JXMLex format. See https://github.com/r-lyeh/JXMLex for details.
* XML writer: convert documents to tagged XML format. See https://github.com/hjiang/jsonxx/issues/12 for details.

h1. Configuration

h4. Strict/permissive parsing

JSONxx can parse JSON documents both in strict or permissive mode.

When <code>jsonxx::Settings::Parser</code> is set to <code>Strict</code>, JSONxx parser will accept:
* Fully conformant JSON documents *only*

When <code>jsonxx::Settings::Parser</code> is set to <code>Permissive</code>, JSONxx parser will accept:
* Fully conformant JSON documents
* Ending commas in arrays and objects: <code>{ "array": [0,1,2,], }</code>
* Single quoted strings: <code>['hello', "world"]</code>
* C++ style comments: <code>{ "width": 320, "height": 240 } //Picture details</code>

Default value is <code>Permissive</code>.

h4. Assertions

JSONxx uses internally <code>JSONXX_ASSERT(...)</code> macro that works both in debug and release mode. Set <code>jsonxx::Settings::Assertions</code> value to <code>Disabled</code> to disable assertions.

Default value is <code>Enabled</code>.

h1. Usage

The following snippets are from one of the unit tests. They are quite self-descriptive.

<pre>
using namespace std;
using namespace jsonxx;

string teststr(
        "{"
        "  \"foo\" : 1,"
        "  \"bar\" : false,"
        "  \"person\" : {\"name\" : \"GWB\", \"age\" : 60,},"
        "  \"data\": [\"abcd\", 42],"
        "}"
);

// Parse string or stream
Object o;
assert(o.parse(teststr));

// Validation. Checking for JSON types and values as well
assert(1 == o.get<Number>("foo"));
assert(o.has<Boolean>("bar"));
assert(o.has<Object>("person"));
assert(o.get<Object>("person").has<Number>("age"));
assert(o.has<Array>("data"));
assert(o.get<Array>("data").get<Number>(1) == 42);
assert(o.get<Array>("data").get<String>(0) == "abcd");
assert(!o.has<Number>("data"));
cout << o.json() << endl;                     // JSON output
cout << o.xml(JSONx) << endl;                 // JSON to XML conversion (JSONx subtype)
cout << o.xml(JXML) << endl;                  // JSON to XML conversion (JXML subtype)
cout << o.xml(JXMLex) << endl;                // JSON to XML conversion (JXMLex subtype)
</pre>

<pre>
// Generate JSON document dinamically
using namespace std;
using namespace jsonxx;
Array a;
a << 123;
a << "hello world";
a << 3.1415;
a << 99.95f;
a << 'h';
a << Object("key", "value");
Object o;
o << "key1" << "value";
o << "key2" << 123;
o << "key3" << a;
cout << o.json() << endl;
</pre>

h1. To do

* Custom JSON comments (C style /**/) when permissive parsing is enabled.
