2016-08-04 19:55:47 +00:00
|
|
|
/*
|
|
|
|
__ _____ _____ _____
|
|
|
|
__| | __| | | | JSON for Modern C++ (test suite)
|
2017-02-25 15:34:38 +00:00
|
|
|
| | |__ | | | | | | version 2.1.1
|
2016-08-04 19:55:47 +00:00
|
|
|
|_____|_____|_____|_|___| https://github.com/nlohmann/json
|
|
|
|
|
|
|
|
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
|
2017-01-02 08:40:00 +00:00
|
|
|
Copyright (c) 2013-2017 Niels Lohmann <http://nlohmann.me>.
|
2016-08-04 19:55:47 +00:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "catch.hpp"
|
|
|
|
|
2017-03-11 18:26:12 +00:00
|
|
|
#define private public
|
2016-08-04 19:55:47 +00:00
|
|
|
#include "json.hpp"
|
|
|
|
using nlohmann::json;
|
|
|
|
|
2016-09-11 20:30:08 +00:00
|
|
|
#include <fstream>
|
2017-03-12 14:20:17 +00:00
|
|
|
#include <list>
|
2017-05-22 20:49:39 +00:00
|
|
|
#include <cstdio>
|
2016-09-11 20:30:08 +00:00
|
|
|
|
2016-08-04 19:55:47 +00:00
|
|
|
TEST_CASE("regression tests")
|
|
|
|
{
|
|
|
|
SECTION("issue #60 - Double quotation mark is not parsed correctly")
|
|
|
|
{
|
|
|
|
SECTION("escape_dobulequote")
|
|
|
|
{
|
|
|
|
auto s = "[\"\\\"foo\\\"\"]";
|
|
|
|
json j = json::parse(s);
|
|
|
|
auto expected = R"(["\"foo\""])"_json;
|
|
|
|
CHECK(j == expected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("issue #70 - Handle infinity and NaN cases")
|
|
|
|
{
|
2017-03-12 17:38:05 +00:00
|
|
|
/*
|
2016-08-04 19:55:47 +00:00
|
|
|
SECTION("NAN value")
|
|
|
|
{
|
|
|
|
CHECK(json(NAN) == json());
|
|
|
|
CHECK(json(json::number_float_t(NAN)) == json());
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("infinity")
|
|
|
|
{
|
|
|
|
CHECK(json(INFINITY) == json());
|
|
|
|
CHECK(json(json::number_float_t(INFINITY)) == json());
|
|
|
|
}
|
2017-03-12 17:38:05 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
// With 3.0.0, the semantics of this changed: NAN and infinity are
|
|
|
|
// stored properly inside the JSON value (no exception or conversion
|
|
|
|
// to null), but are serialized as null.
|
|
|
|
SECTION("NAN value")
|
|
|
|
{
|
|
|
|
json j1 = NAN;
|
|
|
|
CHECK(j1.is_number_float());
|
|
|
|
json::number_float_t f1 = j1;
|
|
|
|
CHECK(std::isnan(f1));
|
|
|
|
|
|
|
|
json j2 = json::number_float_t(NAN);
|
|
|
|
CHECK(j2.is_number_float());
|
|
|
|
json::number_float_t f2 = j2;
|
|
|
|
CHECK(std::isnan(f2));
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("infinity")
|
|
|
|
{
|
|
|
|
json j1 = INFINITY;
|
|
|
|
CHECK(j1.is_number_float());
|
|
|
|
json::number_float_t f1 = j1;
|
|
|
|
CHECK(not std::isfinite(f1));
|
|
|
|
|
|
|
|
json j2 = json::number_float_t(INFINITY);
|
|
|
|
CHECK(j2.is_number_float());
|
|
|
|
json::number_float_t f2 = j2;
|
|
|
|
CHECK(not std::isfinite(f2));
|
|
|
|
}
|
2016-08-04 19:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("pull request #71 - handle enum type")
|
|
|
|
{
|
2017-02-26 10:50:52 +00:00
|
|
|
enum { t = 0, u = 102};
|
2016-08-04 19:55:47 +00:00
|
|
|
json j = json::array();
|
|
|
|
j.push_back(t);
|
|
|
|
|
2017-01-08 13:07:10 +00:00
|
|
|
// maybe this is not the place to test this?
|
|
|
|
json j2 = u;
|
|
|
|
|
|
|
|
auto anon_enum_value = j2.get<decltype(u)>();
|
|
|
|
CHECK(u == anon_enum_value);
|
|
|
|
|
2017-02-26 10:50:52 +00:00
|
|
|
// check if the actual value was stored
|
|
|
|
CHECK(j2 == 102);
|
|
|
|
|
2017-01-08 13:07:10 +00:00
|
|
|
static_assert(std::is_same<decltype(anon_enum_value), decltype(u)>::value, "");
|
|
|
|
|
2016-08-04 19:55:47 +00:00
|
|
|
j.push_back(json::object(
|
|
|
|
{
|
|
|
|
{"game_type", t}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("issue #76 - dump() / parse() not idempotent")
|
|
|
|
{
|
|
|
|
// create JSON object
|
|
|
|
json fields;
|
|
|
|
fields["one"] = std::string("one");
|
|
|
|
fields["two"] = std::string("two three");
|
|
|
|
fields["three"] = std::string("three \"four\"");
|
|
|
|
|
|
|
|
// create another JSON object by deserializing the serialization
|
|
|
|
std::string payload = fields.dump();
|
|
|
|
json parsed_fields = json::parse(payload);
|
|
|
|
|
|
|
|
// check individual fields to match both objects
|
|
|
|
CHECK(parsed_fields["one"] == fields["one"]);
|
|
|
|
CHECK(parsed_fields["two"] == fields["two"]);
|
|
|
|
CHECK(parsed_fields["three"] == fields["three"]);
|
|
|
|
|
|
|
|
// check individual fields to match original input
|
|
|
|
CHECK(parsed_fields["one"] == std::string("one"));
|
|
|
|
CHECK(parsed_fields["two"] == std::string("two three"));
|
|
|
|
CHECK(parsed_fields["three"] == std::string("three \"four\""));
|
|
|
|
|
|
|
|
// check equality of the objects
|
|
|
|
CHECK(parsed_fields == fields);
|
|
|
|
|
|
|
|
// check equality of the serialized objects
|
|
|
|
CHECK(fields.dump() == parsed_fields.dump());
|
|
|
|
|
|
|
|
// check everything in one line
|
|
|
|
CHECK(fields == json::parse(fields.dump()));
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("issue #82 - lexer::get_number return NAN")
|
|
|
|
{
|
|
|
|
const auto content = R"(
|
|
|
|
{
|
|
|
|
"Test":"Test1",
|
|
|
|
"Number":100,
|
|
|
|
"Foo":42.42
|
|
|
|
})";
|
|
|
|
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << content;
|
|
|
|
json j;
|
|
|
|
ss >> j;
|
|
|
|
|
|
|
|
std::string test = j["Test"];
|
|
|
|
CHECK(test == "Test1");
|
|
|
|
int number = j["Number"];
|
|
|
|
CHECK(number == 100);
|
|
|
|
float foo = j["Foo"];
|
|
|
|
CHECK(foo == Approx(42.42));
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("issue #89 - nonstandard integer type")
|
|
|
|
{
|
|
|
|
// create JSON class with nonstandard integer number type
|
|
|
|
using custom_json =
|
|
|
|
nlohmann::basic_json<std::map, std::vector, std::string, bool, int32_t, uint32_t, float>;
|
|
|
|
custom_json j;
|
|
|
|
j["int_1"] = 1;
|
|
|
|
// we need to cast to int to compile with Catch - the value is int32_t
|
|
|
|
CHECK(static_cast<int>(j["int_1"]) == 1);
|
|
|
|
|
|
|
|
// tests for correct handling of non-standard integers that overflow the type selected by the user
|
|
|
|
|
|
|
|
// unsigned integer object creation - expected to wrap and still be stored as an integer
|
|
|
|
j = 4294967296U; // 2^32
|
|
|
|
CHECK(static_cast<int>(j.type()) == static_cast<int>(custom_json::value_t::number_unsigned));
|
|
|
|
CHECK(j.get<uint32_t>() == 0); // Wrap
|
|
|
|
|
|
|
|
// unsigned integer parsing - expected to overflow and be stored as a float
|
|
|
|
j = custom_json::parse("4294967296"); // 2^32
|
|
|
|
CHECK(static_cast<int>(j.type()) == static_cast<int>(custom_json::value_t::number_float));
|
|
|
|
CHECK(j.get<float>() == 4294967296.0f);
|
|
|
|
|
|
|
|
// integer object creation - expected to wrap and still be stored as an integer
|
|
|
|
j = -2147483649LL; // -2^31-1
|
|
|
|
CHECK(static_cast<int>(j.type()) == static_cast<int>(custom_json::value_t::number_integer));
|
|
|
|
CHECK(j.get<int32_t>() == 2147483647); // Wrap
|
|
|
|
|
|
|
|
// integer parsing - expected to overflow and be stored as a float with rounding
|
|
|
|
j = custom_json::parse("-2147483649"); // -2^31
|
|
|
|
CHECK(static_cast<int>(j.type()) == static_cast<int>(custom_json::value_t::number_float));
|
|
|
|
CHECK(j.get<float>() == -2147483650.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("issue #93 reverse_iterator operator inheritance problem")
|
|
|
|
{
|
|
|
|
{
|
|
|
|
json a = {1, 2, 3};
|
|
|
|
json::reverse_iterator rit = a.rbegin();
|
|
|
|
++rit;
|
|
|
|
CHECK(*rit == json(2));
|
|
|
|
CHECK(rit.value() == json(2));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
json a = {1, 2, 3};
|
|
|
|
json::reverse_iterator rit = ++a.rbegin();
|
2017-03-26 15:26:41 +00:00
|
|
|
CHECK(*rit == json(2));
|
2017-03-28 22:39:47 +00:00
|
|
|
CHECK(rit.value() == json(2));
|
2016-08-04 19:55:47 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
json a = {1, 2, 3};
|
|
|
|
json::reverse_iterator rit = a.rbegin();
|
|
|
|
++rit;
|
|
|
|
json b = {0, 0, 0};
|
|
|
|
std::transform(rit, a.rend(), b.rbegin(), [](json el)
|
|
|
|
{
|
|
|
|
return el;
|
|
|
|
});
|
|
|
|
CHECK(b == json({0, 1, 2}));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
json a = {1, 2, 3};
|
|
|
|
json b = {0, 0, 0};
|
|
|
|
std::transform(++a.rbegin(), a.rend(), b.rbegin(), [](json el)
|
|
|
|
{
|
|
|
|
return el;
|
|
|
|
});
|
|
|
|
CHECK(b == json({0, 1, 2}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("issue #100 - failed to iterator json object with reverse_iterator")
|
|
|
|
{
|
|
|
|
json config =
|
|
|
|
{
|
|
|
|
{ "111", 111 },
|
|
|
|
{ "112", 112 },
|
|
|
|
{ "113", 113 }
|
|
|
|
};
|
|
|
|
|
|
|
|
std::stringstream ss;
|
|
|
|
|
|
|
|
for (auto it = config.begin(); it != config.end(); ++it)
|
|
|
|
{
|
|
|
|
ss << it.key() << ": " << it.value() << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto it = config.rbegin(); it != config.rend(); ++it)
|
|
|
|
{
|
|
|
|
ss << it.key() << ": " << it.value() << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK(ss.str() == "111: 111\n112: 112\n113: 113\n113: 113\n112: 112\n111: 111\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("issue #101 - binary string causes numbers to be dumped as hex")
|
|
|
|
{
|
|
|
|
int64_t number = 10;
|
|
|
|
std::string bytes{"\x00" "asdf\n", 6};
|
|
|
|
json j;
|
|
|
|
j["int64"] = number;
|
|
|
|
j["binary string"] = bytes;
|
|
|
|
// make sure the number is really printed as decimal "10" and not as
|
|
|
|
// hexadecimal "a"
|
|
|
|
CHECK(j.dump() == "{\"binary string\":\"\\u0000asdf\\n\",\"int64\":10}");
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("issue #111 - subsequent unicode chars")
|
|
|
|
{
|
|
|
|
std::string bytes{0x7, 0x7};
|
|
|
|
json j;
|
|
|
|
j["string"] = bytes;
|
|
|
|
CHECK(j["string"] == "\u0007\u0007");
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("issue #144 - implicit assignment to std::string fails")
|
|
|
|
{
|
|
|
|
json o = {{"name", "value"}};
|
|
|
|
|
|
|
|
std::string s1 = o["name"];
|
|
|
|
CHECK(s1 == "value");
|
|
|
|
|
|
|
|
std::string s2;
|
|
|
|
s2 = o["name"];
|
|
|
|
|
|
|
|
CHECK(s2 == "value");
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("issue #146 - character following a surrogate pair is skipped")
|
|
|
|
{
|
|
|
|
CHECK(json::parse("\"\\ud80c\\udc60abc\"").get<json::string_t>() == u8"\U00013060abc");
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("issue #171 - Cannot index by key of type static constexpr const char*")
|
|
|
|
{
|
|
|
|
json j;
|
|
|
|
|
|
|
|
// Non-const access with key as "char []"
|
|
|
|
char array_key[] = "Key1";
|
|
|
|
CHECK_NOTHROW(j[array_key] = 1);
|
|
|
|
CHECK(j[array_key] == json(1));
|
|
|
|
|
|
|
|
// Non-const access with key as "const char[]"
|
|
|
|
const char const_array_key[] = "Key2";
|
|
|
|
CHECK_NOTHROW(j[const_array_key] = 2);
|
|
|
|
CHECK(j[const_array_key] == json(2));
|
|
|
|
|
|
|
|
// Non-const access with key as "char *"
|
|
|
|
char _ptr_key[] = "Key3";
|
|
|
|
char* ptr_key = &_ptr_key[0];
|
|
|
|
CHECK_NOTHROW(j[ptr_key] = 3);
|
|
|
|
CHECK(j[ptr_key] == json(3));
|
|
|
|
|
|
|
|
// Non-const access with key as "const char *"
|
|
|
|
const char* const_ptr_key = "Key4";
|
|
|
|
CHECK_NOTHROW(j[const_ptr_key] = 4);
|
|
|
|
CHECK(j[const_ptr_key] == json(4));
|
|
|
|
|
|
|
|
// Non-const access with key as "static constexpr const char *"
|
|
|
|
static constexpr const char* constexpr_ptr_key = "Key5";
|
|
|
|
CHECK_NOTHROW(j[constexpr_ptr_key] = 5);
|
|
|
|
CHECK(j[constexpr_ptr_key] == json(5));
|
|
|
|
|
|
|
|
const json j_const = j;
|
|
|
|
|
|
|
|
// Const access with key as "char []"
|
|
|
|
CHECK(j_const[array_key] == json(1));
|
|
|
|
|
|
|
|
// Const access with key as "const char[]"
|
|
|
|
CHECK(j_const[const_array_key] == json(2));
|
|
|
|
|
|
|
|
// Const access with key as "char *"
|
|
|
|
CHECK(j_const[ptr_key] == json(3));
|
|
|
|
|
|
|
|
// Const access with key as "const char *"
|
|
|
|
CHECK(j_const[const_ptr_key] == json(4));
|
|
|
|
|
|
|
|
// Const access with key as "static constexpr const char *"
|
|
|
|
CHECK(j_const[constexpr_ptr_key] == json(5));
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("issue #186 miloyip/nativejson-benchmark: floating-point parsing")
|
|
|
|
{
|
|
|
|
json j;
|
|
|
|
|
|
|
|
j = json::parse("-0.0");
|
|
|
|
CHECK(j.get<double>() == -0.0);
|
|
|
|
|
|
|
|
j = json::parse("2.22507385850720113605740979670913197593481954635164564e-308");
|
|
|
|
CHECK(j.get<double>() == 2.2250738585072009e-308);
|
|
|
|
|
|
|
|
j = json::parse("0.999999999999999944488848768742172978818416595458984374");
|
|
|
|
CHECK(j.get<double>() == 0.99999999999999989);
|
|
|
|
|
|
|
|
j = json::parse("1.00000000000000011102230246251565404236316680908203126");
|
|
|
|
CHECK(j.get<double>() == 1.00000000000000022);
|
|
|
|
|
|
|
|
j = json::parse("7205759403792793199999e-5");
|
|
|
|
CHECK(j.get<double>() == 72057594037927928.0);
|
|
|
|
|
|
|
|
j = json::parse("922337203685477529599999e-5");
|
|
|
|
CHECK(j.get<double>() == 9223372036854774784.0);
|
|
|
|
|
|
|
|
j = json::parse("1014120480182583464902367222169599999e-5");
|
|
|
|
CHECK(j.get<double>() == 10141204801825834086073718800384.0);
|
|
|
|
|
|
|
|
j = json::parse("5708990770823839207320493820740630171355185151999e-3");
|
|
|
|
CHECK(j.get<double>() == 5708990770823838890407843763683279797179383808.0);
|
|
|
|
|
|
|
|
// create JSON class with nonstandard float number type
|
|
|
|
|
|
|
|
// float
|
|
|
|
nlohmann::basic_json<std::map, std::vector, std::string, bool, int32_t, uint32_t, float> j_float =
|
|
|
|
1.23e25f;
|
|
|
|
CHECK(j_float.get<float>() == 1.23e25f);
|
|
|
|
|
|
|
|
// double
|
|
|
|
nlohmann::basic_json<std::map, std::vector, std::string, bool, int64_t, uint64_t, double> j_double =
|
2017-02-22 17:14:29 +00:00
|
|
|
1.23e35;
|
|
|
|
CHECK(j_double.get<double>() == 1.23e35);
|
2016-08-04 19:55:47 +00:00
|
|
|
|
|
|
|
// long double
|
|
|
|
nlohmann::basic_json<std::map, std::vector, std::string, bool, int64_t, uint64_t, long double>
|
|
|
|
j_long_double = 1.23e45L;
|
|
|
|
CHECK(j_long_double.get<long double>() == 1.23e45L);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("issue #228 - double values are serialized with commas as decimal points")
|
|
|
|
{
|
2017-02-19 19:08:01 +00:00
|
|
|
json j1a = 2312.42;
|
|
|
|
json j1b = json::parse("2312.42");
|
2016-08-04 19:55:47 +00:00
|
|
|
|
|
|
|
json j2a = 2342e-2;
|
|
|
|
//issue #230
|
|
|
|
//json j2b = json::parse("2342e-2");
|
|
|
|
|
|
|
|
json j3a = 10E3;
|
|
|
|
json j3b = json::parse("10E3");
|
|
|
|
json j3c = json::parse("10e3");
|
|
|
|
|
|
|
|
// class to create a locale that would use a comma for decimals
|
|
|
|
class CommaDecimalSeparator : public std::numpunct<char>
|
|
|
|
{
|
|
|
|
protected:
|
2017-03-16 17:39:33 +00:00
|
|
|
char do_decimal_point() const override
|
2016-08-04 19:55:47 +00:00
|
|
|
{
|
|
|
|
return ',';
|
|
|
|
}
|
2017-02-19 19:08:01 +00:00
|
|
|
|
2017-03-16 17:39:33 +00:00
|
|
|
char do_thousands_sep() const override
|
2017-02-19 19:08:01 +00:00
|
|
|
{
|
|
|
|
return '.';
|
|
|
|
}
|
2017-02-19 20:17:05 +00:00
|
|
|
|
2017-03-16 17:39:33 +00:00
|
|
|
std::string do_grouping() const override
|
2017-02-19 19:08:01 +00:00
|
|
|
{
|
|
|
|
return "\03";
|
|
|
|
}
|
2016-08-04 19:55:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// change locale to mess with decimal points
|
2016-12-04 00:05:09 +00:00
|
|
|
auto orig_locale = std::locale::global(std::locale(std::locale(), new CommaDecimalSeparator));
|
2016-08-04 19:55:47 +00:00
|
|
|
|
2017-02-19 19:08:01 +00:00
|
|
|
CHECK(j1a.dump() == "2312.42");
|
|
|
|
CHECK(j1b.dump() == "2312.42");
|
2016-08-04 19:55:47 +00:00
|
|
|
|
|
|
|
// check if locale is properly reset
|
|
|
|
std::stringstream ss;
|
|
|
|
ss.imbue(std::locale(std::locale(), new CommaDecimalSeparator));
|
2017-02-19 19:08:01 +00:00
|
|
|
ss << 4712.11;
|
|
|
|
CHECK(ss.str() == "4.712,11");
|
2016-08-04 19:55:47 +00:00
|
|
|
ss << j1a;
|
2017-02-19 19:08:01 +00:00
|
|
|
CHECK(ss.str() == "4.712,112312.42");
|
2016-08-04 19:55:47 +00:00
|
|
|
ss << 47.11;
|
2017-02-19 19:08:01 +00:00
|
|
|
CHECK(ss.str() == "4.712,112312.4247,11");
|
2016-08-04 19:55:47 +00:00
|
|
|
|
|
|
|
CHECK(j2a.dump() == "23.42");
|
|
|
|
//issue #230
|
|
|
|
//CHECK(j2b.dump() == "23.42");
|
|
|
|
|
2016-12-04 06:38:23 +00:00
|
|
|
CHECK(j3a.dump() == "10000.0");
|
|
|
|
CHECK(j3b.dump() == "10000.0");
|
|
|
|
CHECK(j3c.dump() == "10000.0");
|
2016-08-04 19:55:47 +00:00
|
|
|
//CHECK(j3b.dump() == "1E04"); // roundtrip error
|
|
|
|
//CHECK(j3c.dump() == "1e04"); // roundtrip error
|
2016-12-04 00:05:09 +00:00
|
|
|
|
|
|
|
std::locale::global(orig_locale);
|
2016-08-04 19:55:47 +00:00
|
|
|
}
|
|
|
|
|
2016-12-08 01:23:25 +00:00
|
|
|
SECTION("issue #378 - locale-independent num-to-str")
|
|
|
|
{
|
2016-12-09 03:39:38 +00:00
|
|
|
setlocale(LC_NUMERIC, "de_DE.UTF-8");
|
2016-12-08 01:23:25 +00:00
|
|
|
|
|
|
|
// verify that dumped correctly with '.' and no grouping
|
|
|
|
const json j1 = 12345.67;
|
2016-12-09 03:39:38 +00:00
|
|
|
CHECK(json(12345.67).dump() == "12345.67");
|
|
|
|
setlocale(LC_NUMERIC, "C");
|
2016-12-08 01:23:25 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 00:45:48 +00:00
|
|
|
SECTION("issue #379 - locale-independent str-to-num")
|
|
|
|
{
|
2016-12-09 03:13:05 +00:00
|
|
|
setlocale(LC_NUMERIC, "de_DE.UTF-8");
|
2016-12-07 00:45:48 +00:00
|
|
|
|
|
|
|
// verify that parsed correctly despite using strtod internally
|
2016-12-09 03:13:05 +00:00
|
|
|
CHECK(json::parse("3.14").get<double>() == 3.14);
|
2016-12-07 00:45:48 +00:00
|
|
|
|
|
|
|
// check a different code path
|
2016-12-09 03:13:05 +00:00
|
|
|
CHECK(json::parse("1.000000000000000000000000000000000000000000000000000000000000000000000000").get<double>() == 1.0);
|
2016-12-07 00:45:48 +00:00
|
|
|
}
|
|
|
|
|
2016-08-04 19:55:47 +00:00
|
|
|
SECTION("issue #233 - Can't use basic_json::iterator as a base iterator for std::move_iterator")
|
|
|
|
{
|
|
|
|
json source = {"a", "b", "c"};
|
|
|
|
json expected = {"a", "b"};
|
|
|
|
json dest;
|
|
|
|
|
|
|
|
std::copy_n(std::make_move_iterator(source.begin()), 2, std::back_inserter(dest));
|
|
|
|
|
|
|
|
CHECK(dest == expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("issue #235 - ambiguous overload for 'push_back' and 'operator+='")
|
|
|
|
{
|
|
|
|
json data = {{"key", "value"}};
|
|
|
|
data.push_back({"key2", "value2"});
|
|
|
|
data += {"key3", "value3"};
|
|
|
|
|
|
|
|
CHECK(data == json({{"key", "value"}, {"key2", "value2"}, {"key3", "value3"}}));
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("issue #269 - diff generates incorrect patch when removing multiple array elements")
|
|
|
|
{
|
|
|
|
json doc = R"( { "arr1": [1, 2, 3, 4] } )"_json;
|
|
|
|
json expected = R"( { "arr1": [1, 2] } )"_json;
|
|
|
|
|
|
|
|
// check roundtrip
|
|
|
|
CHECK(doc.patch(json::diff(doc, expected)) == expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("issue #283 - value() does not work with _json_pointer types")
|
|
|
|
{
|
|
|
|
json j =
|
|
|
|
{
|
|
|
|
{"object", {{"key1", 1}, {"key2", 2}}},
|
|
|
|
};
|
|
|
|
|
|
|
|
int at_integer = j.at("/object/key2"_json_pointer);
|
|
|
|
int val_integer = j.value("/object/key2"_json_pointer, 0);
|
|
|
|
|
|
|
|
CHECK(at_integer == val_integer);
|
|
|
|
}
|
2016-09-11 20:30:08 +00:00
|
|
|
|
2016-09-11 20:40:51 +00:00
|
|
|
SECTION("issue #304 - Unused variable warning")
|
|
|
|
{
|
|
|
|
// code triggered a "warning: unused variable" warning and is left
|
|
|
|
// here to avoid the warning in the future
|
|
|
|
json object;
|
|
|
|
json patch = json::array();
|
|
|
|
object = object.patch(patch);
|
|
|
|
}
|
|
|
|
|
2016-09-11 20:30:08 +00:00
|
|
|
SECTION("issue #306 - Parsing fails without space at end of file")
|
|
|
|
{
|
|
|
|
for (auto filename :
|
|
|
|
{
|
|
|
|
"test/data/regression/broken_file.json",
|
|
|
|
"test/data/regression/working_file.json"
|
|
|
|
})
|
|
|
|
{
|
|
|
|
CAPTURE(filename);
|
|
|
|
json j;
|
|
|
|
std::ifstream f(filename);
|
2017-03-28 22:39:47 +00:00
|
|
|
CHECK_NOTHROW(f >> j);
|
2016-09-11 20:30:08 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-14 20:48:12 +00:00
|
|
|
|
|
|
|
SECTION("issue #310 - make json_benchmarks no longer working in 2.0.4")
|
|
|
|
{
|
|
|
|
for (auto filename :
|
|
|
|
{
|
|
|
|
"test/data/regression/floats.json",
|
|
|
|
"test/data/regression/signed_ints.json",
|
|
|
|
"test/data/regression/unsigned_ints.json"
|
|
|
|
})
|
|
|
|
{
|
|
|
|
CAPTURE(filename);
|
|
|
|
json j;
|
|
|
|
std::ifstream f(filename);
|
2017-03-28 22:39:47 +00:00
|
|
|
CHECK_NOTHROW(f >> j);
|
2016-09-14 20:48:12 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-08 12:27:28 +00:00
|
|
|
|
|
|
|
SECTION("issue #323 - add nested object capabilities to pointers")
|
|
|
|
{
|
|
|
|
json j;
|
2016-10-15 14:47:45 +00:00
|
|
|
j["/this/that/2"_json_pointer] = 27;
|
|
|
|
CHECK(j == json({{"this", {{"that", {nullptr, nullptr, 27}}}}}));
|
2016-10-08 12:27:28 +00:00
|
|
|
}
|
2016-10-13 19:00:48 +00:00
|
|
|
|
|
|
|
SECTION("issue #329 - serialized value not always can be parsed")
|
|
|
|
{
|
2017-03-12 19:59:33 +00:00
|
|
|
CHECK_THROWS_AS(json::parse("22e2222"), json::out_of_range);
|
|
|
|
CHECK_THROWS_WITH(json::parse("22e2222"),
|
2017-03-14 15:07:28 +00:00
|
|
|
"[json.exception.out_of_range.406] number overflow parsing '22e2222'");
|
2016-10-13 19:00:48 +00:00
|
|
|
}
|
2016-11-23 15:57:01 +00:00
|
|
|
|
|
|
|
SECTION("issue #366 - json::parse on failed stream gets stuck")
|
|
|
|
{
|
|
|
|
std::ifstream f("file_not_found.json");
|
2017-03-06 21:37:46 +00:00
|
|
|
CHECK_THROWS_AS(json::parse(f), json::parse_error);
|
|
|
|
CHECK_THROWS_WITH(json::parse(f), "[json.exception.parse_error.111] parse error: bad input stream");
|
2016-11-23 15:57:01 +00:00
|
|
|
}
|
2016-11-25 16:39:24 +00:00
|
|
|
|
2016-11-24 21:05:29 +00:00
|
|
|
SECTION("issue #367 - calling stream at EOF")
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
json j;
|
|
|
|
ss << "123";
|
2017-03-28 22:39:47 +00:00
|
|
|
CHECK_NOTHROW(ss >> j);
|
2016-11-24 21:05:29 +00:00
|
|
|
|
|
|
|
// see https://github.com/nlohmann/json/issues/367#issuecomment-262841893:
|
|
|
|
// ss is not at EOF; this yielded an error before the fix
|
|
|
|
// (threw basic_string::append). No, it should just throw
|
|
|
|
// a parse error because of the EOF.
|
2017-03-28 22:39:47 +00:00
|
|
|
CHECK_THROWS_AS(ss >> j, json::parse_error);
|
|
|
|
CHECK_THROWS_WITH(ss >> j,
|
2017-03-29 05:54:26 +00:00
|
|
|
"[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input");
|
2016-11-24 21:05:29 +00:00
|
|
|
}
|
2016-12-12 22:19:43 +00:00
|
|
|
|
2017-05-21 14:36:51 +00:00
|
|
|
SECTION("issue #367 - behavior of operator>> should more closely resemble that of built-in overloads")
|
|
|
|
{
|
|
|
|
SECTION("(empty)")
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
json j;
|
|
|
|
CHECK_THROWS_AS(ss >> j, json::parse_error);
|
|
|
|
CHECK_THROWS_WITH(ss >> j,
|
|
|
|
"[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input");
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("(whitespace)")
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << " ";
|
|
|
|
json j;
|
|
|
|
CHECK_THROWS_AS(ss >> j, json::parse_error);
|
|
|
|
CHECK_THROWS_WITH(ss >> j,
|
|
|
|
"[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input");
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("one value")
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "111";
|
|
|
|
json j;
|
|
|
|
CHECK_NOTHROW(ss >> j);
|
|
|
|
CHECK(j == 111);
|
|
|
|
|
|
|
|
CHECK_THROWS_AS(ss >> j, json::parse_error);
|
|
|
|
CHECK_THROWS_WITH(ss >> j,
|
|
|
|
"[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input");
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("one value + whitespace")
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "222 \t\n";
|
|
|
|
json j;
|
|
|
|
CHECK_NOTHROW(ss >> j);
|
|
|
|
CHECK(j == 222);
|
|
|
|
|
|
|
|
CHECK_THROWS_AS(ss >> j, json::parse_error);
|
|
|
|
CHECK_THROWS_WITH(ss >> j,
|
|
|
|
"[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input");
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("whitespace + one value")
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "\n\t 333";
|
|
|
|
json j;
|
|
|
|
CHECK_NOTHROW(ss >> j);
|
|
|
|
CHECK(j == 333);
|
|
|
|
|
|
|
|
CHECK_THROWS_AS(ss >> j, json::parse_error);
|
|
|
|
CHECK_THROWS_WITH(ss >> j,
|
|
|
|
"[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input");
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("three values")
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << " 111 \n222\n \n 333";
|
|
|
|
json j;
|
|
|
|
CHECK_NOTHROW(ss >> j);
|
|
|
|
CHECK(j == 111);
|
|
|
|
CHECK_NOTHROW(ss >> j);
|
|
|
|
CHECK(j == 222);
|
|
|
|
CHECK_NOTHROW(ss >> j);
|
|
|
|
CHECK(j == 333);
|
|
|
|
|
|
|
|
CHECK_THROWS_AS(ss >> j, json::parse_error);
|
|
|
|
CHECK_THROWS_WITH(ss >> j,
|
|
|
|
"[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input");
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("literals without whitespace")
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "truefalsenull\"\"";
|
|
|
|
json j;
|
|
|
|
CHECK_NOTHROW(ss >> j);
|
|
|
|
CHECK(j == true);
|
|
|
|
CHECK_NOTHROW(ss >> j);
|
|
|
|
CHECK(j == false);
|
|
|
|
CHECK_NOTHROW(ss >> j);
|
|
|
|
CHECK(j == nullptr);
|
|
|
|
CHECK_NOTHROW(ss >> j);
|
|
|
|
CHECK(j == "");
|
|
|
|
|
|
|
|
CHECK_THROWS_AS(ss >> j, json::parse_error);
|
|
|
|
CHECK_THROWS_WITH(ss >> j,
|
|
|
|
"[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input");
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("example from #529")
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "{\n \"one\" : 1,\n \"two\" : 2\n}\n{\n \"three\" : 3\n}";
|
|
|
|
json j;
|
|
|
|
CHECK_NOTHROW(ss >> j);
|
|
|
|
CHECK(j == json({{"one", 1}, {"two", 2}}));
|
|
|
|
CHECK_NOTHROW(ss >> j);
|
|
|
|
CHECK(j == json({{"three", 3}}));
|
|
|
|
|
|
|
|
CHECK_THROWS_AS(ss >> j, json::parse_error);
|
|
|
|
CHECK_THROWS_WITH(ss >> j,
|
|
|
|
"[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input");
|
|
|
|
}
|
2017-05-22 20:49:39 +00:00
|
|
|
|
2017-06-01 05:32:39 +00:00
|
|
|
/*
|
2017-05-22 20:49:39 +00:00
|
|
|
SECTION("second example from #529")
|
|
|
|
{
|
|
|
|
std::string str = "{\n\"one\" : 1,\n\"two\" : 2\n}\n{\n\"three\" : 3\n}";
|
|
|
|
|
|
|
|
{
|
|
|
|
std::ofstream file("test.json");
|
|
|
|
file << str;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ifstream stream("test.json", std::ifstream::in);
|
|
|
|
json val;
|
|
|
|
|
|
|
|
size_t i = 0;
|
|
|
|
while (stream.peek() != EOF)
|
|
|
|
{
|
|
|
|
CAPTURE(i);
|
|
|
|
CHECK_NOTHROW(stream >> val);
|
|
|
|
|
|
|
|
CHECK(i < 2);
|
|
|
|
|
|
|
|
if (i == 0)
|
|
|
|
{
|
|
|
|
CHECK(val == json({{"one", 1}, {"two", 2}}));
|
2017-05-22 20:59:57 +00:00
|
|
|
CHECK(static_cast<int>(stream.tellg()) == 28);
|
2017-05-22 20:49:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (i == 1)
|
|
|
|
{
|
|
|
|
CHECK(val == json({{"three", 3}}));
|
2017-05-22 20:59:57 +00:00
|
|
|
CHECK(static_cast<int>(stream.tellg()) == 44);
|
2017-05-22 20:49:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::remove("test.json");
|
|
|
|
}
|
2017-06-01 05:32:39 +00:00
|
|
|
*/
|
2017-05-21 14:36:51 +00:00
|
|
|
}
|
|
|
|
|
2016-12-12 22:19:43 +00:00
|
|
|
SECTION("issue #389 - Integer-overflow (OSS-Fuzz issue 267)")
|
|
|
|
{
|
|
|
|
// original test case
|
|
|
|
json j1 = json::parse("-9223372036854775808");
|
|
|
|
CHECK(j1.is_number_integer());
|
|
|
|
CHECK(j1.get<json::number_integer_t>() == INT64_MIN);
|
|
|
|
|
|
|
|
// edge case (+1; still an integer)
|
|
|
|
json j2 = json::parse("-9223372036854775807");
|
|
|
|
CHECK(j2.is_number_integer());
|
|
|
|
CHECK(j2.get<json::number_integer_t>() == INT64_MIN + 1);
|
|
|
|
|
|
|
|
// edge case (-1; overflow -> floats)
|
|
|
|
json j3 = json::parse("-9223372036854775809");
|
|
|
|
CHECK(j3.is_number_float());
|
|
|
|
}
|
2016-12-13 20:46:07 +00:00
|
|
|
|
|
|
|
SECTION("issue #380 - bug in overflow detection when parsing integers")
|
|
|
|
{
|
|
|
|
json j = json::parse("166020696663385964490");
|
|
|
|
CHECK(j.is_number_float());
|
|
|
|
CHECK(j.dump() == "1.66020696663386e+20");
|
|
|
|
}
|
2016-12-29 14:39:16 +00:00
|
|
|
|
|
|
|
SECTION("issue #405 - Heap-buffer-overflow (OSS-Fuzz issue 342)")
|
|
|
|
{
|
|
|
|
// original test case
|
|
|
|
std::vector<uint8_t> vec {0x65, 0xf5, 0x0a, 0x48, 0x21};
|
2017-03-08 17:07:21 +00:00
|
|
|
CHECK_THROWS_AS(json::from_cbor(vec), json::parse_error);
|
|
|
|
CHECK_THROWS_WITH(json::from_cbor(vec),
|
2017-04-02 16:46:21 +00:00
|
|
|
"[json.exception.parse_error.110] parse error at 6: unexpected end of input");
|
2016-12-29 14:39:16 +00:00
|
|
|
}
|
2016-12-29 15:14:15 +00:00
|
|
|
|
|
|
|
SECTION("issue #407 - Heap-buffer-overflow (OSS-Fuzz issue 343)")
|
|
|
|
{
|
|
|
|
// original test case: incomplete float64
|
|
|
|
std::vector<uint8_t> vec1 {0xcb, 0x8f, 0x0a};
|
2017-03-08 17:07:21 +00:00
|
|
|
CHECK_THROWS_AS(json::from_msgpack(vec1), json::parse_error);
|
|
|
|
CHECK_THROWS_WITH(json::from_msgpack(vec1),
|
2017-04-04 14:59:19 +00:00
|
|
|
"[json.exception.parse_error.110] parse error at 4: unexpected end of input");
|
2016-12-29 15:14:15 +00:00
|
|
|
|
|
|
|
// related test case: incomplete float32
|
|
|
|
std::vector<uint8_t> vec2 {0xca, 0x8f, 0x0a};
|
2017-03-08 17:07:21 +00:00
|
|
|
CHECK_THROWS_AS(json::from_msgpack(vec2), json::parse_error);
|
|
|
|
CHECK_THROWS_WITH(json::from_msgpack(vec2),
|
2017-04-04 14:59:19 +00:00
|
|
|
"[json.exception.parse_error.110] parse error at 4: unexpected end of input");
|
2016-12-29 15:14:15 +00:00
|
|
|
|
|
|
|
// related test case: incomplete Half-Precision Float (CBOR)
|
|
|
|
std::vector<uint8_t> vec3 {0xf9, 0x8f};
|
2017-03-08 17:07:21 +00:00
|
|
|
CHECK_THROWS_AS(json::from_cbor(vec3), json::parse_error);
|
|
|
|
CHECK_THROWS_WITH(json::from_cbor(vec3),
|
2017-04-02 16:46:21 +00:00
|
|
|
"[json.exception.parse_error.110] parse error at 3: unexpected end of input");
|
2016-12-29 15:14:15 +00:00
|
|
|
|
|
|
|
// related test case: incomplete Single-Precision Float (CBOR)
|
|
|
|
std::vector<uint8_t> vec4 {0xfa, 0x8f, 0x0a};
|
2017-03-08 17:07:21 +00:00
|
|
|
CHECK_THROWS_AS(json::from_cbor(vec4), json::parse_error);
|
|
|
|
CHECK_THROWS_WITH(json::from_cbor(vec4),
|
2017-04-02 16:46:21 +00:00
|
|
|
"[json.exception.parse_error.110] parse error at 4: unexpected end of input");
|
2016-12-29 15:14:15 +00:00
|
|
|
|
|
|
|
// related test case: incomplete Double-Precision Float (CBOR)
|
|
|
|
std::vector<uint8_t> vec5 {0xfb, 0x8f, 0x0a};
|
2017-03-08 17:07:21 +00:00
|
|
|
CHECK_THROWS_AS(json::from_cbor(vec5), json::parse_error);
|
|
|
|
CHECK_THROWS_WITH(json::from_cbor(vec5),
|
2017-04-02 16:46:21 +00:00
|
|
|
"[json.exception.parse_error.110] parse error at 4: unexpected end of input");
|
2016-12-29 15:14:15 +00:00
|
|
|
}
|
2016-12-29 16:00:02 +00:00
|
|
|
|
|
|
|
SECTION("issue #408 - Heap-buffer-overflow (OSS-Fuzz issue 344)")
|
|
|
|
{
|
|
|
|
// original test case
|
|
|
|
std::vector<uint8_t> vec1 {0x87};
|
2017-03-08 17:07:21 +00:00
|
|
|
CHECK_THROWS_AS(json::from_msgpack(vec1), json::parse_error);
|
|
|
|
CHECK_THROWS_WITH(json::from_msgpack(vec1),
|
2017-04-04 14:59:19 +00:00
|
|
|
"[json.exception.parse_error.110] parse error at 2: unexpected end of input");
|
2016-12-29 16:00:02 +00:00
|
|
|
|
|
|
|
// more test cases for MessagePack
|
2017-02-22 17:14:29 +00:00
|
|
|
for (auto b :
|
2016-12-29 16:00:02 +00:00
|
|
|
{
|
|
|
|
0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, // fixmap
|
|
|
|
0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, // fixarray
|
|
|
|
0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, // fixstr
|
|
|
|
0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf
|
|
|
|
})
|
|
|
|
{
|
2017-02-22 17:14:29 +00:00
|
|
|
std::vector<uint8_t> vec(1, static_cast<uint8_t>(b));
|
2017-03-08 17:07:21 +00:00
|
|
|
CHECK_THROWS_AS(json::from_msgpack(vec), json::parse_error);
|
2016-12-29 16:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// more test cases for CBOR
|
2017-02-22 17:14:29 +00:00
|
|
|
for (auto b :
|
2016-12-29 16:00:02 +00:00
|
|
|
{
|
|
|
|
0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
|
|
|
|
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, // UTF-8 string
|
|
|
|
0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
|
|
|
|
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, // array
|
|
|
|
0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
|
|
|
|
0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7 // map
|
|
|
|
})
|
|
|
|
{
|
2017-02-22 17:14:29 +00:00
|
|
|
std::vector<uint8_t> vec(1, static_cast<uint8_t>(b));
|
2017-03-08 17:07:21 +00:00
|
|
|
CHECK_THROWS_AS(json::from_cbor(vec), json::parse_error);
|
2016-12-29 16:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// special case: empty input
|
|
|
|
std::vector<uint8_t> vec2;
|
2017-03-08 17:07:21 +00:00
|
|
|
CHECK_THROWS_AS(json::from_cbor(vec2), json::parse_error);
|
|
|
|
CHECK_THROWS_WITH(json::from_cbor(vec2),
|
2017-04-02 16:46:21 +00:00
|
|
|
"[json.exception.parse_error.110] parse error at 1: unexpected end of input");
|
2017-03-08 17:07:21 +00:00
|
|
|
CHECK_THROWS_AS(json::from_msgpack(vec2), json::parse_error);
|
|
|
|
CHECK_THROWS_WITH(json::from_msgpack(vec2),
|
2017-04-04 14:59:19 +00:00
|
|
|
"[json.exception.parse_error.110] parse error at 1: unexpected end of input");
|
2016-12-29 16:00:02 +00:00
|
|
|
}
|
2017-01-01 14:28:01 +00:00
|
|
|
|
|
|
|
SECTION("issue #411 - Heap-buffer-overflow (OSS-Fuzz issue 366)")
|
|
|
|
{
|
|
|
|
// original test case: empty UTF-8 string (indefinite length)
|
|
|
|
std::vector<uint8_t> vec1 {0x7f};
|
2017-03-08 17:07:21 +00:00
|
|
|
CHECK_THROWS_AS(json::from_cbor(vec1), json::parse_error);
|
|
|
|
CHECK_THROWS_WITH(json::from_cbor(vec1),
|
2017-04-02 16:46:21 +00:00
|
|
|
"[json.exception.parse_error.110] parse error at 2: unexpected end of input");
|
2017-01-01 14:28:01 +00:00
|
|
|
|
|
|
|
// related test case: empty array (indefinite length)
|
|
|
|
std::vector<uint8_t> vec2 {0x9f};
|
2017-03-08 17:07:21 +00:00
|
|
|
CHECK_THROWS_AS(json::from_cbor(vec2), json::parse_error);
|
|
|
|
CHECK_THROWS_WITH(json::from_cbor(vec2),
|
2017-04-02 16:46:21 +00:00
|
|
|
"[json.exception.parse_error.110] parse error at 2: unexpected end of input");
|
2017-01-01 14:28:01 +00:00
|
|
|
|
|
|
|
// related test case: empty map (indefinite length)
|
|
|
|
std::vector<uint8_t> vec3 {0xbf};
|
2017-03-08 17:07:21 +00:00
|
|
|
CHECK_THROWS_AS(json::from_cbor(vec3), json::parse_error);
|
|
|
|
CHECK_THROWS_WITH(json::from_cbor(vec3),
|
2017-04-02 16:46:21 +00:00
|
|
|
"[json.exception.parse_error.110] parse error at 2: unexpected end of input");
|
2017-01-01 14:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("issue #412 - Heap-buffer-overflow (OSS-Fuzz issue 367)")
|
|
|
|
{
|
|
|
|
// original test case
|
|
|
|
std::vector<uint8_t> vec
|
|
|
|
{
|
|
|
|
0xab, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98,
|
|
|
|
0x98, 0x98, 0x98, 0x98, 0x98, 0x00, 0x00, 0x00,
|
|
|
|
0x60, 0xab, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98,
|
|
|
|
0x98, 0x98, 0x98, 0x98, 0x98, 0x00, 0x00, 0x00,
|
|
|
|
0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60,
|
|
|
|
0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60,
|
|
|
|
0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60,
|
|
|
|
0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60,
|
|
|
|
0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60,
|
|
|
|
0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60,
|
|
|
|
0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0xa0, 0x9f,
|
|
|
|
0x9f, 0x97, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60,
|
|
|
|
0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60,
|
|
|
|
0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60,
|
|
|
|
0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60,
|
|
|
|
0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60,
|
|
|
|
0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60
|
|
|
|
};
|
2017-03-08 17:07:21 +00:00
|
|
|
CHECK_THROWS_AS(json::from_cbor(vec), json::parse_error);
|
|
|
|
CHECK_THROWS_WITH(json::from_cbor(vec),
|
2017-03-16 17:39:33 +00:00
|
|
|
"[json.exception.parse_error.113] parse error at 2: expected a CBOR string; last byte: 0x98");
|
2017-01-01 14:28:01 +00:00
|
|
|
|
|
|
|
// related test case: nonempty UTF-8 string (indefinite length)
|
|
|
|
std::vector<uint8_t> vec1 {0x7f, 0x61, 0x61};
|
2017-03-08 17:07:21 +00:00
|
|
|
CHECK_THROWS_AS(json::from_cbor(vec1), json::parse_error);
|
|
|
|
CHECK_THROWS_WITH(json::from_cbor(vec1),
|
2017-04-02 16:46:21 +00:00
|
|
|
"[json.exception.parse_error.110] parse error at 4: unexpected end of input");
|
2017-01-01 14:28:01 +00:00
|
|
|
|
|
|
|
// related test case: nonempty array (indefinite length)
|
|
|
|
std::vector<uint8_t> vec2 {0x9f, 0x01};
|
2017-03-08 17:07:21 +00:00
|
|
|
CHECK_THROWS_AS(json::from_cbor(vec2), json::parse_error);
|
|
|
|
CHECK_THROWS_WITH(json::from_cbor(vec2),
|
2017-04-02 16:46:21 +00:00
|
|
|
"[json.exception.parse_error.110] parse error at 3: unexpected end of input");
|
2017-01-01 14:28:01 +00:00
|
|
|
|
|
|
|
// related test case: nonempty map (indefinite length)
|
|
|
|
std::vector<uint8_t> vec3 {0xbf, 0x61, 0x61, 0x01};
|
2017-03-08 17:07:21 +00:00
|
|
|
CHECK_THROWS_AS(json::from_cbor(vec3), json::parse_error);
|
|
|
|
CHECK_THROWS_WITH(json::from_cbor(vec3),
|
2017-04-02 16:46:21 +00:00
|
|
|
"[json.exception.parse_error.110] parse error at 5: unexpected end of input");
|
2017-01-01 14:28:01 +00:00
|
|
|
}
|
2017-01-03 22:52:01 +00:00
|
|
|
|
2017-01-02 22:49:33 +00:00
|
|
|
SECTION("issue #414 - compare with literal 0)")
|
|
|
|
{
|
2017-02-16 07:50:20 +00:00
|
|
|
#define CHECK_TYPE(v) \
|
|
|
|
CHECK((json(v) == v));\
|
|
|
|
CHECK((v == json(v)));\
|
|
|
|
CHECK_FALSE((json(v) != v));\
|
|
|
|
CHECK_FALSE((v != json(v)));
|
2017-01-02 22:49:33 +00:00
|
|
|
|
|
|
|
CHECK_TYPE(nullptr);
|
|
|
|
CHECK_TYPE(0);
|
|
|
|
CHECK_TYPE(0u);
|
|
|
|
CHECK_TYPE(0L);
|
|
|
|
CHECK_TYPE(0.0);
|
|
|
|
CHECK_TYPE("");
|
|
|
|
|
2017-02-16 07:50:20 +00:00
|
|
|
#undef CHECK_TYPE
|
2017-01-02 22:49:33 +00:00
|
|
|
}
|
2017-02-15 13:00:25 +00:00
|
|
|
|
2017-01-03 22:52:01 +00:00
|
|
|
SECTION("issue #416 - Use-of-uninitialized-value (OSS-Fuzz issue 377)")
|
|
|
|
{
|
|
|
|
// original test case
|
|
|
|
std::vector<uint8_t> vec1
|
|
|
|
{
|
|
|
|
0x94, 0xfa, 0xfa, 0xfa, 0xfa, 0xfa, 0xfa, 0xfa,
|
|
|
|
0x3a, 0x96, 0x96, 0xb4, 0xb4, 0xb4, 0xb4, 0xb4,
|
|
|
|
0xb4, 0xb4, 0xb4, 0xb4, 0xb4, 0xb4, 0xb4, 0x71,
|
|
|
|
0xb4, 0xb4, 0xfa, 0xfa, 0xfa, 0xfa, 0xfa, 0x3a,
|
|
|
|
0x96, 0x96, 0xb4, 0xb4, 0xfa, 0x94, 0x94, 0x61,
|
|
|
|
0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0xfa
|
|
|
|
};
|
2017-03-08 17:07:21 +00:00
|
|
|
CHECK_THROWS_AS(json::from_cbor(vec1), json::parse_error);
|
|
|
|
CHECK_THROWS_WITH(json::from_cbor(vec1),
|
2017-03-16 17:39:33 +00:00
|
|
|
"[json.exception.parse_error.113] parse error at 13: expected a CBOR string; last byte: 0xb4");
|
2017-01-03 22:52:01 +00:00
|
|
|
|
|
|
|
// related test case: double-precision
|
|
|
|
std::vector<uint8_t> vec2
|
|
|
|
{
|
|
|
|
0x94, 0xfa, 0xfa, 0xfa, 0xfa, 0xfa, 0xfa, 0xfa,
|
|
|
|
0x3a, 0x96, 0x96, 0xb4, 0xb4, 0xb4, 0xb4, 0xb4,
|
|
|
|
0xb4, 0xb4, 0xb4, 0xb4, 0xb4, 0xb4, 0xb4, 0x71,
|
|
|
|
0xb4, 0xb4, 0xfa, 0xfa, 0xfa, 0xfa, 0xfa, 0x3a,
|
|
|
|
0x96, 0x96, 0xb4, 0xb4, 0xfa, 0x94, 0x94, 0x61,
|
|
|
|
0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0xfb
|
|
|
|
};
|
2017-03-08 17:07:21 +00:00
|
|
|
CHECK_THROWS_AS(json::from_cbor(vec2), json::parse_error);
|
|
|
|
CHECK_THROWS_WITH(json::from_cbor(vec2),
|
2017-03-16 17:39:33 +00:00
|
|
|
"[json.exception.parse_error.113] parse error at 13: expected a CBOR string; last byte: 0xb4");
|
2017-01-03 22:52:01 +00:00
|
|
|
}
|
2017-02-15 20:30:28 +00:00
|
|
|
|
|
|
|
SECTION("issue #452 - Heap-buffer-overflow (OSS-Fuzz issue 585)")
|
|
|
|
{
|
|
|
|
std::vector<uint8_t> vec = {'-', '0', '1', '2', '2', '7', '4'};
|
2017-03-01 20:28:44 +00:00
|
|
|
CHECK_THROWS_AS(json::parse(vec), json::parse_error);
|
2017-02-15 20:30:28 +00:00
|
|
|
}
|
|
|
|
|
2017-02-16 08:24:45 +00:00
|
|
|
SECTION("issue #454 - doubles are printed as integers")
|
|
|
|
{
|
|
|
|
json j = R"({"bool_value":true,"double_value":2.0,"int_value":10,"level1":{"list_value":[3,"hi",false],"tmp":5.0},"string_value":"hello"})"_json;
|
|
|
|
CHECK(j["double_value"].is_number_float());
|
|
|
|
}
|
2017-02-20 21:48:27 +00:00
|
|
|
|
2017-03-28 21:28:54 +00:00
|
|
|
SECTION("issue #464 - VS2017 implicit to std::string conversion fix")
|
|
|
|
{
|
|
|
|
json v = "test";
|
|
|
|
std::string test;
|
|
|
|
test = v;
|
|
|
|
CHECK(v == "test");
|
|
|
|
}
|
|
|
|
|
2017-02-20 21:48:27 +00:00
|
|
|
SECTION("issue #465 - roundtrip error while parsing 1000000000000000010E5")
|
|
|
|
{
|
|
|
|
json j1 = json::parse("1000000000000000010E5");
|
|
|
|
std::string s1 = j1.dump();
|
|
|
|
json j2 = json::parse(s1);
|
|
|
|
std::string s2 = j2.dump();
|
|
|
|
CHECK(s1 == s2);
|
|
|
|
}
|
2017-03-11 14:32:44 +00:00
|
|
|
|
2017-03-12 14:20:17 +00:00
|
|
|
SECTION("issue #473 - inconsistent behavior in conversion to array type")
|
|
|
|
{
|
|
|
|
json j_array = {1, 2, 3, 4};
|
|
|
|
json j_number = 42;
|
|
|
|
json j_null = nullptr;
|
|
|
|
|
|
|
|
SECTION("std::vector")
|
|
|
|
{
|
|
|
|
auto create = [](const json & j)
|
|
|
|
{
|
|
|
|
std::vector<int> v = j;
|
|
|
|
};
|
|
|
|
|
|
|
|
CHECK_NOTHROW(create(j_array));
|
2017-03-12 19:22:30 +00:00
|
|
|
CHECK_THROWS_AS(create(j_number), json::type_error);
|
|
|
|
CHECK_THROWS_WITH(create(j_number), "[json.exception.type_error.302] type must be array, but is number");
|
|
|
|
CHECK_THROWS_AS(create(j_null), json::type_error);
|
|
|
|
CHECK_THROWS_WITH(create(j_null), "[json.exception.type_error.302] type must be array, but is null");
|
2017-03-12 14:20:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("std::list")
|
|
|
|
{
|
|
|
|
auto create = [](const json & j)
|
|
|
|
{
|
|
|
|
std::list<int> v = j;
|
|
|
|
};
|
|
|
|
|
|
|
|
CHECK_NOTHROW(create(j_array));
|
2017-03-12 19:22:30 +00:00
|
|
|
CHECK_THROWS_AS(create(j_number), json::type_error);
|
|
|
|
CHECK_THROWS_WITH(create(j_number), "[json.exception.type_error.302] type must be array, but is number");
|
|
|
|
CHECK_THROWS_AS(create(j_null), json::type_error);
|
|
|
|
CHECK_THROWS_WITH(create(j_null), "[json.exception.type_error.302] type must be array, but is null");
|
2017-03-12 14:20:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("std::forward_list")
|
|
|
|
{
|
|
|
|
auto create = [](const json & j)
|
|
|
|
{
|
|
|
|
std::forward_list<int> v = j;
|
|
|
|
};
|
|
|
|
|
|
|
|
CHECK_NOTHROW(create(j_array));
|
2017-03-12 19:22:30 +00:00
|
|
|
CHECK_THROWS_AS(create(j_number), json::type_error);
|
|
|
|
CHECK_THROWS_WITH(create(j_number), "[json.exception.type_error.302] type must be array, but is number");
|
|
|
|
CHECK_THROWS_AS(create(j_null), json::type_error);
|
|
|
|
CHECK_THROWS_WITH(create(j_null), "[json.exception.type_error.302] type must be array, but is null");
|
2017-03-12 14:20:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-11 14:44:14 +00:00
|
|
|
SECTION("issue #486 - json::value_t can't be a map's key type in VC++ 2015")
|
|
|
|
{
|
|
|
|
// the code below must compile with MSVC
|
|
|
|
std::map<json::value_t, std::string> jsonTypes ;
|
|
|
|
jsonTypes[json::value_t::array] = "array";
|
|
|
|
}
|
2017-03-12 09:40:36 +00:00
|
|
|
|
2017-03-11 14:32:44 +00:00
|
|
|
SECTION("issue #494 - conversion from vector<bool> to json fails to build")
|
|
|
|
{
|
|
|
|
std::vector<bool> boolVector = {false, true, false, false};
|
|
|
|
json j;
|
|
|
|
j["bool_vector"] = boolVector;
|
|
|
|
|
|
|
|
CHECK(j["bool_vector"].dump() == "[false,true,false,false]");
|
|
|
|
}
|
2017-03-11 18:26:12 +00:00
|
|
|
|
2017-03-24 18:49:02 +00:00
|
|
|
/* NOTE: m_line_buffer is not used any more
|
2017-03-11 18:26:12 +00:00
|
|
|
SECTION("issue #495 - fill_line_buffer incorrectly tests m_stream for eof but not fail or bad bits")
|
|
|
|
{
|
|
|
|
SECTION("setting failbit")
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "[1,2,3\n,4,5]";
|
|
|
|
|
|
|
|
json::lexer l(ss);
|
|
|
|
|
|
|
|
CHECK(l.m_line_buffer == "[1,2,3\n");
|
|
|
|
|
|
|
|
l.m_stream->setstate(std::ios_base::failbit);
|
|
|
|
|
2017-03-14 20:31:36 +00:00
|
|
|
CHECK_THROWS_AS(l.fill_line_buffer(), json::parse_error);
|
|
|
|
CHECK_THROWS_WITH(l.fill_line_buffer(), "[json.exception.parse_error.111] parse error: bad input stream");
|
2017-03-11 18:26:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("setting badbit")
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "[1,2,3\n,4,5]";
|
|
|
|
|
|
|
|
json::lexer l(ss);
|
|
|
|
|
|
|
|
CHECK(l.m_line_buffer == "[1,2,3\n");
|
|
|
|
|
|
|
|
l.m_stream->setstate(std::ios_base::badbit);
|
|
|
|
|
2017-03-14 20:31:36 +00:00
|
|
|
CHECK_THROWS_AS(l.fill_line_buffer(), json::parse_error);
|
|
|
|
CHECK_THROWS_WITH(l.fill_line_buffer(), "[json.exception.parse_error.111] parse error: bad input stream");
|
2017-03-11 18:26:12 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-24 18:49:02 +00:00
|
|
|
*/
|
2017-03-14 20:24:53 +00:00
|
|
|
|
2017-03-14 20:05:38 +00:00
|
|
|
SECTION("issue #504 - assertion error (OSS-Fuzz 856)")
|
|
|
|
{
|
|
|
|
std::vector<uint8_t> vec1 = {0xf9, 0xff, 0xff, 0x4a, 0x3a, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x37, 0x02, 0x38};
|
|
|
|
json j1 = json::from_cbor(vec1);
|
|
|
|
|
|
|
|
// step 2: round trip
|
|
|
|
std::vector<uint8_t> vec2 = json::to_cbor(j1);
|
|
|
|
|
|
|
|
// parse serialization
|
|
|
|
json j2 = json::from_cbor(vec2);
|
|
|
|
|
|
|
|
// NaN is dumped to "null"
|
|
|
|
CHECK(j2.is_number_float());
|
|
|
|
CHECK(std::isnan(j2.get<json::number_float_t>()));
|
|
|
|
CHECK(j2.dump() == "null");
|
|
|
|
|
|
|
|
// check if serializations match
|
|
|
|
CHECK(json::to_cbor(j2) == vec2);
|
|
|
|
}
|
2017-04-07 13:44:41 +00:00
|
|
|
|
|
|
|
SECTION("issue #512 - use of overloaded operator '<=' is ambiguous")
|
|
|
|
{
|
|
|
|
json j;
|
|
|
|
j["a"] = 5;
|
|
|
|
|
|
|
|
// json op scalar
|
|
|
|
CHECK(j["a"] == 5);
|
|
|
|
CHECK(j["a"] != 4);
|
|
|
|
|
|
|
|
CHECK(j["a"] <= 7);
|
|
|
|
CHECK(j["a"] < 7);
|
|
|
|
CHECK(j["a"] >= 3);
|
|
|
|
CHECK(j["a"] > 3);
|
|
|
|
|
|
|
|
|
|
|
|
CHECK(not(j["a"] <= 4));
|
|
|
|
CHECK(not(j["a"] < 4));
|
|
|
|
CHECK(not(j["a"] >= 6));
|
|
|
|
CHECK(not(j["a"] > 6));
|
|
|
|
|
|
|
|
// scalar op json
|
|
|
|
CHECK(5 == j["a"]);
|
|
|
|
CHECK(4 != j["a"]);
|
|
|
|
|
|
|
|
CHECK(7 >= j["a"]);
|
|
|
|
CHECK(7 > j["a"]);
|
|
|
|
CHECK(3 <= j["a"]);
|
|
|
|
CHECK(3 < j["a"]);
|
|
|
|
|
|
|
|
CHECK(not(4 >= j["a"]));
|
|
|
|
CHECK(not(4 > j["a"]));
|
|
|
|
CHECK(not(6 <= j["a"]));
|
|
|
|
CHECK(not(6 < j["a"]));
|
|
|
|
}
|
2017-05-07 11:41:48 +00:00
|
|
|
|
|
|
|
SECTION("issue #575 - heap-buffer-overflow (OSS-Fuzz 1400)")
|
|
|
|
{
|
|
|
|
std::vector<uint8_t> vec = {'"', '\\', '"', 'X', '"', '"'};
|
|
|
|
CHECK_THROWS_AS(json::parse(vec), json::parse_error);
|
|
|
|
}
|
2016-08-04 19:55:47 +00:00
|
|
|
}
|