2016-07-29 13:27:43 +08:00
|
|
|
/*
|
|
|
|
* Define JSON11_TEST_CUSTOM_CONFIG to 1 if you want to build this tester into
|
|
|
|
* your own unit-test framework rather than a stand-alone program. By setting
|
|
|
|
* The values of the variables included below, you can insert your own custom
|
|
|
|
* code into this file as it builds, in order to make it into a test case for
|
|
|
|
* your favorite framework.
|
|
|
|
*/
|
2016-07-29 13:42:01 +08:00
|
|
|
#if !JSON11_TEST_CUSTOM_CONFIG
|
2016-07-29 13:27:43 +08:00
|
|
|
#define JSON11_TEST_CPP_PREFIX_CODE
|
|
|
|
#define JSON11_TEST_CPP_SUFFIX_CODE
|
|
|
|
#define JSON11_TEST_STANDALONE_MAIN 1
|
|
|
|
#define JSON11_TEST_CASE(name) static void name()
|
|
|
|
#define JSON11_TEST_ASSERT(b) assert(b)
|
2016-04-27 17:47:30 +08:00
|
|
|
#ifdef NDEBUG
|
|
|
|
#undef NDEBUG//at now assert will work even in Release build
|
|
|
|
#endif
|
2016-07-29 13:27:43 +08:00
|
|
|
#endif // JSON11_TEST_CUSTOM_CONFIG
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enable or disable code which demonstrates the behavior change in Xcode 7 / Clang 3.7,
|
|
|
|
* introduced by DR1467 and described here: https://llvm.org/bugs/show_bug.cgi?id=23812
|
|
|
|
* Defaults to on in order to act as a warning to anyone who's unaware of the issue.
|
|
|
|
*/
|
|
|
|
#ifndef JSON11_ENABLE_DR1467_CANARY
|
|
|
|
#define JSON11_ENABLE_DR1467_CANARY 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Beginning of standard source file, which makes use of the customizations above.
|
|
|
|
*/
|
2016-04-27 17:47:30 +08:00
|
|
|
#include <cassert>
|
2013-09-04 05:45:11 +08:00
|
|
|
#include <string>
|
|
|
|
#include <cstdio>
|
2015-06-18 09:02:34 +08:00
|
|
|
#include <cstring>
|
2013-09-04 05:45:11 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include "json11.hpp"
|
2013-10-02 10:04:32 +08:00
|
|
|
#include <list>
|
|
|
|
#include <set>
|
|
|
|
#include <unordered_map>
|
2016-04-27 17:47:30 +08:00
|
|
|
#include <algorithm>
|
2016-07-29 13:27:43 +08:00
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
// Insert user-defined prefix code (includes, function declarations, etc)
|
|
|
|
// to set up a custom test suite
|
|
|
|
JSON11_TEST_CPP_PREFIX_CODE
|
2013-09-04 05:45:11 +08:00
|
|
|
|
|
|
|
using namespace json11;
|
|
|
|
using std::string;
|
|
|
|
|
|
|
|
// Check that Json has the properties we want.
|
|
|
|
#define CHECK_TRAIT(x) static_assert(std::x::value, #x)
|
|
|
|
CHECK_TRAIT(is_nothrow_constructible<Json>);
|
|
|
|
CHECK_TRAIT(is_nothrow_default_constructible<Json>);
|
|
|
|
CHECK_TRAIT(is_copy_constructible<Json>);
|
|
|
|
CHECK_TRAIT(is_nothrow_move_constructible<Json>);
|
|
|
|
CHECK_TRAIT(is_copy_assignable<Json>);
|
|
|
|
CHECK_TRAIT(is_nothrow_move_assignable<Json>);
|
|
|
|
CHECK_TRAIT(is_nothrow_destructible<Json>);
|
|
|
|
|
2016-07-29 13:27:43 +08:00
|
|
|
JSON11_TEST_CASE(json11_test) {
|
2013-09-04 05:45:11 +08:00
|
|
|
const string simple_test =
|
|
|
|
R"({"k1":"v1", "k2":42, "k3":["a",123,true,false,null]})";
|
|
|
|
|
|
|
|
string err;
|
|
|
|
auto json = Json::parse(simple_test, err);
|
|
|
|
|
|
|
|
std::cout << "k1: " << json["k1"].string_value() << "\n";
|
|
|
|
std::cout << "k3: " << json["k3"].dump() << "\n";
|
|
|
|
|
|
|
|
for (auto &k : json["k3"].array_items()) {
|
|
|
|
std::cout << " - " << k.dump() << "\n";
|
|
|
|
}
|
|
|
|
|
2015-11-27 23:46:18 +08:00
|
|
|
const string comment_test = R"({
|
2015-11-30 19:42:24 +08:00
|
|
|
// comment /* with nested comment */
|
2015-11-27 23:46:18 +08:00
|
|
|
"a": 1,
|
|
|
|
// comment
|
|
|
|
// continued
|
|
|
|
"b": "text",
|
|
|
|
/* multi
|
|
|
|
line
|
|
|
|
comment */
|
2015-11-30 19:42:24 +08:00
|
|
|
// and single-line comment
|
2015-11-27 23:46:18 +08:00
|
|
|
"c": [1, 2, 3]
|
|
|
|
})";
|
|
|
|
|
|
|
|
string err_comment;
|
2015-11-30 19:27:35 +08:00
|
|
|
auto json_comment = Json::parse(
|
2015-12-01 18:08:37 +08:00
|
|
|
comment_test, err_comment, JsonParse::COMMENTS);
|
2015-11-27 23:46:18 +08:00
|
|
|
if (!err_comment.empty()) {
|
|
|
|
printf("Failed: %s\n", err_comment.c_str());
|
|
|
|
} else {
|
|
|
|
printf("Result: %s\n", json_comment.dump().c_str());
|
|
|
|
}
|
|
|
|
|
2015-11-30 19:43:40 +08:00
|
|
|
string failing_comment_test = R"({
|
|
|
|
/* bad comment
|
|
|
|
"a": 1,
|
2016-05-19 09:31:52 +08:00
|
|
|
// another comment to make C parsers which don't understand raw strings happy */
|
2015-11-30 19:43:40 +08:00
|
|
|
})";
|
|
|
|
|
|
|
|
string err_failing_comment;
|
|
|
|
auto json_failing_comment = Json::parse(
|
2015-12-01 18:08:37 +08:00
|
|
|
failing_comment_test, err_failing_comment, JsonParse::COMMENTS);
|
2015-11-30 19:43:40 +08:00
|
|
|
if (!err_failing_comment.empty()) {
|
|
|
|
printf("Failed: %s\n", err_failing_comment.c_str());
|
|
|
|
} else {
|
|
|
|
printf("Result: %s\n", json_failing_comment.dump().c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
failing_comment_test = R"({
|
2015-12-01 18:00:11 +08:00
|
|
|
/ / bad comment })";
|
2015-11-30 19:43:40 +08:00
|
|
|
|
|
|
|
json_failing_comment = Json::parse(
|
2015-12-01 18:08:37 +08:00
|
|
|
failing_comment_test, err_failing_comment, JsonParse::COMMENTS);
|
2015-11-30 19:43:40 +08:00
|
|
|
if (!err_failing_comment.empty()) {
|
|
|
|
printf("Failed: %s\n", err_failing_comment.c_str());
|
|
|
|
} else {
|
|
|
|
printf("Result: %s\n", json_failing_comment.dump().c_str());
|
|
|
|
}
|
|
|
|
|
2015-12-01 18:01:27 +08:00
|
|
|
failing_comment_test = R"({// bad comment })";
|
|
|
|
|
|
|
|
json_failing_comment = Json::parse(
|
2015-12-01 18:08:37 +08:00
|
|
|
failing_comment_test, err_failing_comment, JsonParse::COMMENTS);
|
2015-12-01 18:01:27 +08:00
|
|
|
if (!err_failing_comment.empty()) {
|
|
|
|
printf("Failed: %s\n", err_failing_comment.c_str());
|
|
|
|
} else {
|
|
|
|
printf("Result: %s\n", json_failing_comment.dump().c_str());
|
|
|
|
}
|
|
|
|
|
2015-11-30 19:43:40 +08:00
|
|
|
failing_comment_test = R"({
|
2015-12-01 18:00:58 +08:00
|
|
|
"a": 1
|
2015-11-30 19:43:40 +08:00
|
|
|
}/)";
|
|
|
|
|
|
|
|
json_failing_comment = Json::parse(
|
2015-12-01 18:08:37 +08:00
|
|
|
failing_comment_test, err_failing_comment, JsonParse::COMMENTS);
|
2015-11-30 19:43:40 +08:00
|
|
|
if (!err_failing_comment.empty()) {
|
|
|
|
printf("Failed: %s\n", err_failing_comment.c_str());
|
|
|
|
} else {
|
|
|
|
printf("Result: %s\n", json_failing_comment.dump().c_str());
|
|
|
|
}
|
|
|
|
|
2015-12-01 18:01:47 +08:00
|
|
|
failing_comment_test = R"({/* bad
|
|
|
|
comment *})";
|
|
|
|
|
|
|
|
json_failing_comment = Json::parse(
|
2015-12-01 18:08:37 +08:00
|
|
|
failing_comment_test, err_failing_comment, JsonParse::COMMENTS);
|
2015-12-01 18:01:47 +08:00
|
|
|
if (!err_failing_comment.empty()) {
|
|
|
|
printf("Failed: %s\n", err_failing_comment.c_str());
|
|
|
|
} else {
|
|
|
|
printf("Result: %s\n", json_failing_comment.dump().c_str());
|
|
|
|
}
|
|
|
|
|
2013-10-02 10:04:32 +08:00
|
|
|
std::list<int> l1 { 1, 2, 3 };
|
|
|
|
std::vector<int> l2 { 1, 2, 3 };
|
|
|
|
std::set<int> l3 { 1, 2, 3 };
|
2016-07-29 13:27:43 +08:00
|
|
|
JSON11_TEST_ASSERT(Json(l1) == Json(l2));
|
|
|
|
JSON11_TEST_ASSERT(Json(l2) == Json(l3));
|
2013-10-02 10:04:32 +08:00
|
|
|
|
|
|
|
std::map<string, string> m1 { { "k1", "v1" }, { "k2", "v2" } };
|
|
|
|
std::unordered_map<string, string> m2 { { "k1", "v1" }, { "k2", "v2" } };
|
2016-07-29 13:27:43 +08:00
|
|
|
JSON11_TEST_ASSERT(Json(m1) == Json(m2));
|
2013-10-02 10:04:32 +08:00
|
|
|
|
2013-09-04 05:45:11 +08:00
|
|
|
// Json literals
|
|
|
|
Json obj = Json::object({
|
|
|
|
{ "k1", "v1" },
|
|
|
|
{ "k2", 42.0 },
|
|
|
|
{ "k3", Json::array({ "a", 123.0, true, false, nullptr }) },
|
|
|
|
});
|
|
|
|
|
|
|
|
std::cout << "obj: " << obj.dump() << "\n";
|
|
|
|
|
2016-07-29 13:27:43 +08:00
|
|
|
JSON11_TEST_ASSERT(Json("a").number_value() == 0);
|
|
|
|
JSON11_TEST_ASSERT(Json("a").string_value() == "a");
|
|
|
|
JSON11_TEST_ASSERT(Json().number_value() == 0);
|
2013-09-04 05:45:11 +08:00
|
|
|
|
2016-07-29 13:27:43 +08:00
|
|
|
JSON11_TEST_ASSERT(obj == json);
|
|
|
|
JSON11_TEST_ASSERT(Json(42) == Json(42.0));
|
|
|
|
JSON11_TEST_ASSERT(Json(42) != Json(42.1));
|
2013-09-04 05:45:11 +08:00
|
|
|
|
|
|
|
const string unicode_escape_test =
|
|
|
|
R"([ "blah\ud83d\udca9blah\ud83dblah\udca9blah\u0000blah\u1234" ])";
|
|
|
|
|
|
|
|
const char utf8[] = "blah" "\xf0\x9f\x92\xa9" "blah" "\xed\xa0\xbd" "blah"
|
|
|
|
"\xed\xb2\xa9" "blah" "\0" "blah" "\xe1\x88\xb4";
|
|
|
|
|
|
|
|
Json uni = Json::parse(unicode_escape_test, err);
|
2016-07-29 13:27:43 +08:00
|
|
|
JSON11_TEST_ASSERT(uni[0].string_value().size() == (sizeof utf8) - 1);
|
|
|
|
JSON11_TEST_ASSERT(std::memcmp(uni[0].string_value().data(), utf8, sizeof utf8) == 0);
|
2013-09-04 05:45:11 +08:00
|
|
|
|
2016-05-19 09:36:58 +08:00
|
|
|
// Demonstrates the behavior change in Xcode 7 / Clang 3.7, introduced by DR1467
|
|
|
|
// and described here: https://llvm.org/bugs/show_bug.cgi?id=23812
|
2016-07-29 13:27:43 +08:00
|
|
|
if (JSON11_ENABLE_DR1467_CANARY) {
|
2016-05-19 09:36:58 +08:00
|
|
|
Json nested_array = Json::array { Json::array { 1, 2, 3 } };
|
2016-07-29 13:27:43 +08:00
|
|
|
JSON11_TEST_ASSERT(nested_array.is_array());
|
|
|
|
JSON11_TEST_ASSERT(nested_array.array_items().size() == 1);
|
|
|
|
JSON11_TEST_ASSERT(nested_array.array_items()[0].is_array());
|
|
|
|
JSON11_TEST_ASSERT(nested_array.array_items()[0].array_items().size() == 3);
|
2016-05-19 09:36:58 +08:00
|
|
|
}
|
2015-06-18 06:02:22 +08:00
|
|
|
|
2016-04-27 17:47:30 +08:00
|
|
|
{
|
|
|
|
const std::string good_json = R"( {"k1" : "v1"})";
|
|
|
|
const std::string bad_json1 = good_json + " {";
|
|
|
|
const std::string bad_json2 = good_json + R"({"k2":"v2", "k3":[)";
|
|
|
|
struct TestMultiParse {
|
|
|
|
std::string input;
|
|
|
|
std::string::size_type expect_parser_stop_pos;
|
|
|
|
size_t expect_not_empty_elms_count;
|
|
|
|
Json expect_parse_res;
|
|
|
|
} tests[] = {
|
|
|
|
{" {", 0, 0, {}},
|
|
|
|
{good_json, good_json.size(), 1, Json(std::map<string, string>{ { "k1", "v1" } })},
|
|
|
|
{bad_json1, good_json.size() + 1, 1, Json(std::map<string, string>{ { "k1", "v1" } })},
|
|
|
|
{bad_json2, good_json.size(), 1, Json(std::map<string, string>{ { "k1", "v1" } })},
|
|
|
|
{"{}", 2, 1, Json::object{}},
|
|
|
|
};
|
|
|
|
for (const auto &tst : tests) {
|
|
|
|
std::string::size_type parser_stop_pos;
|
|
|
|
std::string err;
|
|
|
|
auto res = Json::parse_multi(tst.input, parser_stop_pos, err);
|
2016-07-29 13:27:43 +08:00
|
|
|
JSON11_TEST_ASSERT(parser_stop_pos == tst.expect_parser_stop_pos);
|
|
|
|
JSON11_TEST_ASSERT(
|
2016-05-19 09:31:52 +08:00
|
|
|
(size_t)std::count_if(res.begin(), res.end(),
|
|
|
|
[](const Json& j) { return !j.is_null(); })
|
2016-04-27 17:47:30 +08:00
|
|
|
== tst.expect_not_empty_elms_count);
|
|
|
|
if (!res.empty()) {
|
2016-07-29 13:27:43 +08:00
|
|
|
JSON11_TEST_ASSERT(tst.expect_parse_res == res[0]);
|
2016-04-27 17:47:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-04 05:45:11 +08:00
|
|
|
Json my_json = Json::object {
|
|
|
|
{ "key1", "value1" },
|
|
|
|
{ "key2", false },
|
|
|
|
{ "key3", Json::array { 1, 2, 3 } },
|
|
|
|
};
|
|
|
|
std::string json_str = my_json.dump();
|
|
|
|
printf("%s\n", json_str.c_str());
|
|
|
|
|
|
|
|
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();
|
|
|
|
printf("%s\n", points_json.c_str());
|
|
|
|
}
|
2016-07-29 13:27:43 +08:00
|
|
|
|
2016-07-29 13:42:01 +08:00
|
|
|
#if JSON11_TEST_STANDALONE_MAIN
|
2016-07-29 13:27:43 +08:00
|
|
|
|
|
|
|
static void parse_from_stdin() {
|
|
|
|
string buf;
|
|
|
|
string line;
|
|
|
|
while (std::getline(std::cin, line)) {
|
|
|
|
buf += line + "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
string err;
|
|
|
|
auto json = Json::parse(buf, err);
|
|
|
|
if (!err.empty()) {
|
|
|
|
printf("Failed: %s\n", err.c_str());
|
|
|
|
} else {
|
|
|
|
printf("Result: %s\n", json.dump().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
if (argc == 2 && argv[1] == string("--stdin")) {
|
|
|
|
parse_from_stdin();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
json11_test();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // JSON11_TEST_STANDALONE_MAIN
|
|
|
|
|
|
|
|
// Insert user-defined suffix code (function definitions, etc)
|
|
|
|
// to set up a custom test suite
|
|
|
|
JSON11_TEST_CPP_SUFFIX_CODE
|