annotate json11/test.cpp @ 116:d15cb1151d76

Add JSON support directly to the server. Had hoped to avoid this (using Capnp as canonical in the server and then converting externally as necessary) but it's just too useful for debugging purposes when bundled with client app
author Chris Cannam <c.cannam@qmul.ac.uk>
date Thu, 27 Oct 2016 11:39:41 +0100
parents 81e1c48e97f9
children
rev   line source
c@75 1 #include <string>
c@75 2 #include <cstdio>
c@75 3 #include <cstring>
c@75 4 #include <iostream>
c@75 5 #include <sstream>
c@75 6 #include "json11.hpp"
c@75 7 #include <cassert>
c@75 8 #include <list>
c@75 9 #include <set>
c@75 10 #include <unordered_map>
c@75 11
c@75 12 using namespace json11;
c@75 13 using std::string;
c@75 14
c@75 15 // Check that Json has the properties we want.
c@75 16 #include <type_traits>
c@75 17 #define CHECK_TRAIT(x) static_assert(std::x::value, #x)
c@75 18 CHECK_TRAIT(is_nothrow_constructible<Json>);
c@75 19 CHECK_TRAIT(is_nothrow_default_constructible<Json>);
c@75 20 CHECK_TRAIT(is_copy_constructible<Json>);
c@75 21 CHECK_TRAIT(is_nothrow_move_constructible<Json>);
c@75 22 CHECK_TRAIT(is_copy_assignable<Json>);
c@75 23 CHECK_TRAIT(is_nothrow_move_assignable<Json>);
c@75 24 CHECK_TRAIT(is_nothrow_destructible<Json>);
c@75 25
c@75 26 void parse_from_stdin() {
c@75 27 string buf;
c@75 28 string line;
c@75 29 while (std::getline(std::cin, line)) {
c@75 30 buf += line + "\n";
c@75 31 }
c@75 32
c@75 33 string err;
c@75 34 auto json = Json::parse(buf, err);
c@75 35 if (!err.empty()) {
c@75 36 printf("Failed: %s\n", err.c_str());
c@75 37 } else {
c@75 38 printf("Result: %s\n", json.dump().c_str());
c@75 39 }
c@75 40 }
c@75 41
c@75 42 int main(int argc, char **argv) {
c@75 43 if (argc == 2 && argv[1] == string("--stdin")) {
c@75 44 parse_from_stdin();
c@75 45 return 0;
c@75 46 }
c@75 47
c@75 48 const string simple_test =
c@75 49 R"({"k1":"v1", "k2":42, "k3":["a",123,true,false,null]})";
c@75 50
c@75 51 string err;
c@75 52 auto json = Json::parse(simple_test, err);
c@75 53
c@75 54 std::cout << "k1: " << json["k1"].string_value() << "\n";
c@75 55 std::cout << "k3: " << json["k3"].dump() << "\n";
c@75 56
c@75 57 for (auto &k : json["k3"].array_items()) {
c@75 58 std::cout << " - " << k.dump() << "\n";
c@75 59 }
c@75 60
c@75 61 const string comment_test = R"({
c@75 62 // comment /* with nested comment */
c@75 63 "a": 1,
c@75 64 // comment
c@75 65 // continued
c@75 66 "b": "text",
c@75 67 /* multi
c@75 68 line
c@75 69 comment */
c@75 70 // and single-line comment
c@75 71 "c": [1, 2, 3]
c@75 72 })";
c@75 73
c@75 74 string err_comment;
c@75 75 auto json_comment = Json::parse(
c@75 76 comment_test, err_comment, JsonParse::COMMENTS);
c@75 77 if (!err_comment.empty()) {
c@75 78 printf("Failed: %s\n", err_comment.c_str());
c@75 79 } else {
c@75 80 printf("Result: %s\n", json_comment.dump().c_str());
c@75 81 }
c@75 82
c@75 83 string failing_comment_test = R"({
c@75 84 /* bad comment
c@75 85 "a": 1,
c@75 86 })";
c@75 87
c@75 88 string err_failing_comment;
c@75 89 auto json_failing_comment = Json::parse(
c@75 90 failing_comment_test, err_failing_comment, JsonParse::COMMENTS);
c@75 91 if (!err_failing_comment.empty()) {
c@75 92 printf("Failed: %s\n", err_failing_comment.c_str());
c@75 93 } else {
c@75 94 printf("Result: %s\n", json_failing_comment.dump().c_str());
c@75 95 }
c@75 96
c@75 97 failing_comment_test = R"({
c@75 98 / / bad comment })";
c@75 99
c@75 100 json_failing_comment = Json::parse(
c@75 101 failing_comment_test, err_failing_comment, JsonParse::COMMENTS);
c@75 102 if (!err_failing_comment.empty()) {
c@75 103 printf("Failed: %s\n", err_failing_comment.c_str());
c@75 104 } else {
c@75 105 printf("Result: %s\n", json_failing_comment.dump().c_str());
c@75 106 }
c@75 107
c@75 108 failing_comment_test = R"({// bad comment })";
c@75 109
c@75 110 json_failing_comment = Json::parse(
c@75 111 failing_comment_test, err_failing_comment, JsonParse::COMMENTS);
c@75 112 if (!err_failing_comment.empty()) {
c@75 113 printf("Failed: %s\n", err_failing_comment.c_str());
c@75 114 } else {
c@75 115 printf("Result: %s\n", json_failing_comment.dump().c_str());
c@75 116 }
c@75 117
c@75 118 failing_comment_test = R"({
c@75 119 "a": 1
c@75 120 }/)";
c@75 121
c@75 122 json_failing_comment = Json::parse(
c@75 123 failing_comment_test, err_failing_comment, JsonParse::COMMENTS);
c@75 124 if (!err_failing_comment.empty()) {
c@75 125 printf("Failed: %s\n", err_failing_comment.c_str());
c@75 126 } else {
c@75 127 printf("Result: %s\n", json_failing_comment.dump().c_str());
c@75 128 }
c@75 129
c@75 130 failing_comment_test = R"({/* bad
c@75 131 comment *})";
c@75 132
c@75 133 json_failing_comment = Json::parse(
c@75 134 failing_comment_test, err_failing_comment, JsonParse::COMMENTS);
c@75 135 if (!err_failing_comment.empty()) {
c@75 136 printf("Failed: %s\n", err_failing_comment.c_str());
c@75 137 } else {
c@75 138 printf("Result: %s\n", json_failing_comment.dump().c_str());
c@75 139 }
c@75 140
c@75 141 std::list<int> l1 { 1, 2, 3 };
c@75 142 std::vector<int> l2 { 1, 2, 3 };
c@75 143 std::set<int> l3 { 1, 2, 3 };
c@75 144 assert(Json(l1) == Json(l2));
c@75 145 assert(Json(l2) == Json(l3));
c@75 146
c@75 147 std::map<string, string> m1 { { "k1", "v1" }, { "k2", "v2" } };
c@75 148 std::unordered_map<string, string> m2 { { "k1", "v1" }, { "k2", "v2" } };
c@75 149 assert(Json(m1) == Json(m2));
c@75 150
c@75 151 // Json literals
c@75 152 Json obj = Json::object({
c@75 153 { "k1", "v1" },
c@75 154 { "k2", 42.0 },
c@75 155 { "k3", Json::array({ "a", 123.0, true, false, nullptr }) },
c@75 156 });
c@75 157
c@75 158 std::cout << "obj: " << obj.dump() << "\n";
c@75 159
c@75 160 assert(Json("a").number_value() == 0);
c@75 161 assert(Json("a").string_value() == "a");
c@75 162 assert(Json().number_value() == 0);
c@75 163
c@75 164 assert(obj == json);
c@75 165 assert(Json(42) == Json(42.0));
c@75 166 assert(Json(42) != Json(42.1));
c@75 167
c@75 168 const string unicode_escape_test =
c@75 169 R"([ "blah\ud83d\udca9blah\ud83dblah\udca9blah\u0000blah\u1234" ])";
c@75 170
c@75 171 const char utf8[] = "blah" "\xf0\x9f\x92\xa9" "blah" "\xed\xa0\xbd" "blah"
c@75 172 "\xed\xb2\xa9" "blah" "\0" "blah" "\xe1\x88\xb4";
c@75 173
c@75 174 Json uni = Json::parse(unicode_escape_test, err);
c@75 175 assert(uni[0].string_value().size() == (sizeof utf8) - 1);
c@75 176 assert(std::memcmp(uni[0].string_value().data(), utf8, sizeof utf8) == 0);
c@75 177
c@75 178 // Demonstrates the behavior change in Xcode 7 / Clang 3.7 described
c@75 179 // here: https://llvm.org/bugs/show_bug.cgi?id=23812
c@75 180 Json nested_array = Json::array { Json::array { 1, 2, 3 } };
c@75 181 assert(nested_array.is_array());
c@75 182 assert(nested_array.array_items().size() == 1);
c@75 183 assert(nested_array.array_items()[0].is_array());
c@75 184 assert(nested_array.array_items()[0].array_items().size() == 3);
c@75 185
c@75 186 Json my_json = Json::object {
c@75 187 { "key1", "value1" },
c@75 188 { "key2", false },
c@75 189 { "key3", Json::array { 1, 2, 3 } },
c@75 190 };
c@75 191 std::string json_str = my_json.dump();
c@75 192 printf("%s\n", json_str.c_str());
c@75 193
c@75 194 class Point {
c@75 195 public:
c@75 196 int x;
c@75 197 int y;
c@75 198 Point (int x, int y) : x(x), y(y) {}
c@75 199 Json to_json() const { return Json::array { x, y }; }
c@75 200 };
c@75 201
c@75 202 std::vector<Point> points = { { 1, 2 }, { 10, 20 }, { 100, 200 } };
c@75 203 std::string points_json = Json(points).dump();
c@75 204 printf("%s\n", points_json.c_str());
c@75 205 }