Go to file
j4cbo ec4e45219a Change Json map/vector conversions to invoke begin() directly (#110)
... instead of using key_type/value_type/mapped_type. Because this could
otherwise false-positive on things like std::optional, which has a value_type
member type, but is not a container.

Fixes #109.
2017-06-20 16:48:22 -07:00
.gitignore Generate pkg-config File & Add Install 2016-04-16 18:43:09 +01:00
CMakeLists.txt Revert VERSION change, now setting CMP0048 policy to NEW 2017-04-28 14:46:38 -03:00
json11.cpp Extending identity check also to Json::operator< 2017-04-23 10:52:52 +02:00
json11.hpp Change Json map/vector conversions to invoke begin() directly (#110) 2017-06-20 16:48:22 -07:00
json11.pc.in CMakeLists: pkgconfig: stage for architecture specific compile/install 2016-09-07 17:33:00 -07:00
LICENSE.txt Add LICENSE.txt; some minor cleanups 2013-10-01 19:05:12 -07:00
Makefile Allow json11 tests to be customized with pre-processor defines 2016-07-28 22:27:43 -07:00
README.md
test.cpp Improve handling of errors in comments, and make comment tests self-checking. 2017-04-03 15:41:07 -07:00

json11

json11 is a tiny JSON library for C++11, providing JSON parsing and serialization.

The core object provided by the library is json11::Json. A Json object represents any JSON value: null, bool, number (int or double), string (std::string), array (std::vector), or object (std::map).

Json objects act like values. They can be assigned, copied, moved, compared for equality or order, and so on. There are also helper methods Json::dump, to serialize a Json to a string, and Json::parse (static) to parse a std::string as a Json object.

It's easy to make a JSON object with C++11's new initializer syntax:

Json my_json = Json::object {
    { "key1", "value1" },
    { "key2", false },
    { "key3", Json::array { 1, 2, 3 } },
};
std::string json_str = my_json.dump();

There are also implicit constructors that allow standard and user-defined types to be automatically converted to JSON. For example:

class Point {
public:
    int x;
    int y;
    Point (int x, int y) : x(x), y(y) {}
    Json to_json() const { return Json::array { x, y }; }
};

std::vector<Point> points = { { 1, 2 }, { 10, 20 }, { 100, 200 } };
std::string points_json = Json(points).dump();

JSON values can have their values queried and inspected:

Json json = Json::array { Json::object { { "k", "v" } } };
std::string str = json[0]["k"].string_value();

More documentation is still to come. For now, see json11.hpp.