|
| 1 | +json11 |
| 2 | +------ |
| 3 | + |
| 4 | +json11 is a tiny JSON library for C++11, providing JSON parsing and serialization. |
| 5 | + |
| 6 | +The core object provided by the library is json11::Json. A Json object represents any JSON |
| 7 | +value: null, bool, number (int or double), string (std::string), array (std::vector), or |
| 8 | +object (std::map). |
| 9 | + |
| 10 | +Json objects act like values. They can be assigned, copied, moved, compared for equality or |
| 11 | +order, and so on. There are also helper methods Json::dump, to serialize a Json to a string, and |
| 12 | +Json::parse (static) to parse a std::string as a Json object. |
| 13 | + |
| 14 | +It's easy to make a JSON object with C++11's new initializer syntax: |
| 15 | + |
| 16 | + Json my_json = Json::object { |
| 17 | + { "key1", "value1" }, |
| 18 | + { "key2", false }, |
| 19 | + { "key3", Json::array { 1, 2, 3 } }, |
| 20 | + }; |
| 21 | + std::string json_str = my_json.dump(); |
| 22 | + |
| 23 | +There are also implicit constructors that allow standard and user-defined types to be |
| 24 | +automatically converted to JSON. For example: |
| 25 | + |
| 26 | + class Point { |
| 27 | + public: |
| 28 | + int x; |
| 29 | + int y; |
| 30 | + Point (int x, int y) : x(x), y(y) {} |
| 31 | + Json to_json() const { return Json::array { x, y }; } |
| 32 | + }; |
| 33 | + |
| 34 | + std::vector<Point> points = { { 1, 2 }, { 10, 20 }, { 100, 200 } }; |
| 35 | + std::string points_json = Json(points).dump(); |
| 36 | + |
| 37 | +JSON values can have their values queried and inspected: |
| 38 | + |
| 39 | + Json json = Json::array { Json::object { { "k", "v" } } }; |
| 40 | + std::string str = json[0]["k"].string_value(); |
| 41 | + |
| 42 | +More documentation is still to come. For now, see json11.hpp. |
0 commit comments