annotate json/json11/test.cpp @ 66:6f160dee1192

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