Using the jsonsax class
The jsonsax class provides a callback-based framework for parsing JSON data. When it encounters an object, member, value, or other JSON component, it calls one of its callback methods. These methods may be overridden by a child class to perform specific tasks. The jsonsax class is especially useful if you can't afford to load the entire document into memory and use the jsondom class, or if you just need to extract specific data from a JSON file.
The following example creates a child class of jsonsax that overrides the objectStart(), memberName(), stringValue(), numberValue(), trueValue(), falseValue(), arrayStart(), arrayEnd(), and objectEnd() callback methods to print information about each JSON component encountered during parsing.
#include <rudiments/jsonsax.h>
#include <rudiments/stdio.h>
// create a json sax handler by inheriting from jsonsax
class myjsonhandler : public jsonsax {
protected:
bool objectStart() {
stdoutput.write("object start\n");
return true;
}
bool memberName(const char *name) {
stdoutput.printf(" member: %s\n",name);
return true;
}
bool stringValue(const char *value) {
stdoutput.printf(" string: %s\n",value);
return true;
}
bool numberValue(const char *value) {
stdoutput.printf(" number: %s\n",value);
return true;
}
bool trueValue() {
stdoutput.write(" true\n");
return true;
}
bool falseValue() {
stdoutput.write(" false\n");
return true;
}
bool arrayStart() {
stdoutput.write(" array start\n");
return true;
}
bool arrayEnd() {
stdoutput.write(" array end\n");
return true;
}
bool objectEnd() {
stdoutput.write("object end\n");
return true;
}
};
int main(int argc, const char **argv) {
myjsonhandler handler;
// parse a json string
stdoutput.write("parsing json:\n\n");
handler.parseString(
"{"
" \"animals\": ["
" {"
" \"name\": \"dog\","
" \"type\": \"mammal\","
" \"legs\": 4,"
" \"domestic\": true"
" },"
" {"
" \"name\": \"eagle\","
" \"type\": \"bird\","
" \"legs\": 2,"
" \"domestic\": false"
" },"
" {"
" \"name\": \"snake\","
" \"type\": \"reptile\","
" \"legs\": 0,"
" \"domestic\": false"
" }"
" ]"
"}");
}