A C++ JSON library
This library is not intended to fully replace other C++ JSON implementations.
The goal of this library was to solve two problems of other solutions I found:
- Data Reordering: Input order is kept.
- Memory Exhaustion: Tuples are immediatly flushed to disk.
Just include the lib directory on your solution
- No list support
- No nested entry support
- 5-entry-limited tuples
- Escapes only the slash character
Output JSON is idented based on the defined value of #define INITIAL_IDENT 0. No identation depth configuration is implemented yet.
A lot, please report them.
A set of tests to check JsonWriter's working.
Just create and destroy the JsonWriter object:
JsonWriter *json = new JsonWriter(file.c_str());
Output:
{
}
Just a single entry:
json->add_to_entry("field 1","1","field 2","2","field 3","3","field 4","4","field 5","5");
Output:
{
"Libs":
[
{
"field 1":"1"
,
"field 2":"2"
,
"field 3":"3"
,
"field 4":"4"
,
"field 5":"5"
}
]
}
Another single entry, without passing all arguments:
json->add_to_entry("field 1","1","field 2","2","field 3","3","","","","");
Output:
{
"Libs":
[
{
"field 1":"1"
,
"field 2":"2"
,
"field 3":"3"
}
]
}
Adding more entries to the previous examples:
json->add_to_entry("field 1","1","field 2","2","field 3","3","field 4","4","field 5","5");
json->add_to_entry("field 1","10","field 2","20","field 3","30","field 4","40","field 5","50");
Output:
{
"Libs":
[
{
"field 1":"1"
,
"field 2":"2"
,
"field 3":"3"
,
"field 4":"4"
,
"field 5":"5"
}
,
{
"field 1":"10"
,
"field 2":"20"
,
"field 3":"30"
,
"field 4":"40"
,
"field 5":"50"
}
]
}
Adding data on a higher entry:
json->add_entry(entry);
json->add_to_entry("field 1","1","field 2","2","field 3","3","field 4","4","field 5","5");
json->add_entry(entry2);
json->add_to_entry("field 1","10","field 2","20","field 3","30","field 4","40","field 5","50");
Output:
{
"Libs":
[
{
"field 1":"1"
,
"field 2":"2"
,
"field 3":"3"
,
"field 4":"4"
,
"field 5":"5"
}
]
,
"Bins":
[
{
"field 1":"10"
,
"field 2":"20"
,
"field 3":"30"
,
"field 4":"40"
,
"field 5":"50"
}
]
}
The most complete example:
json->add_entry(entry);
json->add_to_entry("field 1","1","field 2","2","field 3","3","field 4","4","field 5","5");
json->add_to_entry("field 1","1","field 2","2","field 3","3","field 4","4","field 5","5");
json->add_entry(entry2);
json->add_to_entry("field 1","10","field 2","20","field 3","30","field 4","40","field 5","50");
json->add_to_entry("field 1","10","field 2","20","field 3","30","field 4","40","field 5","50");
json->add_to_entry("field 1","10","field 2","20","field 3","30","field 4","40","field 5","50");
Output:
{
"Libs":
[
{
"field 1":"1"
,
"field 2":"2"
,
"field 3":"3"
,
"field 4":"4"
,
"field 5":"5"
}
,
{
"field 1":"1"
,
"field 2":"2"
,
"field 3":"3"
,
"field 4":"4"
,
"field 5":"5"
}
]
,
"Bins":
[
{
"field 1":"10"
,
"field 2":"20"
,
"field 3":"30"
,
"field 4":"40"
,
"field 5":"50"
}
,
{
"field 1":"10"
,
"field 2":"20"
,
"field 3":"30"
,
"field 4":"40"
,
"field 5":"50"
}
,
{
"field 1":"10"
,
"field 2":"20"
,
"field 3":"30"
,
"field 4":"40"
,
"field 5":"50"
}
]
}
Consider the following input:
{
"Movies":
[
{
"title":"A New Hope",
"year":"1977"
},
{
"title":"The Empire Strikes Back",
"year":"1980"
},
{
"title":"Return of the Jedi",
"year":"1983"
}
]
}
It can be parsed by the following python code:
f = open(sys.argv[1],"r").read()
l = json.loads(f)
for entries in l['Movies']:
print("Title",entries['title'])
print("Year",entries['year'])
The following output is generated:
('Title', u'A New Hope')
('Year', u'1977')
('Title', u'The Empire Strikes Back')
('Year', u'1980')
('Title', u'Return of the Jedi')
('Year', u'1983')