jsonpp
jsonpp copied to clipboard
C++11 JSON parser and writer
trafficstars
jsonpp
jsonpp is a header-only JSON parser and writer that is currently in development. It is a semi-strict parser that throws exceptions for errors.
jsonpp is licensed with the MIT license.
Features
- Easy to use with a simple API.
- No special null, array, or object types.
json::nullis a type alias tostd::nullptr_t.json::arrayisstd::vector<json::value>.json::objectisstd::map<std::string, json::value>.
- Decently fast.
- No dependencies, only the standard library and a C++11 compiler.
Documentation
Documentation is an on going process and can be found here. Amongst documentation you can also find examples.
Example usage
Parsing
#include <jsonpp/parser.hpp>
#include <iostream>
int main() {
json::parser p;
json::value v;
try {
p.parse("[null, \"hello\", 10.0]", v);
if(v.is<json::array>()) {
for(auto&& val : v.as<json::array>()) {
std::cout << val.as<std::string>("stuff");
}
}
}
catch(const std::exception& e) {
std::cerr << e.what() << '\n';
}
}
Output:
stuff hello stuff
Writing
#include <jsonpp/value.hpp>
#include <iostream>
int main() {
json::value v = { nullptr, "hello", 10 };
json::object o = {
{ "key", "value" },
{ "key2", 2 },
{ "key3", nullptr }
};
json::dump(std::cout, o);
}
Output:
{
"key": "value",
"key2": 2,
"key3": null
}
Quirks and Specification
- NaN and inf are currently allowed.
- Comments, e.g.
// stuffis planned to be supported in the future. - The parser is not destructive.
- The parser is recursive descent.
- Numbers are stored in a
doublejust like JSON butv.as<int>and friends work with caution. - String is expected to be in UTF-8.
- Some errors are not caught but effort has been made to catch a lot of errors.