Some C++ classes and templates to read JSON into objects in a flexible and robust way.
The underlying parsing work is done by JSMN.
The class is a template which allows json to be of type char *, const char * or others (?). There is a class to create the parser on stack.
Its written for usage in my own projects and may not be useful to others. The new documentation and example code is mainly there to help me use it myself and improve the API to make it easier to use.
Documentation
- View API documentation
- Create and view API documentation
make doxy-api-view
- Crate and view source documentation
make doxy-dev-view
Usage
To de-serialize an object, it would like this:
struct MyStruct {
int id;
char name[32];
template<typename jsmn_iterator>
bool from_json(jsmn_iterator &it) {
return jsoneat::take_all_from_object(it, JSONEAT_XNSPs(id, name));
}
};
class MyClass {
public:
int n;
int narr[4];
float f;
float farr[5];
bool b;
bool barr[b];
class MyStruct o;
class MyStruct oarr[6];
public:
template<typename jsmn_iterator>
bool from_json(jsmn_iterator &it) {
return jsoneat::take_all_from_object(it, JSONEAT_XNSPs(n, narr, f, farr, b, barr, o, oarr));
}
};
bool deserialize_my_object_from_json_string(const char *json) {
MyClass obj = {};
if (jsoneat::from_json_member(obj, json)) {
g(obj)
return true;
}
if (jsoneat::from_json_member<128>(obj, json)) {
g(obj)
return true;
}
}
Testing
- work in progress
- for now tests are run from application or a separate test package. Using git submodule to get all dependencies was not feasable.
Online Git Repositories
- This repository on GitHub
- JSON parser/tokenizer submodule: jsmn