C++ header only jsmn based JSON parser
You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ICScaryThings c2bf0a72cb Move includes to folder 1 year ago
include Move includes to folder 1 year ago
Makefile Move includes to folder 1 year ago
README.md add example to readme 1 year ago
jsmnxpp_md_backup_test.cpp Seperate into header so it can be used 1 year ago


jsmnxpp - jsmn extended for c++

This is mini-header library that wraps and extends jsmn.h for c++ making it easier to use by providing that jsmn on its own doesn't:

  • Type conversion via the get_as<T>() method
  • String escape (e.g. \", \u3242, etc.) translation when get_as<std::string>() is used
  • Easier access to nested members with [] object key and array index lookups

At this time, this does not include a way to compose JSON, but neither does jsmn.h. I may make one in the future, but it would require adding more code from scratch, but could be contained to a different header to manage the bloat so the main problem would be the amount of debugging needed.


It appears to only run at half the speed of Rapidjson according to my current tests. In my tests with using jsmn.h in pure C, on the contrary I saw jsmn is capable of running twice as fast as Rapidjson. So that means the overhead of my code is resulting in performance that is a quarter of what jsmn.h alone is capable of.

The cause appears to be the copies that occur when accessing members so a better solution may be needed.

Anyways, despite not living up to my performance expectations, it is still much faster than most JSON parsers and one advantage over Rapidjson in its reduced code size. So it may be an option for keeping your included code size under control.


jsmnxpp::json(std::string json_string)

The constructor parses the JSON string with jsmn.h and makes it ready for use

jsmnxpp::json::operator[](std::string key)

If the current top level type is an object and the key exists within it a json of only the value for that key is returned. Otherwise, exceptions are thrown.

jsmnxpp::json::operator[](size_t index)

If the top level is a JSON array and index is within the size of the array, the value at that index is returned. Otherwise, exceptions are thrown.


If the json object only contains a primitive value (not an array or object), attempt to convert it to type T. If the conversion fails or if the json is not a primitive, exceptions will be thrown. For std::string, translate escapes and return the string.


Returns the number of elements or members in the top level of the JSON.


    jsmnxpp::json j(json_str);
    std::cout << j["foo"]["bar"].get_as<std::string>() << std::endl;
    std::cout << j["some"]["number"].get_as<long>() << std::endl;
    for (size_t k = 0; k < j["some_list"].size(); k++) std::cout << j["some_list"][k].get_as<long>() << std::endl;