Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
118 lines (93 sloc) 4.11 KB

json++

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

The main block is here, which is generated from this github repo.

Simple parser and converter from JSON to XML

This example is already in biicode, it is very simple to build it, by just opening the block and building it.

~$ bii init jsonxx
~$ cd jsonxx
~/jsonxx$ bii open examples/jsonxx
~/jsonxx$ bii build

The code of the example is like this:

#include "hjiang/jsonxx/jsonxx.h"
using namespace jsonxx;
using namespace std;

int main() {
    #define QUOTE(...) #__VA_ARGS__
    string input = QUOTE(
    {
      "name/surname":"John Smith",
      'alias': 'Joe',
      "address": {
        "streetAddress": "21 2nd Street",
        "city": "New York",
        "state": "NY",
        "postal-code": 10021,
      }
    }
    );

    Object o;
    if( o.parse(input) ) {
        cout << o.xml(JSONx) << endl;            // XML output, JSONx flavor
        cout << o.xml(JXML) << endl;             // XML output, JXML flavor
        cout << o.xml(JXMLex) << endl;           // XML output, JXMLex flavor
        cout << o.xml(TaggedXML) << endl;        // XML output, tagged XML flavor
    }
    return 0;
}

Now, run the hello example.

~/jsonxx$ bin/examples_jsonxx_json_to_xml

You can see four diferent XML at the output:

<?xml version="1.0" encoding="UTF-8"?><!-- generated by jsonxx 0.22-a -->
<json:object xsi:schemaLocation="http://www.datapower.com/schemas/json jsonx.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx">
        <json:object name="address">
                <json:string name="city">New York</json:string>
                <json:number name="postal-code">10021</json:number>
                <json:string name="state">NY</json:string>
                <json:string name="streetAddress">21 2nd Street</json:string>
        </json:object>
        <json:string name="alias">Joe</json:string>
        <json:string name="name\/surname">John Smith</json:string>
</json:object>
<?xml version="1.0" encoding="UTF-8"?><!-- generated by jsonxx 0.22-a -->
<j son="o">
        <j son="o:address">
                <j son="s:city">New York</j>
                <j son="n:postal-code">10021</j>
                <j son="s:state">NY</j>
                <j son="s:streetAddress">21 2nd Street</j>
        </j>
        <j son="s:alias">Joe</j>
        <j son="s:name\/surname">John Smith</j>
</j>
<?xml version="1.0" encoding="UTF-8"?><!-- generated by jsonxx 0.22-a -->
<j son="o">
        <j son="o:address" address="">
                <j son="s:city" city="New York">New York</j>
                <j son="n:postal-code" postal_code="10021">10021</j>
                <j son="s:state" state="NY">NY</j>
                <j son="s:streetAddress" streetAddress="21 2nd Street">21 2nd Street</j>
        </j>
        <j son="s:alias" alias="Joe">Joe</j>
        <j son="s:name\/surname" name_surname="John Smith">John Smith</j>
</j>
<?xml version="1.0" encoding="UTF-8"?><!-- generated by jsonxx 0.22-a -->
<JsonItem type="json:object">
        <address type="json:object" name="address">
                <city type="json:string" name="city">New York</city>
                <postal_code type="json:number" name="postal-code">10021</postal_code>
                <state type="json:string" name="state">NY</state>
                <streetAddress type="json:string" name="streetAddress">21 2nd Street</streetAddress>
        </address>
        <alias type="json:string" name="alias">Joe</alias>
        <name_surname type="json:string" name="name\/surname">John Smith</name_surname>
</JsonItem>