🚨 fix Clang-Tidy warnings

This commit is contained in:
Niels Lohmann 2022-09-11 07:51:40 +02:00
parent d4bc9f9262
commit 649e9c7f30
No known key found for this signature in database
GPG Key ID: 7F3CEA63AE251B69
26 changed files with 386 additions and 386 deletions

View File

@ -26,7 +26,7 @@ int main()
#endif #endif
// copy stdin to byte vector // copy stdin to byte vector
std::vector<uint8_t> vec; std::vector<uint8_t> vec;
char c; char c = 0;
while (std::cin.get(c)) while (std::cin.get(c))
{ {
vec.push_back(static_cast<uint8_t>(c)); vec.push_back(static_cast<uint8_t>(c));

View File

@ -37,24 +37,24 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
try try
{ {
// step 1: parse input // step 1: parse input
std::vector<uint8_t> vec1(data, data + size); std::vector<uint8_t> const vec1(data, data + size);
json j1 = json::from_bjdata(vec1); json const j1 = json::from_bjdata(vec1);
try try
{ {
// step 2.1: round trip without adding size annotations to container types // step 2.1: round trip without adding size annotations to container types
std::vector<uint8_t> vec2 = json::to_bjdata(j1, false, false); std::vector<uint8_t> const vec2 = json::to_bjdata(j1, false, false);
// step 2.2: round trip with adding size annotations but without adding type annonations to container types // step 2.2: round trip with adding size annotations but without adding type annonations to container types
std::vector<uint8_t> vec3 = json::to_bjdata(j1, true, false); std::vector<uint8_t> const vec3 = json::to_bjdata(j1, true, false);
// step 2.3: round trip with adding size as well as type annotations to container types // step 2.3: round trip with adding size as well as type annotations to container types
std::vector<uint8_t> vec4 = json::to_bjdata(j1, true, true); std::vector<uint8_t> const vec4 = json::to_bjdata(j1, true, true);
// parse serialization // parse serialization
json j2 = json::from_bjdata(vec2); json const j2 = json::from_bjdata(vec2);
json j3 = json::from_bjdata(vec3); json const j3 = json::from_bjdata(vec3);
json j4 = json::from_bjdata(vec4); json const j4 = json::from_bjdata(vec4);
// serializations must match // serializations must match
assert(json::to_bjdata(j2, false, false) == vec2); assert(json::to_bjdata(j2, false, false) == vec2);

View File

@ -31,8 +31,8 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
try try
{ {
// step 1: parse input // step 1: parse input
std::vector<uint8_t> vec1(data, data + size); std::vector<uint8_t> const vec1(data, data + size);
json j1 = json::from_bson(vec1); json const j1 = json::from_bson(vec1);
if (j1.is_discarded()) if (j1.is_discarded())
{ {
@ -42,10 +42,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
try try
{ {
// step 2: round trip // step 2: round trip
std::vector<uint8_t> vec2 = json::to_bson(j1); std::vector<uint8_t> const vec2 = json::to_bson(j1);
// parse serialization // parse serialization
json j2 = json::from_bson(vec2); json const j2 = json::from_bson(vec2);
// serializations must match // serializations must match
assert(json::to_bson(j2) == vec2); assert(json::to_bson(j2) == vec2);

View File

@ -31,16 +31,16 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
try try
{ {
// step 1: parse input // step 1: parse input
std::vector<uint8_t> vec1(data, data + size); std::vector<uint8_t> const vec1(data, data + size);
json j1 = json::from_cbor(vec1); json const j1 = json::from_cbor(vec1);
try try
{ {
// step 2: round trip // step 2: round trip
std::vector<uint8_t> vec2 = json::to_cbor(j1); std::vector<uint8_t> const vec2 = json::to_cbor(j1);
// parse serialization // parse serialization
json j2 = json::from_cbor(vec2); json const j2 = json::from_cbor(vec2);
// serializations must match // serializations must match
assert(json::to_cbor(j2) == vec2); assert(json::to_cbor(j2) == vec2);

View File

@ -32,20 +32,20 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
try try
{ {
// step 1: parse input // step 1: parse input
json j1 = json::parse(data, data + size); json const j1 = json::parse(data, data + size);
try try
{ {
// step 2: round trip // step 2: round trip
// first serialization // first serialization
std::string s1 = j1.dump(); std::string const s1 = j1.dump();
// parse serialization // parse serialization
json j2 = json::parse(s1); json const j2 = json::parse(s1);
// second serialization // second serialization
std::string s2 = j2.dump(); std::string const s2 = j2.dump();
// serializations must match // serializations must match
assert(s1 == s2); assert(s1 == s2);

View File

@ -31,16 +31,16 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
try try
{ {
// step 1: parse input // step 1: parse input
std::vector<uint8_t> vec1(data, data + size); std::vector<uint8_t> const vec1(data, data + size);
json j1 = json::from_msgpack(vec1); json const j1 = json::from_msgpack(vec1);
try try
{ {
// step 2: round trip // step 2: round trip
std::vector<uint8_t> vec2 = json::to_msgpack(j1); std::vector<uint8_t> const vec2 = json::to_msgpack(j1);
// parse serialization // parse serialization
json j2 = json::from_msgpack(vec2); json const j2 = json::from_msgpack(vec2);
// serializations must match // serializations must match
assert(json::to_msgpack(j2) == vec2); assert(json::to_msgpack(j2) == vec2);

View File

@ -37,24 +37,24 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
try try
{ {
// step 1: parse input // step 1: parse input
std::vector<uint8_t> vec1(data, data + size); std::vector<uint8_t> const vec1(data, data + size);
json j1 = json::from_ubjson(vec1); json const j1 = json::from_ubjson(vec1);
try try
{ {
// step 2.1: round trip without adding size annotations to container types // step 2.1: round trip without adding size annotations to container types
std::vector<uint8_t> vec2 = json::to_ubjson(j1, false, false); std::vector<uint8_t> const vec2 = json::to_ubjson(j1, false, false);
// step 2.2: round trip with adding size annotations but without adding type annonations to container types // step 2.2: round trip with adding size annotations but without adding type annonations to container types
std::vector<uint8_t> vec3 = json::to_ubjson(j1, true, false); std::vector<uint8_t> const vec3 = json::to_ubjson(j1, true, false);
// step 2.3: round trip with adding size as well as type annotations to container types // step 2.3: round trip with adding size as well as type annotations to container types
std::vector<uint8_t> vec4 = json::to_ubjson(j1, true, true); std::vector<uint8_t> const vec4 = json::to_ubjson(j1, true, true);
// parse serialization // parse serialization
json j2 = json::from_ubjson(vec2); json const j2 = json::from_ubjson(vec2);
json j3 = json::from_ubjson(vec3); json const j3 = json::from_ubjson(vec3);
json j4 = json::from_ubjson(vec4); json const j4 = json::from_ubjson(vec4);
// serializations must match // serializations must match
assert(json::to_ubjson(j2, false, false) == vec2); assert(json::to_ubjson(j2, false, false) == vec2);

View File

@ -34,9 +34,9 @@ TEST_CASE_TEMPLATE_DEFINE("value_in_range_of trait", T, value_in_range_of_test)
constexpr bool max_in_range = T::max_in_range; constexpr bool max_in_range = T::max_in_range;
type val_min = std::numeric_limits<type>::min(); type val_min = std::numeric_limits<type>::min();
type val_min2 = val_min + 1; type const val_min2 = val_min + 1;
type val_max = std::numeric_limits<type>::max(); type val_max = std::numeric_limits<type>::max();
type val_max2 = val_max - 1; type const val_max2 = val_max - 1;
REQUIRE(CHAR_BIT == 8); REQUIRE(CHAR_BIT == 8);
@ -108,8 +108,8 @@ TEST_CASE("BJData")
{ {
SECTION("optimized array: negative size") SECTION("optimized array: negative size")
{ {
std::vector<uint8_t> vM = {'[', '$', 'M', '#', '[', 'I', 0x00, 0x20, 'M', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0xFF, ']'}; std::vector<uint8_t> const vM = {'[', '$', 'M', '#', '[', 'I', 0x00, 0x20, 'M', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0xFF, ']'};
std::vector<uint8_t> vMX = {'[', '$', 'U', '#', '[', 'M', 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 'U', 0x01, ']'}; std::vector<uint8_t> const vMX = {'[', '$', 'U', '#', '[', 'M', 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 'U', 0x01, ']'};
json _; json _;
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vM), "[json.exception.out_of_range.408] syntax error while parsing BJData size: integer value overflow", json::out_of_range&); CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vM), "[json.exception.out_of_range.408] syntax error while parsing BJData size: integer value overflow", json::out_of_range&);
@ -121,8 +121,8 @@ TEST_CASE("BJData")
SECTION("optimized array: integer value overflow") SECTION("optimized array: integer value overflow")
{ {
std::vector<uint8_t> vL = {'[', '#', 'L', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F}; std::vector<uint8_t> const vL = {'[', '#', 'L', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F};
std::vector<uint8_t> vM = {'[', '$', 'M', '#', '[', 'I', 0x00, 0x20, 'M', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0xFF, ']'}; std::vector<uint8_t> const vM = {'[', '$', 'M', '#', '[', 'I', 0x00, 0x20, 'M', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0xFF, ']'};
json _; json _;
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vL), "[json.exception.out_of_range.408] syntax error while parsing BJData size: integer value overflow", json::out_of_range&); CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vL), "[json.exception.out_of_range.408] syntax error while parsing BJData size: integer value overflow", json::out_of_range&);

View File

@ -45,7 +45,7 @@ TEST_CASE("byte_container_with_subtype")
SECTION("comparisons") SECTION("comparisons")
{ {
std::vector<std::uint8_t> bytes = {{0xCA, 0xFE, 0xBA, 0xBE}}; std::vector<std::uint8_t> const bytes = {{0xCA, 0xFE, 0xBA, 0xBE}};
nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>> container1; nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>> container1;
nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>> container2({}, 42); nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>> container2({}, 42);
nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>> container3(bytes); nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>> container3(bytes);

View File

@ -416,7 +416,7 @@ TEST_CASE("capacity")
{ {
SECTION("boolean") SECTION("boolean")
{ {
json j = true; json const j = true;
const json j_const = true; const json j_const = true;
SECTION("result of max_size") SECTION("result of max_size")
@ -428,7 +428,7 @@ TEST_CASE("capacity")
SECTION("string") SECTION("string")
{ {
json j = "hello world"; json const j = "hello world";
const json j_const = "hello world"; const json j_const = "hello world";
SECTION("result of max_size") SECTION("result of max_size")
@ -442,7 +442,7 @@ TEST_CASE("capacity")
{ {
SECTION("empty array") SECTION("empty array")
{ {
json j = json::array(); json const j = json::array();
const json j_const = json::array(); const json j_const = json::array();
SECTION("result of max_size") SECTION("result of max_size")
@ -454,7 +454,7 @@ TEST_CASE("capacity")
SECTION("filled array") SECTION("filled array")
{ {
json j = {1, 2, 3}; json const j = {1, 2, 3};
const json j_const = {1, 2, 3}; const json j_const = {1, 2, 3};
SECTION("result of max_size") SECTION("result of max_size")
@ -469,7 +469,7 @@ TEST_CASE("capacity")
{ {
SECTION("empty object") SECTION("empty object")
{ {
json j = json::object(); json const j = json::object();
const json j_const = json::object(); const json j_const = json::object();
SECTION("result of max_size") SECTION("result of max_size")
@ -481,7 +481,7 @@ TEST_CASE("capacity")
SECTION("filled object") SECTION("filled object")
{ {
json j = {{"one", 1}, {"two", 2}, {"three", 3}}; json const j = {{"one", 1}, {"two", 2}, {"three", 3}};
const json j_const = {{"one", 1}, {"two", 2}, {"three", 3}}; const json j_const = {{"one", 1}, {"two", 2}, {"three", 3}};
SECTION("result of max_size") SECTION("result of max_size")
@ -494,7 +494,7 @@ TEST_CASE("capacity")
SECTION("number (integer)") SECTION("number (integer)")
{ {
json j = -23; json const j = -23;
const json j_const = -23; const json j_const = -23;
SECTION("result of max_size") SECTION("result of max_size")
@ -506,7 +506,7 @@ TEST_CASE("capacity")
SECTION("number (unsigned)") SECTION("number (unsigned)")
{ {
json j = 23u; json const j = 23u;
const json j_const = 23u; const json j_const = 23u;
SECTION("result of max_size") SECTION("result of max_size")
@ -518,7 +518,7 @@ TEST_CASE("capacity")
SECTION("number (float)") SECTION("number (float)")
{ {
json j = 23.42; json const j = 23.42;
const json j_const = 23.42; const json j_const = 23.42;
SECTION("result of max_size") SECTION("result of max_size")
@ -530,7 +530,7 @@ TEST_CASE("capacity")
SECTION("null") SECTION("null")
{ {
json j = nullptr; json const j = nullptr;
const json j_const = nullptr; const json j_const = nullptr;
SECTION("result of max_size") SECTION("result of max_size")

View File

@ -249,11 +249,11 @@ bool accept_helper(const std::string& s)
CHECK(json::parser(nlohmann::detail::input_adapter(s)).accept(false) == !el.errored); CHECK(json::parser(nlohmann::detail::input_adapter(s)).accept(false) == !el.errored);
// 5. parse with simple callback // 5. parse with simple callback
json::parser_callback_t cb = [](int /*unused*/, json::parse_event_t /*unused*/, json& /*unused*/) noexcept json::parser_callback_t const cb = [](int /*unused*/, json::parse_event_t /*unused*/, json& /*unused*/) noexcept
{ {
return true; return true;
}; };
json j_cb = json::parse(s, cb, false); json const j_cb = json::parse(s, cb, false);
const bool ok_noexcept_cb = !j_cb.is_discarded(); const bool ok_noexcept_cb = !j_cb.is_discarded();
// 6. check if this approach came to the same result // 6. check if this approach came to the same result
@ -1093,7 +1093,7 @@ TEST_CASE("parser class")
for (int c = 1; c < 128; ++c) for (int c = 1; c < 128; ++c)
{ {
std::string s = "\"\\u"; std::string const s = "\"\\u";
// create a string with the iterated character at each position // create a string with the iterated character at each position
auto s1 = s + "000" + std::string(1, static_cast<char>(c)) + "\""; auto s1 = s + "000" + std::string(1, static_cast<char>(c)) + "\"";
@ -1308,7 +1308,7 @@ TEST_CASE("parser class")
for (int c = 1; c < 128; ++c) for (int c = 1; c < 128; ++c)
{ {
std::string s = "\"\\u"; std::string const s = "\"\\u";
// create a string with the iterated character at each position // create a string with the iterated character at each position
const auto s1 = s + "000" + std::string(1, static_cast<char>(c)) + "\""; const auto s1 = s + "000" + std::string(1, static_cast<char>(c)) + "\"";
@ -1361,7 +1361,7 @@ TEST_CASE("parser class")
// test case to make sure the callback is properly evaluated after reading a key // test case to make sure the callback is properly evaluated after reading a key
{ {
json::parser_callback_t cb = [](int /*unused*/, json::parse_event_t event, json& /*unused*/) noexcept json::parser_callback_t const cb = [](int /*unused*/, json::parse_event_t event, json& /*unused*/) noexcept
{ {
return event != json::parse_event_t::key; return event != json::parse_event_t::key;
}; };
@ -1417,7 +1417,7 @@ TEST_CASE("parser class")
SECTION("filter everything") SECTION("filter everything")
{ {
json j_object = json::parse(s_object, [](int /*unused*/, json::parse_event_t /*unused*/, const json& /*unused*/) noexcept json const j_object = json::parse(s_object, [](int /*unused*/, json::parse_event_t /*unused*/, const json& /*unused*/) noexcept
{ {
return false; return false;
}); });
@ -1425,7 +1425,7 @@ TEST_CASE("parser class")
// the top-level object will be discarded, leaving a null // the top-level object will be discarded, leaving a null
CHECK (j_object.is_null()); CHECK (j_object.is_null());
json j_array = json::parse(s_array, [](int /*unused*/, json::parse_event_t /*unused*/, const json& /*unused*/) noexcept json const j_array = json::parse(s_array, [](int /*unused*/, json::parse_event_t /*unused*/, const json& /*unused*/) noexcept
{ {
return false; return false;
}); });
@ -1574,7 +1574,7 @@ TEST_CASE("parser class")
SECTION("from std::initializer_list") SECTION("from std::initializer_list")
{ {
std::initializer_list<uint8_t> v = {'t', 'r', 'u', 'e'}; std::initializer_list<uint8_t> const v = {'t', 'r', 'u', 'e'};
json j; json j;
json::parser(nlohmann::detail::input_adapter(std::begin(v), std::end(v))).parse(true, j); json::parser(nlohmann::detail::input_adapter(std::begin(v), std::end(v))).parse(true, j);
CHECK(j == json(true)); CHECK(j == json(true));
@ -1593,7 +1593,7 @@ TEST_CASE("parser class")
{ {
SECTION("parser with callback") SECTION("parser with callback")
{ {
json::parser_callback_t cb = [](int /*unused*/, json::parse_event_t /*unused*/, json& /*unused*/) noexcept json::parser_callback_t const cb = [](int /*unused*/, json::parse_event_t /*unused*/, json& /*unused*/) noexcept
{ {
return true; return true;
}; };

View File

@ -173,9 +173,9 @@ TEST_CASE("convenience functions")
using nlohmann::detail::concat; using nlohmann::detail::concat;
const char* expected = "Hello, world!"; const char* expected = "Hello, world!";
alt_string_iter hello_iter{"Hello, "}; alt_string_iter const hello_iter{"Hello, "};
alt_string_data hello_data{"Hello, "}; alt_string_data const hello_data{"Hello, "};
std::string world = "world"; std::string const world = "world";
SECTION("std::string") SECTION("std::string")
{ {

View File

@ -188,7 +188,7 @@ TEST_CASE("JSON Node Metadata")
value.metadata().emplace_back(1); value.metadata().emplace_back(1);
value.metadata().emplace_back(2); value.metadata().emplace_back(2);
json array(10, value); json const array(10, value);
CHECK(value.metadata().size() == 2); CHECK(value.metadata().size() == 2);
CHECK(value.metadata().at(0) == 1); CHECK(value.metadata().at(0) == 1);

View File

@ -265,7 +265,7 @@ TEST_CASE("deserialization")
SECTION("string_t") SECTION("string_t")
{ {
json::string_t s = R"(["foo",1,2,3,false,{"one":1}])"; json::string_t const s = R"(["foo",1,2,3,false,{"one":1}])";
json j = json::parse(s); json j = json::parse(s);
CHECK(json::accept(s)); CHECK(json::accept(s));
CHECK(j == json({"foo", 1, 2, 3, false, {{"one", 1}}})); CHECK(j == json({"foo", 1, 2, 3, false, {{"one", 1}}}));
@ -341,7 +341,7 @@ TEST_CASE("deserialization")
SECTION("string") SECTION("string")
{ {
json::string_t s = R"(["foo",1,2,3,false,{"one":1})"; json::string_t const s = R"(["foo",1,2,3,false,{"one":1})";
json _; json _;
CHECK_THROWS_WITH_AS(_ = json::parse(s), "[json.exception.parse_error.101] parse error at line 1, column 29: syntax error while parsing array - unexpected end of input; expected ']'", json::parse_error&); CHECK_THROWS_WITH_AS(_ = json::parse(s), "[json.exception.parse_error.101] parse error at line 1, column 29: syntax error while parsing array - unexpected end of input; expected ']'", json::parse_error&);
CHECK(!json::accept(s)); CHECK(!json::accept(s));
@ -390,7 +390,7 @@ TEST_CASE("deserialization")
{ {
SECTION("from std::vector") SECTION("from std::vector")
{ {
std::vector<uint8_t> v = {'t', 'r', 'u', 'e'}; std::vector<uint8_t> const v = {'t', 'r', 'u', 'e'};
CHECK(json::parse(v) == json(true)); CHECK(json::parse(v) == json(true));
CHECK(json::accept(v)); CHECK(json::accept(v));
@ -402,7 +402,7 @@ TEST_CASE("deserialization")
SECTION("from std::array") SECTION("from std::array")
{ {
std::array<uint8_t, 5> v { {'t', 'r', 'u', 'e'} }; std::array<uint8_t, 5> const v { {'t', 'r', 'u', 'e'} };
CHECK(json::parse(v) == json(true)); CHECK(json::parse(v) == json(true));
CHECK(json::accept(v)); CHECK(json::accept(v));
@ -445,7 +445,7 @@ TEST_CASE("deserialization")
SECTION("from std::string") SECTION("from std::string")
{ {
std::string v = {'t', 'r', 'u', 'e'}; std::string const v = {'t', 'r', 'u', 'e'};
CHECK(json::parse(v) == json(true)); CHECK(json::parse(v) == json(true));
CHECK(json::accept(v)); CHECK(json::accept(v));
@ -457,7 +457,7 @@ TEST_CASE("deserialization")
SECTION("from std::initializer_list") SECTION("from std::initializer_list")
{ {
std::initializer_list<uint8_t> v = {'t', 'r', 'u', 'e'}; std::initializer_list<uint8_t> const v = {'t', 'r', 'u', 'e'};
CHECK(json::parse(v) == json(true)); CHECK(json::parse(v) == json(true));
CHECK(json::accept(v)); CHECK(json::accept(v));
@ -469,7 +469,7 @@ TEST_CASE("deserialization")
SECTION("empty container") SECTION("empty container")
{ {
std::vector<uint8_t> v; std::vector<uint8_t> const v;
json _; json _;
CHECK_THROWS_AS(_ = json::parse(v), json::parse_error&); CHECK_THROWS_AS(_ = json::parse(v), json::parse_error&);
CHECK(!json::accept(v)); CHECK(!json::accept(v));
@ -534,7 +534,7 @@ TEST_CASE("deserialization")
SECTION("from std::initializer_list") SECTION("from std::initializer_list")
{ {
std::initializer_list<uint8_t> v = {'t', 'r', 'u', 'e'}; std::initializer_list<uint8_t> const v = {'t', 'r', 'u', 'e'};
CHECK(json::parse(std::begin(v), std::end(v)) == json(true)); CHECK(json::parse(std::begin(v), std::end(v)) == json(true));
CHECK(json::accept(std::begin(v), std::end(v))); CHECK(json::accept(std::begin(v), std::end(v)));
@ -1045,7 +1045,7 @@ TEST_CASE("deserialization")
SECTION("SAX and early abort") SECTION("SAX and early abort")
{ {
std::string s = R"([1, ["string", 43.12], null, {"key1": true, "key2": false}])"; std::string const s = R"([1, ["string", 43.12], null, {"key1": true, "key2": false}])";
SaxEventLogger default_logger; SaxEventLogger default_logger;
SaxEventLoggerExitAfterStartObject exit_after_start_object; SaxEventLoggerExitAfterStartObject exit_after_start_object;
@ -1139,7 +1139,7 @@ TEST_CASE_TEMPLATE("deserialization of different character types (ASCII)", T,
std::int16_t, std::uint16_t, std::int16_t, std::uint16_t,
std::int32_t, std::uint32_t) std::int32_t, std::uint32_t)
{ {
std::vector<T> v = {'t', 'r', 'u', 'e'}; std::vector<T> const v = {'t', 'r', 'u', 'e'};
CHECK(json::parse(v) == json(true)); CHECK(json::parse(v) == json(true));
CHECK(json::accept(v)); CHECK(json::accept(v));
@ -1153,7 +1153,7 @@ TEST_CASE_TEMPLATE("deserialization of different character types (UTF-8)", T,
char, unsigned char, std::uint8_t) char, unsigned char, std::uint8_t)
{ {
// a star emoji // a star emoji
std::vector<T> v = {'"', static_cast<T>(0xe2u), static_cast<T>(0xadu), static_cast<T>(0x90u), static_cast<T>(0xefu), static_cast<T>(0xb8u), static_cast<T>(0x8fu), '"'}; std::vector<T> const v = {'"', static_cast<T>(0xe2u), static_cast<T>(0xadu), static_cast<T>(0x90u), static_cast<T>(0xefu), static_cast<T>(0xb8u), static_cast<T>(0x8fu), '"'};
CHECK(json::parse(v).dump(-1, ' ', true) == "\"\\u2b50\\ufe0f\""); CHECK(json::parse(v).dump(-1, ' ', true) == "\"\\u2b50\\ufe0f\"");
CHECK(json::accept(v)); CHECK(json::accept(v));
@ -1166,7 +1166,7 @@ TEST_CASE_TEMPLATE("deserialization of different character types (UTF-16)", T,
char16_t, std::uint16_t) char16_t, std::uint16_t)
{ {
// a star emoji // a star emoji
std::vector<T> v = {static_cast<T>('"'), static_cast<T>(0x2b50), static_cast<T>(0xfe0f), static_cast<T>('"')}; std::vector<T> const v = {static_cast<T>('"'), static_cast<T>(0x2b50), static_cast<T>(0xfe0f), static_cast<T>('"')};
CHECK(json::parse(v).dump(-1, ' ', true) == "\"\\u2b50\\ufe0f\""); CHECK(json::parse(v).dump(-1, ' ', true) == "\"\\u2b50\\ufe0f\"");
CHECK(json::accept(v)); CHECK(json::accept(v));
@ -1179,7 +1179,7 @@ TEST_CASE_TEMPLATE("deserialization of different character types (UTF-32)", T,
char32_t, std::uint32_t) char32_t, std::uint32_t)
{ {
// a star emoji // a star emoji
std::vector<T> v = {static_cast<T>('"'), static_cast<T>(0x2b50), static_cast<T>(0xfe0f), static_cast<T>('"')}; std::vector<T> const v = {static_cast<T>('"'), static_cast<T>(0x2b50), static_cast<T>(0xfe0f), static_cast<T>('"')};
CHECK(json::parse(v).dump(-1, ' ', true) == "\"\\u2b50\\ufe0f\""); CHECK(json::parse(v).dump(-1, ' ', true) == "\"\\u2b50\\ufe0f\"");
CHECK(json::accept(v)); CHECK(json::accept(v));

View File

@ -274,13 +274,13 @@ TEST_CASE("element access 1")
{ {
{ {
json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}}; json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
json::iterator it2 = jarray.erase(jarray.begin()); json::iterator const it2 = jarray.erase(jarray.begin());
CHECK(jarray == json({1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}})); CHECK(jarray == json({1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}}));
CHECK(*it2 == json(1u)); CHECK(*it2 == json(1u));
} }
{ {
json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}}; json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
json::const_iterator it2 = jarray.erase(jarray.cbegin()); json::const_iterator const it2 = jarray.erase(jarray.cbegin());
CHECK(jarray == json({1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}})); CHECK(jarray == json({1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}}));
CHECK(*it2 == json(1u)); CHECK(*it2 == json(1u));
} }
@ -306,13 +306,13 @@ TEST_CASE("element access 1")
{ {
{ {
json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}}; json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
json::iterator it2 = jarray.erase(jarray.begin(), jarray.begin()); json::iterator const it2 = jarray.erase(jarray.begin(), jarray.begin());
CHECK(jarray == json({1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}})); CHECK(jarray == json({1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}}));
CHECK(*it2 == json(1)); CHECK(*it2 == json(1));
} }
{ {
json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}}; json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
json::const_iterator it2 = jarray.erase(jarray.cbegin(), jarray.cbegin()); json::const_iterator const it2 = jarray.erase(jarray.cbegin(), jarray.cbegin());
CHECK(jarray == json({1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}})); CHECK(jarray == json({1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}}));
CHECK(*it2 == json(1)); CHECK(*it2 == json(1));
} }
@ -322,15 +322,15 @@ TEST_CASE("element access 1")
{ {
{ {
json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}}; json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
json::iterator it = jarray.begin() + 4; json::iterator const it = jarray.begin() + 4;
json::iterator it2 = jarray.erase(it); json::iterator const it2 = jarray.erase(it);
CHECK(jarray == json({1, 1u, true, nullptr, 42.23, json::object(), {1, 2, 3}})); CHECK(jarray == json({1, 1u, true, nullptr, 42.23, json::object(), {1, 2, 3}}));
CHECK(*it2 == json(42.23)); CHECK(*it2 == json(42.23));
} }
{ {
json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}}; json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
json::const_iterator it = jarray.cbegin() + 4; json::const_iterator const it = jarray.cbegin() + 4;
json::const_iterator it2 = jarray.erase(it); json::const_iterator const it2 = jarray.erase(it);
CHECK(jarray == json({1, 1u, true, nullptr, 42.23, json::object(), {1, 2, 3}})); CHECK(jarray == json({1, 1u, true, nullptr, 42.23, json::object(), {1, 2, 3}}));
CHECK(*it2 == json(42.23)); CHECK(*it2 == json(42.23));
} }
@ -340,13 +340,13 @@ TEST_CASE("element access 1")
{ {
{ {
json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}}; json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
json::iterator it2 = jarray.erase(jarray.begin() + 3, jarray.begin() + 6); json::iterator const it2 = jarray.erase(jarray.begin() + 3, jarray.begin() + 6);
CHECK(jarray == json({1, 1u, true, json::object(), {1, 2, 3}})); CHECK(jarray == json({1, 1u, true, json::object(), {1, 2, 3}}));
CHECK(*it2 == json::object()); CHECK(*it2 == json::object());
} }
{ {
json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}}; json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
json::const_iterator it2 = jarray.erase(jarray.cbegin() + 3, jarray.cbegin() + 6); json::const_iterator const it2 = jarray.erase(jarray.cbegin() + 3, jarray.cbegin() + 6);
CHECK(jarray == json({1, 1u, true, json::object(), {1, 2, 3}})); CHECK(jarray == json({1, 1u, true, json::object(), {1, 2, 3}}));
CHECK(*it2 == json::object()); CHECK(*it2 == json::object());
} }
@ -369,7 +369,7 @@ TEST_CASE("element access 1")
} }
{ {
json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}}; json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
json jarray2 = {"foo", "bar"}; json const jarray2 = {"foo", "bar"};
CHECK_THROWS_WITH_AS(jarray.erase(jarray2.cbegin()), CHECK_THROWS_WITH_AS(jarray.erase(jarray2.cbegin()),
"[json.exception.invalid_iterator.202] iterator does not fit current value", json::invalid_iterator&); "[json.exception.invalid_iterator.202] iterator does not fit current value", json::invalid_iterator&);

View File

@ -21,7 +21,7 @@ TEST_CASE("object inspection")
{ {
SECTION("object") SECTION("object")
{ {
json j {{"foo", 1}, {"bar", false}}; json const j {{"foo", 1}, {"bar", false}};
CHECK(!j.is_null()); CHECK(!j.is_null());
CHECK(!j.is_boolean()); CHECK(!j.is_boolean());
CHECK(!j.is_number()); CHECK(!j.is_number());
@ -39,7 +39,7 @@ TEST_CASE("object inspection")
SECTION("array") SECTION("array")
{ {
json j {"foo", 1, 1u, 42.23, false}; json const j {"foo", 1, 1u, 42.23, false};
CHECK(!j.is_null()); CHECK(!j.is_null());
CHECK(!j.is_boolean()); CHECK(!j.is_boolean());
CHECK(!j.is_number()); CHECK(!j.is_number());
@ -57,7 +57,7 @@ TEST_CASE("object inspection")
SECTION("null") SECTION("null")
{ {
json j(nullptr); json const j(nullptr);
CHECK(j.is_null()); CHECK(j.is_null());
CHECK(!j.is_boolean()); CHECK(!j.is_boolean());
CHECK(!j.is_number()); CHECK(!j.is_number());
@ -75,7 +75,7 @@ TEST_CASE("object inspection")
SECTION("boolean") SECTION("boolean")
{ {
json j(true); json const j(true);
CHECK(!j.is_null()); CHECK(!j.is_null());
CHECK(j.is_boolean()); CHECK(j.is_boolean());
CHECK(!j.is_number()); CHECK(!j.is_number());
@ -93,7 +93,7 @@ TEST_CASE("object inspection")
SECTION("string") SECTION("string")
{ {
json j("Hello world"); json const j("Hello world");
CHECK(!j.is_null()); CHECK(!j.is_null());
CHECK(!j.is_boolean()); CHECK(!j.is_boolean());
CHECK(!j.is_number()); CHECK(!j.is_number());
@ -111,7 +111,7 @@ TEST_CASE("object inspection")
SECTION("number (integer)") SECTION("number (integer)")
{ {
json j(42); json const j(42);
CHECK(!j.is_null()); CHECK(!j.is_null());
CHECK(!j.is_boolean()); CHECK(!j.is_boolean());
CHECK(j.is_number()); CHECK(j.is_number());
@ -129,7 +129,7 @@ TEST_CASE("object inspection")
SECTION("number (unsigned)") SECTION("number (unsigned)")
{ {
json j(42u); json const j(42u);
CHECK(!j.is_null()); CHECK(!j.is_null());
CHECK(!j.is_boolean()); CHECK(!j.is_boolean());
CHECK(j.is_number()); CHECK(j.is_number());
@ -147,7 +147,7 @@ TEST_CASE("object inspection")
SECTION("number (floating-point)") SECTION("number (floating-point)")
{ {
json j(42.23); json const j(42.23);
CHECK(!j.is_null()); CHECK(!j.is_null());
CHECK(!j.is_boolean()); CHECK(!j.is_boolean());
CHECK(j.is_number()); CHECK(j.is_number());
@ -165,7 +165,7 @@ TEST_CASE("object inspection")
SECTION("binary") SECTION("binary")
{ {
json j(json::value_t::binary); json const j(json::value_t::binary);
CHECK(!j.is_null()); CHECK(!j.is_null());
CHECK(!j.is_boolean()); CHECK(!j.is_boolean());
CHECK(!j.is_number()); CHECK(!j.is_number());
@ -183,7 +183,7 @@ TEST_CASE("object inspection")
SECTION("discarded") SECTION("discarded")
{ {
json j(json::value_t::discarded); json const j(json::value_t::discarded);
CHECK(!j.is_null()); CHECK(!j.is_null());
CHECK(!j.is_boolean()); CHECK(!j.is_boolean());
CHECK(!j.is_number()); CHECK(!j.is_number());
@ -202,7 +202,7 @@ TEST_CASE("object inspection")
SECTION("serialization") SECTION("serialization")
{ {
json j {{"object", json::object()}, {"array", {1, 2, 3, 4}}, {"number", 42}, {"boolean", false}, {"null", nullptr}, {"string", "Hello world"} }; json const j {{"object", json::object()}, {"array", {1, 2, 3, 4}}, {"number", 42}, {"boolean", false}, {"null", nullptr}, {"string", "Hello world"} };
SECTION("no indent / indent=-1") SECTION("no indent / indent=-1")
{ {
@ -288,7 +288,7 @@ TEST_CASE("object inspection")
std::ifstream f_escaped(TEST_DATA_DIRECTORY "/json_nlohmann_tests/all_unicode_ascii.json"); std::ifstream f_escaped(TEST_DATA_DIRECTORY "/json_nlohmann_tests/all_unicode_ascii.json");
std::ifstream f_unescaped(TEST_DATA_DIRECTORY "/json_nlohmann_tests/all_unicode.json"); std::ifstream f_unescaped(TEST_DATA_DIRECTORY "/json_nlohmann_tests/all_unicode.json");
json value = json::parse(f_unescaped); json const value = json::parse(f_unescaped);
std::string text = value.dump(4, ' ', true); std::string text = value.dump(4, ' ', true);
std::string expected((std::istreambuf_iterator<char>(f_escaped)), std::string expected((std::istreambuf_iterator<char>(f_escaped)),
@ -299,7 +299,7 @@ TEST_CASE("object inspection")
SECTION("serialization of discarded element") SECTION("serialization of discarded element")
{ {
json j_discarded(json::value_t::discarded); json const j_discarded(json::value_t::discarded);
CHECK(j_discarded.dump() == "<discarded>"); CHECK(j_discarded.dump() == "<discarded>");
} }
@ -315,7 +315,7 @@ TEST_CASE("object inspection")
ss.str(std::string()); ss.str(std::string());
// use stringstream for JSON serialization // use stringstream for JSON serialization
json j_number = 3.14159265358979; json const j_number = 3.14159265358979;
ss << j_number; ss << j_number;
// check that precision has been overridden during serialization // check that precision has been overridden during serialization
@ -332,9 +332,9 @@ TEST_CASE("object inspection")
{"3.141592653589793", "1000000000000000010E5" {"3.141592653589793", "1000000000000000010E5"
}) })
{ {
json j1 = json::parse(s); json const j1 = json::parse(s);
std::string s1 = j1.dump(); std::string s1 = j1.dump();
json j2 = json::parse(s1); json const j2 = json::parse(s1);
std::string s2 = j2.dump(); std::string s2 = j2.dump();
CHECK(s1 == s2); CHECK(s1 == s2);
} }
@ -344,49 +344,49 @@ TEST_CASE("object inspection")
{ {
SECTION("null") SECTION("null")
{ {
json j = nullptr; json const j = nullptr;
CHECK(j.type() == json::value_t::null); CHECK(j.type() == json::value_t::null);
} }
SECTION("object") SECTION("object")
{ {
json j = {{"foo", "bar"}}; json const j = {{"foo", "bar"}};
CHECK(j.type() == json::value_t::object); CHECK(j.type() == json::value_t::object);
} }
SECTION("array") SECTION("array")
{ {
json j = {1, 2, 3, 4}; json const j = {1, 2, 3, 4};
CHECK(j.type() == json::value_t::array); CHECK(j.type() == json::value_t::array);
} }
SECTION("boolean") SECTION("boolean")
{ {
json j = true; json const j = true;
CHECK(j.type() == json::value_t::boolean); CHECK(j.type() == json::value_t::boolean);
} }
SECTION("string") SECTION("string")
{ {
json j = "Hello world"; json const j = "Hello world";
CHECK(j.type() == json::value_t::string); CHECK(j.type() == json::value_t::string);
} }
SECTION("number (integer)") SECTION("number (integer)")
{ {
json j = 23; json const j = 23;
CHECK(j.type() == json::value_t::number_integer); CHECK(j.type() == json::value_t::number_integer);
} }
SECTION("number (unsigned)") SECTION("number (unsigned)")
{ {
json j = 23u; json const j = 23u;
CHECK(j.type() == json::value_t::number_unsigned); CHECK(j.type() == json::value_t::number_unsigned);
} }
SECTION("number (floating-point)") SECTION("number (floating-point)")
{ {
json j = 42.23; json const j = 42.23;
CHECK(j.type() == json::value_t::number_float); CHECK(j.type() == json::value_t::number_float);
} }
} }
@ -395,63 +395,63 @@ TEST_CASE("object inspection")
{ {
SECTION("null") SECTION("null")
{ {
json j = nullptr; json const j = nullptr;
json::value_t t = j; json::value_t t = j;
CHECK(t == j.type()); CHECK(t == j.type());
} }
SECTION("object") SECTION("object")
{ {
json j = {{"foo", "bar"}}; json const j = {{"foo", "bar"}};
json::value_t t = j; json::value_t t = j;
CHECK(t == j.type()); CHECK(t == j.type());
} }
SECTION("array") SECTION("array")
{ {
json j = {1, 2, 3, 4}; json const j = {1, 2, 3, 4};
json::value_t t = j; json::value_t t = j;
CHECK(t == j.type()); CHECK(t == j.type());
} }
SECTION("boolean") SECTION("boolean")
{ {
json j = true; json const j = true;
json::value_t t = j; json::value_t t = j;
CHECK(t == j.type()); CHECK(t == j.type());
} }
SECTION("string") SECTION("string")
{ {
json j = "Hello world"; json const j = "Hello world";
json::value_t t = j; json::value_t t = j;
CHECK(t == j.type()); CHECK(t == j.type());
} }
SECTION("number (integer)") SECTION("number (integer)")
{ {
json j = 23; json const j = 23;
json::value_t t = j; json::value_t t = j;
CHECK(t == j.type()); CHECK(t == j.type());
} }
SECTION("number (unsigned)") SECTION("number (unsigned)")
{ {
json j = 23u; json const j = 23u;
json::value_t t = j; json::value_t t = j;
CHECK(t == j.type()); CHECK(t == j.type());
} }
SECTION("number (floating-point)") SECTION("number (floating-point)")
{ {
json j = 42.23; json const j = 42.23;
json::value_t t = j; json::value_t t = j;
CHECK(t == j.type()); CHECK(t == j.type());
} }
SECTION("binary") SECTION("binary")
{ {
json j = json::binary({}); json const j = json::binary({});
json::value_t t = j; json::value_t t = j;
CHECK(t == j.type()); CHECK(t == j.type());
} }

View File

@ -35,13 +35,13 @@ TEST_CASE("JSON patch")
SECTION("4.1 add") SECTION("4.1 add")
{ {
json patch1 = R"([{ "op": "add", "path": "/a/b", "value": [ "foo", "bar" ] }])"_json; json const patch1 = R"([{ "op": "add", "path": "/a/b", "value": [ "foo", "bar" ] }])"_json;
// However, the object itself or an array containing it does need // However, the object itself or an array containing it does need
// to exist, and it remains an error for that not to be the case. // to exist, and it remains an error for that not to be the case.
// For example, an "add" with a target location of "/a/b" starting // For example, an "add" with a target location of "/a/b" starting
// with this document // with this document
json doc1 = R"({ "a": { "foo": 1 } })"_json; json const doc1 = R"({ "a": { "foo": 1 } })"_json;
// is not an error, because "a" exists, and "b" will be added to // is not an error, because "a" exists, and "b" will be added to
// its value. // its value.
@ -57,13 +57,13 @@ TEST_CASE("JSON patch")
CHECK(doc1.patch(patch1) == doc1_ans); CHECK(doc1.patch(patch1) == doc1_ans);
// It is an error in this document: // It is an error in this document:
json doc2 = R"({ "q": { "bar": 2 } })"_json; json const doc2 = R"({ "q": { "bar": 2 } })"_json;
// because "a" does not exist. // because "a" does not exist.
CHECK_THROWS_WITH_AS(doc2.patch(patch1), "[json.exception.out_of_range.403] key 'a' not found", json::out_of_range&); CHECK_THROWS_WITH_AS(doc2.patch(patch1), "[json.exception.out_of_range.403] key 'a' not found", json::out_of_range&);
json doc3 = R"({ "a": {} })"_json; json const doc3 = R"({ "a": {} })"_json;
json patch2 = R"([{ "op": "add", "path": "/a/b/c", "value": 1 }])"_json; json const patch2 = R"([{ "op": "add", "path": "/a/b/c", "value": 1 }])"_json;
// should cause an error because "b" does not exist in doc3 // should cause an error because "b" does not exist in doc3
#if JSON_DIAGNOSTICS #if JSON_DIAGNOSTICS
@ -77,20 +77,20 @@ TEST_CASE("JSON patch")
{ {
// If removing an element from an array, any elements above the // If removing an element from an array, any elements above the
// specified index are shifted one position to the left. // specified index are shifted one position to the left.
json doc = {1, 2, 3, 4}; json const doc = {1, 2, 3, 4};
json patch = {{{"op", "remove"}, {"path", "/1"}}}; json const patch = {{{"op", "remove"}, {"path", "/1"}}};
CHECK(doc.patch(patch) == json({1, 3, 4})); CHECK(doc.patch(patch) == json({1, 3, 4}));
} }
SECTION("A.1. Adding an Object Member") SECTION("A.1. Adding an Object Member")
{ {
// An example target JSON document: // An example target JSON document:
json doc = R"( json const doc = R"(
{ "foo": "bar"} { "foo": "bar"}
)"_json; )"_json;
// A JSON Patch document: // A JSON Patch document:
json patch = R"( json const patch = R"(
[ [
{ "op": "add", "path": "/baz", "value": "qux" } { "op": "add", "path": "/baz", "value": "qux" }
] ]
@ -114,12 +114,12 @@ TEST_CASE("JSON patch")
SECTION("A.2. Adding an Array Element") SECTION("A.2. Adding an Array Element")
{ {
// An example target JSON document: // An example target JSON document:
json doc = R"( json const doc = R"(
{ "foo": [ "bar", "baz" ] } { "foo": [ "bar", "baz" ] }
)"_json; )"_json;
// A JSON Patch document: // A JSON Patch document:
json patch = R"( json const patch = R"(
[ [
{ "op": "add", "path": "/foo/1", "value": "qux" } { "op": "add", "path": "/foo/1", "value": "qux" }
] ]
@ -140,7 +140,7 @@ TEST_CASE("JSON patch")
SECTION("A.3. Removing an Object Member") SECTION("A.3. Removing an Object Member")
{ {
// An example target JSON document: // An example target JSON document:
json doc = R"( json const doc = R"(
{ {
"baz": "qux", "baz": "qux",
"foo": "bar" "foo": "bar"
@ -148,7 +148,7 @@ TEST_CASE("JSON patch")
)"_json; )"_json;
// A JSON Patch document: // A JSON Patch document:
json patch = R"( json const patch = R"(
[ [
{ "op": "remove", "path": "/baz" } { "op": "remove", "path": "/baz" }
] ]
@ -169,12 +169,12 @@ TEST_CASE("JSON patch")
SECTION("A.4. Removing an Array Element") SECTION("A.4. Removing an Array Element")
{ {
// An example target JSON document: // An example target JSON document:
json doc = R"( json const doc = R"(
{ "foo": [ "bar", "qux", "baz" ] } { "foo": [ "bar", "qux", "baz" ] }
)"_json; )"_json;
// A JSON Patch document: // A JSON Patch document:
json patch = R"( json const patch = R"(
[ [
{ "op": "remove", "path": "/foo/1" } { "op": "remove", "path": "/foo/1" }
] ]
@ -195,7 +195,7 @@ TEST_CASE("JSON patch")
SECTION("A.5. Replacing a Value") SECTION("A.5. Replacing a Value")
{ {
// An example target JSON document: // An example target JSON document:
json doc = R"( json const doc = R"(
{ {
"baz": "qux", "baz": "qux",
"foo": "bar" "foo": "bar"
@ -203,7 +203,7 @@ TEST_CASE("JSON patch")
)"_json; )"_json;
// A JSON Patch document: // A JSON Patch document:
json patch = R"( json const patch = R"(
[ [
{ "op": "replace", "path": "/baz", "value": "boo" } { "op": "replace", "path": "/baz", "value": "boo" }
] ]
@ -226,7 +226,7 @@ TEST_CASE("JSON patch")
SECTION("A.6. Moving a Value") SECTION("A.6. Moving a Value")
{ {
// An example target JSON document: // An example target JSON document:
json doc = R"( json const doc = R"(
{ {
"foo": { "foo": {
"bar": "baz", "bar": "baz",
@ -239,7 +239,7 @@ TEST_CASE("JSON patch")
)"_json; )"_json;
// A JSON Patch document: // A JSON Patch document:
json patch = R"( json const patch = R"(
[ [
{ "op": "move", "from": "/foo/waldo", "path": "/qux/thud" } { "op": "move", "from": "/foo/waldo", "path": "/qux/thud" }
] ]
@ -268,12 +268,12 @@ TEST_CASE("JSON patch")
SECTION("A.7. Moving a Value") SECTION("A.7. Moving a Value")
{ {
// An example target JSON document: // An example target JSON document:
json doc = R"( json const doc = R"(
{ "foo": [ "all", "grass", "cows", "eat" ] } { "foo": [ "all", "grass", "cows", "eat" ] }
)"_json; )"_json;
// A JSON Patch document: // A JSON Patch document:
json patch = R"( json const patch = R"(
[ [
{ "op": "move", "from": "/foo/1", "path": "/foo/3" } { "op": "move", "from": "/foo/1", "path": "/foo/3" }
] ]
@ -302,7 +302,7 @@ TEST_CASE("JSON patch")
)"_json; )"_json;
// A JSON Patch document that will result in successful evaluation: // A JSON Patch document that will result in successful evaluation:
json patch = R"( json const patch = R"(
[ [
{ "op": "test", "path": "/baz", "value": "qux" }, { "op": "test", "path": "/baz", "value": "qux" },
{ "op": "test", "path": "/foo/1", "value": 2 } { "op": "test", "path": "/foo/1", "value": 2 }
@ -318,7 +318,7 @@ TEST_CASE("JSON patch")
SECTION("A.9. Testing a Value: Error") SECTION("A.9. Testing a Value: Error")
{ {
// An example target JSON document: // An example target JSON document:
json doc = R"( json const doc = R"(
{ "baz": "qux" } { "baz": "qux" }
)"_json; )"_json;
@ -341,12 +341,12 @@ TEST_CASE("JSON patch")
SECTION("A.10. Adding a Nested Member Object") SECTION("A.10. Adding a Nested Member Object")
{ {
// An example target JSON document: // An example target JSON document:
json doc = R"( json const doc = R"(
{ "foo": "bar" } { "foo": "bar" }
)"_json; )"_json;
// A JSON Patch document: // A JSON Patch document:
json patch = R"( json const patch = R"(
[ [
{ "op": "add", "path": "/child", "value": { "grandchild": { } } } { "op": "add", "path": "/child", "value": { "grandchild": { } } }
] ]
@ -373,12 +373,12 @@ TEST_CASE("JSON patch")
SECTION("A.11. Ignoring Unrecognized Elements") SECTION("A.11. Ignoring Unrecognized Elements")
{ {
// An example target JSON document: // An example target JSON document:
json doc = R"( json const doc = R"(
{ "foo": "bar" } { "foo": "bar" }
)"_json; )"_json;
// A JSON Patch document: // A JSON Patch document:
json patch = R"( json const patch = R"(
[ [
{ "op": "add", "path": "/baz", "value": "qux", "xyz": 123 } { "op": "add", "path": "/baz", "value": "qux", "xyz": 123 }
] ]
@ -401,12 +401,12 @@ TEST_CASE("JSON patch")
SECTION("A.12. Adding to a Nonexistent Target") SECTION("A.12. Adding to a Nonexistent Target")
{ {
// An example target JSON document: // An example target JSON document:
json doc = R"( json const doc = R"(
{ "foo": "bar" } { "foo": "bar" }
)"_json; )"_json;
// A JSON Patch document: // A JSON Patch document:
json patch = R"( json const patch = R"(
[ [
{ "op": "add", "path": "/baz/bat", "value": "qux" } { "op": "add", "path": "/baz/bat", "value": "qux" }
] ]
@ -427,7 +427,7 @@ TEST_CASE("JSON patch")
SECTION("A.14. Escape Ordering") SECTION("A.14. Escape Ordering")
{ {
// An example target JSON document: // An example target JSON document:
json doc = R"( json const doc = R"(
{ {
"/": 9, "/": 9,
"~1": 10 "~1": 10
@ -435,7 +435,7 @@ TEST_CASE("JSON patch")
)"_json; )"_json;
// A JSON Patch document: // A JSON Patch document:
json patch = R"( json const patch = R"(
[ [
{"op": "test", "path": "/~01", "value": 10} {"op": "test", "path": "/~01", "value": 10}
] ]
@ -458,7 +458,7 @@ TEST_CASE("JSON patch")
SECTION("A.15. Comparing Strings and Numbers") SECTION("A.15. Comparing Strings and Numbers")
{ {
// An example target JSON document: // An example target JSON document:
json doc = R"( json const doc = R"(
{ {
"/": 9, "/": 9,
"~1": 10 "~1": 10
@ -484,12 +484,12 @@ TEST_CASE("JSON patch")
SECTION("A.16. Adding an Array Value") SECTION("A.16. Adding an Array Value")
{ {
// An example target JSON document: // An example target JSON document:
json doc = R"( json const doc = R"(
{ "foo": ["bar"] } { "foo": ["bar"] }
)"_json; )"_json;
// A JSON Patch document: // A JSON Patch document:
json patch = R"( json const patch = R"(
[ [
{ "op": "add", "path": "/foo/-", "value": ["abc", "def"] } { "op": "add", "path": "/foo/-", "value": ["abc", "def"] }
] ]
@ -519,10 +519,10 @@ TEST_CASE("JSON patch")
// document. // document.
// An example target JSON document: // An example target JSON document:
json doc = 17; json const doc = 17;
// A JSON Patch document: // A JSON Patch document:
json patch = R"( json const patch = R"(
[ [
{ "op": "add", "path": "", "value": [1,2,3] } { "op": "add", "path": "", "value": [1,2,3] }
] ]
@ -545,10 +545,10 @@ TEST_CASE("JSON patch")
// exactly the number of elements in the array which is legal. // exactly the number of elements in the array which is legal.
// An example target JSON document: // An example target JSON document:
json doc = {0, 1, 2}; json const doc = {0, 1, 2};
// A JSON Patch document: // A JSON Patch document:
json patch = R"( json const patch = R"(
[ [
{ "op": "add", "path": "/3", "value": 3 } { "op": "add", "path": "/3", "value": 3 }
] ]
@ -568,7 +568,7 @@ TEST_CASE("JSON patch")
SECTION("copy") SECTION("copy")
{ {
// An example target JSON document: // An example target JSON document:
json doc = R"( json const doc = R"(
{ {
"foo": { "foo": {
"bar": "baz", "bar": "baz",
@ -581,7 +581,7 @@ TEST_CASE("JSON patch")
)"_json; )"_json;
// A JSON Patch document: // A JSON Patch document:
json patch = R"( json const patch = R"(
[ [
{ "op": "copy", "from": "/foo/waldo", "path": "/qux/thud" } { "op": "copy", "from": "/foo/waldo", "path": "/qux/thud" }
] ]
@ -610,8 +610,8 @@ TEST_CASE("JSON patch")
SECTION("replace") SECTION("replace")
{ {
json j = "string"; json const j = "string";
json patch = {{{"op", "replace"}, {"path", ""}, {"value", 1}}}; json const patch = {{{"op", "replace"}, {"path", ""}, {"value", 1}}};
CHECK(j.patch(patch) == json(1)); CHECK(j.patch(patch) == json(1));
} }
@ -619,12 +619,12 @@ TEST_CASE("JSON patch")
{ {
{ {
// a JSON patch // a JSON patch
json p1 = R"( json const p1 = R"(
[{"op": "add", "path": "/GB", "value": "London"}] [{"op": "add", "path": "/GB", "value": "London"}]
)"_json; )"_json;
// a JSON value // a JSON value
json source = R"( json const source = R"(
{"D": "Berlin", "F": "Paris"} {"D": "Berlin", "F": "Paris"}
)"_json; )"_json;
@ -665,15 +665,15 @@ TEST_CASE("JSON patch")
{ {
SECTION("not an array") SECTION("not an array")
{ {
json j; json const j;
json patch = {{"op", "add"}, {"path", ""}, {"value", 1}}; json const patch = {{"op", "add"}, {"path", ""}, {"value", 1}};
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.104] parse error: JSON patch must be an array of objects", json::parse_error&); CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.104] parse error: JSON patch must be an array of objects", json::parse_error&);
} }
SECTION("not an array of objects") SECTION("not an array of objects")
{ {
json j; json const j;
json patch = {"op", "add", "path", "", "value", 1}; json const patch = {"op", "add", "path", "", "value", 1};
#if JSON_DIAGNOSTICS #if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.104] parse error: (/0) JSON patch must be an array of objects", json::parse_error&); CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.104] parse error: (/0) JSON patch must be an array of objects", json::parse_error&);
#else #else
@ -683,8 +683,8 @@ TEST_CASE("JSON patch")
SECTION("missing 'op'") SECTION("missing 'op'")
{ {
json j; json const j;
json patch = {{{"foo", "bar"}}}; json const patch = {{{"foo", "bar"}}};
#if JSON_DIAGNOSTICS #if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation must have member 'op'", json::parse_error&); CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation must have member 'op'", json::parse_error&);
#else #else
@ -694,8 +694,8 @@ TEST_CASE("JSON patch")
SECTION("non-string 'op'") SECTION("non-string 'op'")
{ {
json j; json const j;
json patch = {{{"op", 1}}}; json const patch = {{{"op", 1}}};
#if JSON_DIAGNOSTICS #if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation must have string member 'op'", json::parse_error&); CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation must have string member 'op'", json::parse_error&);
#else #else
@ -705,8 +705,8 @@ TEST_CASE("JSON patch")
SECTION("invalid operation") SECTION("invalid operation")
{ {
json j; json const j;
json patch = {{{"op", "foo"}, {"path", ""}}}; json const patch = {{{"op", "foo"}, {"path", ""}}};
#if JSON_DIAGNOSTICS #if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation value 'foo' is invalid", json::parse_error&); CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation value 'foo' is invalid", json::parse_error&);
#else #else
@ -719,8 +719,8 @@ TEST_CASE("JSON patch")
{ {
SECTION("missing 'path'") SECTION("missing 'path'")
{ {
json j; json const j;
json patch = {{{"op", "add"}}}; json const patch = {{{"op", "add"}}};
#if JSON_DIAGNOSTICS #if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'add' must have member 'path'", json::parse_error&); CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'add' must have member 'path'", json::parse_error&);
#else #else
@ -730,8 +730,8 @@ TEST_CASE("JSON patch")
SECTION("non-string 'path'") SECTION("non-string 'path'")
{ {
json j; json const j;
json patch = {{{"op", "add"}, {"path", 1}}}; json const patch = {{{"op", "add"}, {"path", 1}}};
#if JSON_DIAGNOSTICS #if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'add' must have string member 'path'", json::parse_error&); CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'add' must have string member 'path'", json::parse_error&);
#else #else
@ -741,8 +741,8 @@ TEST_CASE("JSON patch")
SECTION("missing 'value'") SECTION("missing 'value'")
{ {
json j; json const j;
json patch = {{{"op", "add"}, {"path", ""}}}; json const patch = {{{"op", "add"}, {"path", ""}}};
#if JSON_DIAGNOSTICS #if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'add' must have member 'value'", json::parse_error&); CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'add' must have member 'value'", json::parse_error&);
#else #else
@ -752,8 +752,8 @@ TEST_CASE("JSON patch")
SECTION("invalid array index") SECTION("invalid array index")
{ {
json j = {1, 2}; json const j = {1, 2};
json patch = {{{"op", "add"}, {"path", "/4"}, {"value", 4}}}; json const patch = {{{"op", "add"}, {"path", "/4"}, {"value", 4}}};
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.out_of_range.401] array index 4 is out of range", json::out_of_range&); CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.out_of_range.401] array index 4 is out of range", json::out_of_range&);
} }
} }
@ -762,8 +762,8 @@ TEST_CASE("JSON patch")
{ {
SECTION("missing 'path'") SECTION("missing 'path'")
{ {
json j; json const j;
json patch = {{{"op", "remove"}}}; json const patch = {{{"op", "remove"}}};
#if JSON_DIAGNOSTICS #if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'remove' must have member 'path'", json::parse_error&); CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'remove' must have member 'path'", json::parse_error&);
#else #else
@ -773,8 +773,8 @@ TEST_CASE("JSON patch")
SECTION("non-string 'path'") SECTION("non-string 'path'")
{ {
json j; json const j;
json patch = {{{"op", "remove"}, {"path", 1}}}; json const patch = {{{"op", "remove"}, {"path", 1}}};
#if JSON_DIAGNOSTICS #if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'remove' must have string member 'path'", json::parse_error&); CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'remove' must have string member 'path'", json::parse_error&);
#else #else
@ -784,22 +784,22 @@ TEST_CASE("JSON patch")
SECTION("nonexisting target location (array)") SECTION("nonexisting target location (array)")
{ {
json j = {1, 2, 3}; json const j = {1, 2, 3};
json patch = {{{"op", "remove"}, {"path", "/17"}}}; json const patch = {{{"op", "remove"}, {"path", "/17"}}};
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.out_of_range.401] array index 17 is out of range", json::out_of_range&); CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.out_of_range.401] array index 17 is out of range", json::out_of_range&);
} }
SECTION("nonexisting target location (object)") SECTION("nonexisting target location (object)")
{ {
json j = {{"foo", 1}, {"bar", 2}}; json const j = {{"foo", 1}, {"bar", 2}};
json patch = {{{"op", "remove"}, {"path", "/baz"}}}; json const patch = {{{"op", "remove"}, {"path", "/baz"}}};
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.out_of_range.403] key 'baz' not found", json::out_of_range&); CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.out_of_range.403] key 'baz' not found", json::out_of_range&);
} }
SECTION("root element as target location") SECTION("root element as target location")
{ {
json j = "string"; json const j = "string";
json patch = {{{"op", "remove"}, {"path", ""}}}; json const patch = {{{"op", "remove"}, {"path", ""}}};
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.out_of_range.405] JSON pointer has no parent", json::out_of_range&); CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.out_of_range.405] JSON pointer has no parent", json::out_of_range&);
} }
} }
@ -808,8 +808,8 @@ TEST_CASE("JSON patch")
{ {
SECTION("missing 'path'") SECTION("missing 'path'")
{ {
json j; json const j;
json patch = {{{"op", "replace"}}}; json const patch = {{{"op", "replace"}}};
#if JSON_DIAGNOSTICS #if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'replace' must have member 'path'", json::parse_error&); CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'replace' must have member 'path'", json::parse_error&);
#else #else
@ -819,8 +819,8 @@ TEST_CASE("JSON patch")
SECTION("non-string 'path'") SECTION("non-string 'path'")
{ {
json j; json const j;
json patch = {{{"op", "replace"}, {"path", 1}}}; json const patch = {{{"op", "replace"}, {"path", 1}}};
#if JSON_DIAGNOSTICS #if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'replace' must have string member 'path'", json::parse_error&); CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'replace' must have string member 'path'", json::parse_error&);
#else #else
@ -830,8 +830,8 @@ TEST_CASE("JSON patch")
SECTION("missing 'value'") SECTION("missing 'value'")
{ {
json j; json const j;
json patch = {{{"op", "replace"}, {"path", ""}}}; json const patch = {{{"op", "replace"}, {"path", ""}}};
#if JSON_DIAGNOSTICS #if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'replace' must have member 'value'", json::parse_error&); CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'replace' must have member 'value'", json::parse_error&);
#else #else
@ -841,15 +841,15 @@ TEST_CASE("JSON patch")
SECTION("nonexisting target location (array)") SECTION("nonexisting target location (array)")
{ {
json j = {1, 2, 3}; json const j = {1, 2, 3};
json patch = {{{"op", "replace"}, {"path", "/17"}, {"value", 19}}}; json const patch = {{{"op", "replace"}, {"path", "/17"}, {"value", 19}}};
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.out_of_range.401] array index 17 is out of range", json::out_of_range&); CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.out_of_range.401] array index 17 is out of range", json::out_of_range&);
} }
SECTION("nonexisting target location (object)") SECTION("nonexisting target location (object)")
{ {
json j = {{"foo", 1}, {"bar", 2}}; json const j = {{"foo", 1}, {"bar", 2}};
json patch = {{{"op", "replace"}, {"path", "/baz"}, {"value", 3}}}; json const patch = {{{"op", "replace"}, {"path", "/baz"}, {"value", 3}}};
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.out_of_range.403] key 'baz' not found", json::out_of_range&); CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.out_of_range.403] key 'baz' not found", json::out_of_range&);
} }
} }
@ -858,8 +858,8 @@ TEST_CASE("JSON patch")
{ {
SECTION("missing 'path'") SECTION("missing 'path'")
{ {
json j; json const j;
json patch = {{{"op", "move"}}}; json const patch = {{{"op", "move"}}};
#if JSON_DIAGNOSTICS #if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'move' must have member 'path'", json::parse_error&); CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'move' must have member 'path'", json::parse_error&);
#else #else
@ -869,8 +869,8 @@ TEST_CASE("JSON patch")
SECTION("non-string 'path'") SECTION("non-string 'path'")
{ {
json j; json const j;
json patch = {{{"op", "move"}, {"path", 1}}}; json const patch = {{{"op", "move"}, {"path", 1}}};
#if JSON_DIAGNOSTICS #if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'move' must have string member 'path'", json::parse_error&); CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'move' must have string member 'path'", json::parse_error&);
#else #else
@ -880,8 +880,8 @@ TEST_CASE("JSON patch")
SECTION("missing 'from'") SECTION("missing 'from'")
{ {
json j; json const j;
json patch = {{{"op", "move"}, {"path", ""}}}; json const patch = {{{"op", "move"}, {"path", ""}}};
CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_AS(j.patch(patch), json::parse_error&);
#if JSON_DIAGNOSTICS #if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'move' must have member 'from'", json::parse_error&); CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'move' must have member 'from'", json::parse_error&);
@ -892,8 +892,8 @@ TEST_CASE("JSON patch")
SECTION("non-string 'from'") SECTION("non-string 'from'")
{ {
json j; json const j;
json patch = {{{"op", "move"}, {"path", ""}, {"from", 1}}}; json const patch = {{{"op", "move"}, {"path", ""}, {"from", 1}}};
CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_AS(j.patch(patch), json::parse_error&);
#if JSON_DIAGNOSTICS #if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'move' must have string member 'from'", json::parse_error&); CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'move' must have string member 'from'", json::parse_error&);
@ -904,15 +904,15 @@ TEST_CASE("JSON patch")
SECTION("nonexisting from location (array)") SECTION("nonexisting from location (array)")
{ {
json j = {1, 2, 3}; json const j = {1, 2, 3};
json patch = {{{"op", "move"}, {"path", "/0"}, {"from", "/5"}}}; json const patch = {{{"op", "move"}, {"path", "/0"}, {"from", "/5"}}};
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.out_of_range.401] array index 5 is out of range", json::out_of_range&); CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.out_of_range.401] array index 5 is out of range", json::out_of_range&);
} }
SECTION("nonexisting from location (object)") SECTION("nonexisting from location (object)")
{ {
json j = {{"foo", 1}, {"bar", 2}}; json const j = {{"foo", 1}, {"bar", 2}};
json patch = {{{"op", "move"}, {"path", "/baz"}, {"from", "/baz"}}}; json const patch = {{{"op", "move"}, {"path", "/baz"}, {"from", "/baz"}}};
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.out_of_range.403] key 'baz' not found", json::out_of_range&); CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.out_of_range.403] key 'baz' not found", json::out_of_range&);
} }
} }
@ -921,8 +921,8 @@ TEST_CASE("JSON patch")
{ {
SECTION("missing 'path'") SECTION("missing 'path'")
{ {
json j; json const j;
json patch = {{{"op", "copy"}}}; json const patch = {{{"op", "copy"}}};
#if JSON_DIAGNOSTICS #if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'copy' must have member 'path'", json::parse_error&); CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'copy' must have member 'path'", json::parse_error&);
#else #else
@ -932,8 +932,8 @@ TEST_CASE("JSON patch")
SECTION("non-string 'path'") SECTION("non-string 'path'")
{ {
json j; json const j;
json patch = {{{"op", "copy"}, {"path", 1}}}; json const patch = {{{"op", "copy"}, {"path", 1}}};
#if JSON_DIAGNOSTICS #if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'copy' must have string member 'path'", json::parse_error&); CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'copy' must have string member 'path'", json::parse_error&);
#else #else
@ -943,8 +943,8 @@ TEST_CASE("JSON patch")
SECTION("missing 'from'") SECTION("missing 'from'")
{ {
json j; json const j;
json patch = {{{"op", "copy"}, {"path", ""}}}; json const patch = {{{"op", "copy"}, {"path", ""}}};
#if JSON_DIAGNOSTICS #if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'copy' must have member 'from'", json::parse_error&); CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'copy' must have member 'from'", json::parse_error&);
#else #else
@ -954,8 +954,8 @@ TEST_CASE("JSON patch")
SECTION("non-string 'from'") SECTION("non-string 'from'")
{ {
json j; json const j;
json patch = {{{"op", "copy"}, {"path", ""}, {"from", 1}}}; json const patch = {{{"op", "copy"}, {"path", ""}, {"from", 1}}};
#if JSON_DIAGNOSTICS #if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'copy' must have string member 'from'", json::parse_error&); CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'copy' must have string member 'from'", json::parse_error&);
#else #else
@ -965,15 +965,15 @@ TEST_CASE("JSON patch")
SECTION("nonexisting from location (array)") SECTION("nonexisting from location (array)")
{ {
json j = {1, 2, 3}; json const j = {1, 2, 3};
json patch = {{{"op", "copy"}, {"path", "/0"}, {"from", "/5"}}}; json const patch = {{{"op", "copy"}, {"path", "/0"}, {"from", "/5"}}};
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.out_of_range.401] array index 5 is out of range", json::out_of_range&); CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.out_of_range.401] array index 5 is out of range", json::out_of_range&);
} }
SECTION("nonexisting from location (object)") SECTION("nonexisting from location (object)")
{ {
json j = {{"foo", 1}, {"bar", 2}}; json const j = {{"foo", 1}, {"bar", 2}};
json patch = {{{"op", "copy"}, {"path", "/fob"}, {"from", "/baz"}}}; json const patch = {{{"op", "copy"}, {"path", "/fob"}, {"from", "/baz"}}};
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.out_of_range.403] key 'baz' not found", json::out_of_range&); CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.out_of_range.403] key 'baz' not found", json::out_of_range&);
} }
} }
@ -982,8 +982,8 @@ TEST_CASE("JSON patch")
{ {
SECTION("missing 'path'") SECTION("missing 'path'")
{ {
json j; json const j;
json patch = {{{"op", "test"}}}; json const patch = {{{"op", "test"}}};
#if JSON_DIAGNOSTICS #if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'test' must have member 'path'", json::parse_error&); CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'test' must have member 'path'", json::parse_error&);
#else #else
@ -993,8 +993,8 @@ TEST_CASE("JSON patch")
SECTION("non-string 'path'") SECTION("non-string 'path'")
{ {
json j; json const j;
json patch = {{{"op", "test"}, {"path", 1}}}; json const patch = {{{"op", "test"}, {"path", 1}}};
#if JSON_DIAGNOSTICS #if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'test' must have string member 'path'", json::parse_error&); CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'test' must have string member 'path'", json::parse_error&);
#else #else
@ -1004,8 +1004,8 @@ TEST_CASE("JSON patch")
SECTION("missing 'value'") SECTION("missing 'value'")
{ {
json j; json const j;
json patch = {{{"op", "test"}, {"path", ""}}}; json const patch = {{{"op", "test"}, {"path", ""}}};
#if JSON_DIAGNOSTICS #if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'test' must have member 'value'", json::parse_error&); CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'test' must have member 'value'", json::parse_error&);
#else #else
@ -1020,7 +1020,7 @@ TEST_CASE("JSON patch")
SECTION("Simple Example") SECTION("Simple Example")
{ {
// The original document // The original document
json doc = R"( json const doc = R"(
{ {
"baz": "qux", "baz": "qux",
"foo": "bar" "foo": "bar"
@ -1028,7 +1028,7 @@ TEST_CASE("JSON patch")
)"_json; )"_json;
// The patch // The patch
json patch = R"( json const patch = R"(
[ [
{ "op": "replace", "path": "/baz", "value": "boo" }, { "op": "replace", "path": "/baz", "value": "boo" },
{ "op": "add", "path": "/hello", "value": ["world"] }, { "op": "add", "path": "/hello", "value": ["world"] },
@ -1054,7 +1054,7 @@ TEST_CASE("JSON patch")
SECTION("Operations") SECTION("Operations")
{ {
// The original document // The original document
json doc = R"( json const doc = R"(
{ {
"biscuits": [ "biscuits": [
{"name":"Digestive"}, {"name":"Digestive"},
@ -1066,7 +1066,7 @@ TEST_CASE("JSON patch")
SECTION("add") SECTION("add")
{ {
// The patch // The patch
json patch = R"( json const patch = R"(
[ [
{"op": "add", "path": "/biscuits/1", "value": {"name": "Ginger Nut"}} {"op": "add", "path": "/biscuits/1", "value": {"name": "Ginger Nut"}}
] ]
@ -1093,7 +1093,7 @@ TEST_CASE("JSON patch")
SECTION("remove") SECTION("remove")
{ {
// The patch // The patch
json patch = R"( json const patch = R"(
[ [
{"op": "remove", "path": "/biscuits"} {"op": "remove", "path": "/biscuits"}
] ]
@ -1114,7 +1114,7 @@ TEST_CASE("JSON patch")
SECTION("replace") SECTION("replace")
{ {
// The patch // The patch
json patch = R"( json const patch = R"(
[ [
{"op": "replace", "path": "/biscuits/0/name", "value": "Chocolate Digestive"} {"op": "replace", "path": "/biscuits/0/name", "value": "Chocolate Digestive"}
] ]
@ -1140,7 +1140,7 @@ TEST_CASE("JSON patch")
SECTION("copy") SECTION("copy")
{ {
// The patch // The patch
json patch = R"( json const patch = R"(
[ [
{"op": "copy", "from": "/biscuits/0", "path": "/best_biscuit"} {"op": "copy", "from": "/biscuits/0", "path": "/best_biscuit"}
] ]
@ -1169,7 +1169,7 @@ TEST_CASE("JSON patch")
SECTION("move") SECTION("move")
{ {
// The patch // The patch
json patch = R"( json const patch = R"(
[ [
{"op": "move", "from": "/biscuits", "path": "/cookies"} {"op": "move", "from": "/biscuits", "path": "/cookies"}
] ]
@ -1290,7 +1290,7 @@ TEST_CASE("JSON patch")
{ {
CAPTURE(filename) CAPTURE(filename)
std::ifstream f(filename); std::ifstream f(filename);
json suite = json::parse(f); json const suite = json::parse(f);
for (const auto& test : suite) for (const auto& test : suite)
{ {

View File

@ -40,7 +40,7 @@ TEST_CASE("JSON pointers")
SECTION("array index error") SECTION("array index error")
{ {
json v = {1, 2, 3, 4}; json v = {1, 2, 3, 4};
json::json_pointer ptr("/10e"); json::json_pointer const const ptr("/10e");
CHECK_THROWS_WITH_AS(v[ptr], CHECK_THROWS_WITH_AS(v[ptr],
"[json.exception.out_of_range.404] unresolved reference token '10e'", json::out_of_range&); "[json.exception.out_of_range.404] unresolved reference token '10e'", json::out_of_range&);
} }
@ -311,8 +311,8 @@ TEST_CASE("JSON pointers")
{ {
auto too_large_index = std::to_string((std::numeric_limits<unsigned long long>::max)()) + "1"; auto too_large_index = std::to_string((std::numeric_limits<unsigned long long>::max)()) + "1";
json::json_pointer jp(std::string("/") + too_large_index); json::json_pointer const const jp(std::string("/") + too_large_index);
std::string throw_msg = std::string("[json.exception.out_of_range.404] unresolved reference token '") + too_large_index + "'"; std::string const throw_msg = std::string("[json.exception.out_of_range.404] unresolved reference token '") + too_large_index + "'";
CHECK_THROWS_WITH_AS(j[jp] = 1, throw_msg.c_str(), json::out_of_range&); CHECK_THROWS_WITH_AS(j[jp] = 1, throw_msg.c_str(), json::out_of_range&);
CHECK_THROWS_WITH_AS(j_const[jp] == 1, throw_msg.c_str(), json::out_of_range&); CHECK_THROWS_WITH_AS(j_const[jp] == 1, throw_msg.c_str(), json::out_of_range&);
@ -326,8 +326,8 @@ TEST_CASE("JSON pointers")
{ {
auto size_type_max_uul = static_cast<unsigned long long>((std::numeric_limits<json::size_type>::max)()); auto size_type_max_uul = static_cast<unsigned long long>((std::numeric_limits<json::size_type>::max)());
auto too_large_index = std::to_string(size_type_max_uul); auto too_large_index = std::to_string(size_type_max_uul);
json::json_pointer jp(std::string("/") + too_large_index); json::json_pointer const const jp(std::string("/") + too_large_index);
std::string throw_msg = std::string("[json.exception.out_of_range.410] array index ") + too_large_index + " exceeds size_type"; std::string const throw_msg = std::string("[json.exception.out_of_range.410] array index ") + too_large_index + " exceeds size_type";
CHECK_THROWS_WITH_AS(j[jp] = 1, throw_msg.c_str(), json::out_of_range&); CHECK_THROWS_WITH_AS(j[jp] = 1, throw_msg.c_str(), json::out_of_range&);
CHECK_THROWS_WITH_AS(j_const[jp] == 1, throw_msg.c_str(), json::out_of_range&); CHECK_THROWS_WITH_AS(j_const[jp] == 1, throw_msg.c_str(), json::out_of_range&);
@ -455,7 +455,7 @@ TEST_CASE("JSON pointers")
#endif #endif
// error for conflicting values // error for conflicting values
json j_error = {{"", 42}, {"/foo", 17}}; json const j_error = {{"", 42}, {"/foo", 17}};
CHECK_THROWS_WITH_AS(j_error.unflatten(), CHECK_THROWS_WITH_AS(j_error.unflatten(),
"[json.exception.type_error.313] invalid value to unflatten", json::type_error&); "[json.exception.type_error.313] invalid value to unflatten", json::type_error&);
@ -473,9 +473,9 @@ TEST_CASE("JSON pointers")
CHECK(j_string.flatten().unflatten() == j_string); CHECK(j_string.flatten().unflatten() == j_string);
// roundtrip for empty structured values (will be unflattened to null) // roundtrip for empty structured values (will be unflattened to null)
json j_array(json::value_t::array); json const j_array(json::value_t::array);
CHECK(j_array.flatten().unflatten() == json()); CHECK(j_array.flatten().unflatten() == json());
json j_object(json::value_t::object); json const j_object(json::value_t::object);
CHECK(j_object.flatten().unflatten() == json()); CHECK(j_object.flatten().unflatten() == json());
} }
@ -485,7 +485,7 @@ TEST_CASE("JSON pointers")
{"", "/foo", "/foo/0", "/", "/a~1b", "/c%d", "/e^f", "/g|h", "/i\\j", "/k\"l", "/ ", "/m~0n" {"", "/foo", "/foo/0", "/", "/a~1b", "/c%d", "/e^f", "/g|h", "/i\\j", "/k\"l", "/ ", "/m~0n"
}) })
{ {
json::json_pointer ptr(ptr_str); json::json_pointer const const ptr(ptr_str);
std::stringstream ss; std::stringstream ss;
ss << ptr; ss << ptr;
CHECK(ptr.to_string() == ptr_str); CHECK(ptr.to_string() == ptr_str);
@ -742,7 +742,7 @@ TEST_CASE("JSON pointers")
CHECK(std::is_same<json_ptr_str::string_t, json_ptr_j::string_t>::value); CHECK(std::is_same<json_ptr_str::string_t, json_ptr_j::string_t>::value);
CHECK(std::is_same<json_ptr_str::string_t, json_ptr_oj::string_t>::value); CHECK(std::is_same<json_ptr_str::string_t, json_ptr_oj::string_t>::value);
std::string ptr_string{"/foo/0"}; std::string const ptr_string{"/foo/0"};
json_ptr_str ptr{ptr_string}; json_ptr_str ptr{ptr_string};
json_ptr_j ptr_j{ptr_string}; json_ptr_j ptr_j{ptr_string};
json_ptr_oj ptr_oj{ptr_string}; json_ptr_oj ptr_oj{ptr_string};

View File

@ -28,7 +28,7 @@ TEST_CASE("JSON Merge Patch")
} }
})"_json; })"_json;
json patch = R"({ json const patch = R"({
"a": "z", "a": "z",
"c": { "c": {
"f": null "f": null
@ -61,7 +61,7 @@ TEST_CASE("JSON Merge Patch")
"content": "This will be unchanged" "content": "This will be unchanged"
})"_json; })"_json;
json patch = R"({ json const patch = R"({
"title": "Hello!", "title": "Hello!",
"phoneNumber": "+01-123-456-7890", "phoneNumber": "+01-123-456-7890",
"author": { "author": {
@ -93,7 +93,7 @@ TEST_CASE("JSON Merge Patch")
SECTION("Example 1") SECTION("Example 1")
{ {
json original = R"({"a":"b"})"_json; json original = R"({"a":"b"})"_json;
json patch = R"({"a":"c"})"_json; json const patch = R"({"a":"c"})"_json;
json result = R"({"a":"c"})"_json; json result = R"({"a":"c"})"_json;
original.merge_patch(patch); original.merge_patch(patch);
@ -103,7 +103,7 @@ TEST_CASE("JSON Merge Patch")
SECTION("Example 2") SECTION("Example 2")
{ {
json original = R"({"a":"b"})"_json; json original = R"({"a":"b"})"_json;
json patch = R"({"b":"c"})"_json; json const patch = R"({"b":"c"})"_json;
json result = R"({"a":"b", "b":"c"})"_json; json result = R"({"a":"b", "b":"c"})"_json;
original.merge_patch(patch); original.merge_patch(patch);
@ -113,7 +113,7 @@ TEST_CASE("JSON Merge Patch")
SECTION("Example 3") SECTION("Example 3")
{ {
json original = R"({"a":"b"})"_json; json original = R"({"a":"b"})"_json;
json patch = R"({"a":null})"_json; json const patch = R"({"a":null})"_json;
json result = R"({})"_json; json result = R"({})"_json;
original.merge_patch(patch); original.merge_patch(patch);
@ -123,7 +123,7 @@ TEST_CASE("JSON Merge Patch")
SECTION("Example 4") SECTION("Example 4")
{ {
json original = R"({"a":"b","b":"c"})"_json; json original = R"({"a":"b","b":"c"})"_json;
json patch = R"({"a":null})"_json; json const patch = R"({"a":null})"_json;
json result = R"({"b":"c"})"_json; json result = R"({"b":"c"})"_json;
original.merge_patch(patch); original.merge_patch(patch);
@ -133,7 +133,7 @@ TEST_CASE("JSON Merge Patch")
SECTION("Example 5") SECTION("Example 5")
{ {
json original = R"({"a":["b"]})"_json; json original = R"({"a":["b"]})"_json;
json patch = R"({"a":"c"})"_json; json const patch = R"({"a":"c"})"_json;
json result = R"({"a":"c"})"_json; json result = R"({"a":"c"})"_json;
original.merge_patch(patch); original.merge_patch(patch);
@ -143,7 +143,7 @@ TEST_CASE("JSON Merge Patch")
SECTION("Example 6") SECTION("Example 6")
{ {
json original = R"({"a":"c"})"_json; json original = R"({"a":"c"})"_json;
json patch = R"({"a":["b"]})"_json; json const patch = R"({"a":["b"]})"_json;
json result = R"({"a":["b"]})"_json; json result = R"({"a":["b"]})"_json;
original.merge_patch(patch); original.merge_patch(patch);
@ -153,7 +153,7 @@ TEST_CASE("JSON Merge Patch")
SECTION("Example 7") SECTION("Example 7")
{ {
json original = R"({"a":{"b": "c"}})"_json; json original = R"({"a":{"b": "c"}})"_json;
json patch = R"({"a":{"b":"d","c":null}})"_json; json const patch = R"({"a":{"b":"d","c":null}})"_json;
json result = R"({"a": {"b": "d"}})"_json; json result = R"({"a": {"b": "d"}})"_json;
original.merge_patch(patch); original.merge_patch(patch);
@ -163,7 +163,7 @@ TEST_CASE("JSON Merge Patch")
SECTION("Example 8") SECTION("Example 8")
{ {
json original = R"({"a":[{"b":"c"}]})"_json; json original = R"({"a":[{"b":"c"}]})"_json;
json patch = R"({"a":[1]})"_json; json const patch = R"({"a":[1]})"_json;
json result = R"({"a":[1]})"_json; json result = R"({"a":[1]})"_json;
original.merge_patch(patch); original.merge_patch(patch);
@ -173,7 +173,7 @@ TEST_CASE("JSON Merge Patch")
SECTION("Example 9") SECTION("Example 9")
{ {
json original = R"(["a","b"])"_json; json original = R"(["a","b"])"_json;
json patch = R"(["c","d"])"_json; json const patch = R"(["c","d"])"_json;
json result = R"(["c","d"])"_json; json result = R"(["c","d"])"_json;
original.merge_patch(patch); original.merge_patch(patch);
@ -183,7 +183,7 @@ TEST_CASE("JSON Merge Patch")
SECTION("Example 10") SECTION("Example 10")
{ {
json original = R"({"a":"b"})"_json; json original = R"({"a":"b"})"_json;
json patch = R"(["c"])"_json; json const patch = R"(["c"])"_json;
json result = R"(["c"])"_json; json result = R"(["c"])"_json;
original.merge_patch(patch); original.merge_patch(patch);
@ -193,7 +193,7 @@ TEST_CASE("JSON Merge Patch")
SECTION("Example 11") SECTION("Example 11")
{ {
json original = R"({"a":"foo"})"_json; json original = R"({"a":"foo"})"_json;
json patch = R"(null)"_json; json const patch = R"(null)"_json;
json result = R"(null)"_json; json result = R"(null)"_json;
original.merge_patch(patch); original.merge_patch(patch);
@ -203,7 +203,7 @@ TEST_CASE("JSON Merge Patch")
SECTION("Example 12") SECTION("Example 12")
{ {
json original = R"({"a":"foo"})"_json; json original = R"({"a":"foo"})"_json;
json patch = R"("bar")"_json; json const patch = R"("bar")"_json;
json result = R"("bar")"_json; json result = R"("bar")"_json;
original.merge_patch(patch); original.merge_patch(patch);
@ -213,7 +213,7 @@ TEST_CASE("JSON Merge Patch")
SECTION("Example 13") SECTION("Example 13")
{ {
json original = R"({"e":null})"_json; json original = R"({"e":null})"_json;
json patch = R"({"a":1})"_json; json const patch = R"({"a":1})"_json;
json result = R"({"e":null,"a":1})"_json; json result = R"({"e":null,"a":1})"_json;
original.merge_patch(patch); original.merge_patch(patch);
@ -223,7 +223,7 @@ TEST_CASE("JSON Merge Patch")
SECTION("Example 14") SECTION("Example 14")
{ {
json original = R"([1,2])"_json; json original = R"([1,2])"_json;
json patch = R"({"a":"b","c":null})"_json; json const patch = R"({"a":"b","c":null})"_json;
json result = R"({"a":"b"})"_json; json result = R"({"a":"b"})"_json;
original.merge_patch(patch); original.merge_patch(patch);
@ -233,7 +233,7 @@ TEST_CASE("JSON Merge Patch")
SECTION("Example 15") SECTION("Example 15")
{ {
json original = R"({})"_json; json original = R"({})"_json;
json patch = R"({"a":{"bb":{"ccc":null}}})"_json; json const patch = R"({"a":{"bb":{"ccc":null}}})"_json;
json result = R"({"a":{"bb":{}}})"_json; json result = R"({"a":{"bb":{}}})"_json;
original.merge_patch(patch); original.merge_patch(patch);

View File

@ -19,7 +19,7 @@ TEST_CASE("modifiers")
SECTION("boolean") SECTION("boolean")
{ {
json j = true; json j = true;
json k = j; json const k = j;
j.clear(); j.clear();
CHECK(j == json(json::value_t::boolean)); CHECK(j == json(json::value_t::boolean));
@ -29,7 +29,7 @@ TEST_CASE("modifiers")
SECTION("string") SECTION("string")
{ {
json j = "hello world"; json j = "hello world";
json k = j; json const k = j;
j.clear(); j.clear();
CHECK(j == json(json::value_t::string)); CHECK(j == json(json::value_t::string));
@ -41,7 +41,7 @@ TEST_CASE("modifiers")
SECTION("empty array") SECTION("empty array")
{ {
json j = json::array(); json j = json::array();
json k = j; json const k = j;
j.clear(); j.clear();
CHECK(j.empty()); CHECK(j.empty());
@ -52,7 +52,7 @@ TEST_CASE("modifiers")
SECTION("filled array") SECTION("filled array")
{ {
json j = {1, 2, 3}; json j = {1, 2, 3};
json k = j; json const k = j;
j.clear(); j.clear();
CHECK(j.empty()); CHECK(j.empty());
@ -66,7 +66,7 @@ TEST_CASE("modifiers")
SECTION("empty object") SECTION("empty object")
{ {
json j = json::object(); json j = json::object();
json k = j; json const k = j;
j.clear(); j.clear();
CHECK(j.empty()); CHECK(j.empty());
@ -77,7 +77,7 @@ TEST_CASE("modifiers")
SECTION("filled object") SECTION("filled object")
{ {
json j = {{"one", 1}, {"two", 2}, {"three", 3}}; json j = {{"one", 1}, {"two", 2}, {"three", 3}};
json k = j; json const k = j;
j.clear(); j.clear();
CHECK(j.empty()); CHECK(j.empty());
@ -91,7 +91,7 @@ TEST_CASE("modifiers")
SECTION("empty binary") SECTION("empty binary")
{ {
json j = json::binary({}); json j = json::binary({});
json k = j; json const k = j;
j.clear(); j.clear();
CHECK(!j.empty()); CHECK(!j.empty());
@ -102,7 +102,7 @@ TEST_CASE("modifiers")
SECTION("filled binary") SECTION("filled binary")
{ {
json j = json::binary({1, 2, 3, 4, 5}); json j = json::binary({1, 2, 3, 4, 5});
json k = j; json const k = j;
j.clear(); j.clear();
CHECK(!j.empty()); CHECK(!j.empty());
@ -114,7 +114,7 @@ TEST_CASE("modifiers")
SECTION("number (integer)") SECTION("number (integer)")
{ {
json j = 23; json j = 23;
json k = j; json const k = j;
j.clear(); j.clear();
CHECK(j == json(json::value_t::number_integer)); CHECK(j == json(json::value_t::number_integer));
@ -124,7 +124,7 @@ TEST_CASE("modifiers")
SECTION("number (unsigned)") SECTION("number (unsigned)")
{ {
json j = 23u; json j = 23u;
json k = j; json const k = j;
j.clear(); j.clear();
CHECK(j == json(json::value_t::number_integer)); CHECK(j == json(json::value_t::number_integer));
@ -134,7 +134,7 @@ TEST_CASE("modifiers")
SECTION("number (float)") SECTION("number (float)")
{ {
json j = 23.42; json j = 23.42;
json k = j; json const k = j;
j.clear(); j.clear();
CHECK(j == json(json::value_t::number_float)); CHECK(j == json(json::value_t::number_float));
@ -144,7 +144,7 @@ TEST_CASE("modifiers")
SECTION("null") SECTION("null")
{ {
json j = nullptr; json j = nullptr;
json k = j; json const k = j;
j.clear(); j.clear();
CHECK(j == json(json::value_t::null)); CHECK(j == json(json::value_t::null));
@ -187,7 +187,7 @@ TEST_CASE("modifiers")
SECTION("null") SECTION("null")
{ {
json j; json j;
json k(1); json const k(1);
j.push_back(k); j.push_back(k);
j.push_back(k); j.push_back(k);
CHECK(j.type() == json::value_t::array); CHECK(j.type() == json::value_t::array);
@ -197,7 +197,7 @@ TEST_CASE("modifiers")
SECTION("array") SECTION("array")
{ {
json j = {1, 2, 3}; json j = {1, 2, 3};
json k("Hello"); json const k("Hello");
j.push_back(k); j.push_back(k);
CHECK(j.type() == json::value_t::array); CHECK(j.type() == json::value_t::array);
CHECK(j == json({1, 2, 3, "Hello"})); CHECK(j == json({1, 2, 3, "Hello"}));
@ -206,7 +206,7 @@ TEST_CASE("modifiers")
SECTION("other type") SECTION("other type")
{ {
json j = 1; json j = 1;
json k("Hello"); json const k("Hello");
CHECK_THROWS_WITH_AS(j.push_back(k), "[json.exception.type_error.308] cannot use push_back() with number", json::type_error&); CHECK_THROWS_WITH_AS(j.push_back(k), "[json.exception.type_error.308] cannot use push_back() with number", json::type_error&);
} }
} }
@ -238,7 +238,7 @@ TEST_CASE("modifiers")
SECTION("other type") SECTION("other type")
{ {
json j = 1; json j = 1;
json k("Hello"); json const k("Hello");
CHECK_THROWS_WITH_AS(j.push_back(json::object_t::value_type({"one", 1})), "[json.exception.type_error.308] cannot use push_back() with number", json::type_error&); CHECK_THROWS_WITH_AS(j.push_back(json::object_t::value_type({"one", 1})), "[json.exception.type_error.308] cannot use push_back() with number", json::type_error&);
} }
} }
@ -415,7 +415,7 @@ TEST_CASE("modifiers")
SECTION("null") SECTION("null")
{ {
json j; json j;
json k(1); json const k(1);
j += k; j += k;
j += k; j += k;
CHECK(j.type() == json::value_t::array); CHECK(j.type() == json::value_t::array);
@ -425,7 +425,7 @@ TEST_CASE("modifiers")
SECTION("array") SECTION("array")
{ {
json j = {1, 2, 3}; json j = {1, 2, 3};
json k("Hello"); json const k("Hello");
j += k; j += k;
CHECK(j.type() == json::value_t::array); CHECK(j.type() == json::value_t::array);
CHECK(j == json({1, 2, 3, "Hello"})); CHECK(j == json({1, 2, 3, "Hello"}));
@ -434,7 +434,7 @@ TEST_CASE("modifiers")
SECTION("other type") SECTION("other type")
{ {
json j = 1; json j = 1;
json k("Hello"); json const k("Hello");
CHECK_THROWS_WITH_AS(j += k, "[json.exception.type_error.308] cannot use push_back() with number", json::type_error&); CHECK_THROWS_WITH_AS(j += k, "[json.exception.type_error.308] cannot use push_back() with number", json::type_error&);
} }
} }
@ -466,7 +466,7 @@ TEST_CASE("modifiers")
SECTION("other type") SECTION("other type")
{ {
json j = 1; json j = 1;
json k("Hello"); json const k("Hello");
CHECK_THROWS_WITH_AS(j += json::object_t::value_type({"one", 1}), "[json.exception.type_error.308] cannot use push_back() with number", json::type_error&); CHECK_THROWS_WITH_AS(j += json::object_t::value_type({"one", 1}), "[json.exception.type_error.308] cannot use push_back() with number", json::type_error&);
} }
} }
@ -663,7 +663,7 @@ TEST_CASE("modifiers")
SECTION("invalid iterators") SECTION("invalid iterators")
{ {
json j_other_array2 = {"first", "second"}; json const j_other_array2 = {"first", "second"};
CHECK_THROWS_WITH_AS(j_array.insert(j_object2.begin(), j_object2.end()), "[json.exception.type_error.309] cannot use insert() with array", json::type_error&); CHECK_THROWS_WITH_AS(j_array.insert(j_object2.begin(), j_object2.end()), "[json.exception.type_error.309] cannot use insert() with array", json::type_error&);
CHECK_THROWS_WITH_AS(j_object1.insert(j_object1.begin(), j_object2.end()), "[json.exception.invalid_iterator.210] iterators do not fit", json::invalid_iterator&); CHECK_THROWS_WITH_AS(j_object1.insert(j_object1.begin(), j_object2.end()), "[json.exception.invalid_iterator.210] iterators do not fit", json::invalid_iterator&);
@ -774,7 +774,7 @@ TEST_CASE("modifiers")
SECTION("invalid iterators") SECTION("invalid iterators")
{ {
json j_other_array2 = {"first", "second"}; json const j_other_array2 = {"first", "second"};
CHECK_THROWS_WITH_AS(j_array.update(j_object2.begin(), j_object2.end()), "[json.exception.type_error.312] cannot use update() with array", json::type_error&); CHECK_THROWS_WITH_AS(j_array.update(j_object2.begin(), j_object2.end()), "[json.exception.type_error.312] cannot use update() with array", json::type_error&);
CHECK_THROWS_WITH_AS(j_object1.update(j_object1.begin(), j_object2.end()), "[json.exception.invalid_iterator.210] iterators do not fit", json::invalid_iterator&); CHECK_THROWS_WITH_AS(j_object1.update(j_object1.begin(), j_object2.end()), "[json.exception.invalid_iterator.210] iterators do not fit", json::invalid_iterator&);
@ -790,7 +790,7 @@ TEST_CASE("modifiers")
SECTION("extend object") SECTION("extend object")
{ {
json j1 = {{"string", "s"}, {"numbers", {{"one", 1}}}}; json j1 = {{"string", "s"}, {"numbers", {{"one", 1}}}};
json j2 = {{"string", "t"}, {"numbers", {{"two", 2}}}}; json const j2 = {{"string", "t"}, {"numbers", {{"two", 2}}}};
j1.update(j2, true); j1.update(j2, true);
CHECK(j1 == json({{"string", "t"}, {"numbers", {{"one", 1}, {"two", 2}}}})); CHECK(j1 == json({{"string", "t"}, {"numbers", {{"one", 1}, {"two", 2}}}}));
} }
@ -798,7 +798,7 @@ TEST_CASE("modifiers")
SECTION("replace object") SECTION("replace object")
{ {
json j1 = {{"string", "s"}, {"numbers", {{"one", 1}}}}; json j1 = {{"string", "s"}, {"numbers", {{"one", 1}}}};
json j2 = {{"string", "t"}, {"numbers", 1}}; json const j2 = {{"string", "t"}, {"numbers", 1}};
j1.update(j2, true); j1.update(j2, true);
CHECK(j1 == json({{"string", "t"}, {"numbers", 1}})); CHECK(j1 == json({{"string", "t"}, {"numbers", 1}}));
} }

View File

@ -106,7 +106,7 @@ TEST_CASE("MessagePack")
SECTION("discarded") SECTION("discarded")
{ {
// discarded values are not serialized // discarded values are not serialized
json j = json::value_t::discarded; json const j = json::value_t::discarded;
const auto result = json::to_msgpack(j); const auto result = json::to_msgpack(j);
CHECK(result.empty()); CHECK(result.empty());
} }
@ -1150,7 +1150,7 @@ TEST_CASE("MessagePack")
// create JSON value with byte array containing of N * 'x' // create JSON value with byte array containing of N * 'x'
const auto s = std::vector<uint8_t>(N, 'x'); const auto s = std::vector<uint8_t>(N, 'x');
json j = json::binary(s); json j = json::binary(s);
std::uint8_t subtype = 42; std::uint8_t const subtype = 42;
j.get_binary().set_subtype(subtype); j.get_binary().set_subtype(subtype);
// create expected byte vector // create expected byte vector
@ -1225,7 +1225,7 @@ TEST_CASE("MessagePack")
// create JSON value with string containing of N * 'x' // create JSON value with string containing of N * 'x'
const auto s = std::vector<uint8_t>(N, 'x'); const auto s = std::vector<uint8_t>(N, 'x');
json j = json::binary(s); json j = json::binary(s);
std::uint8_t subtype = 42; std::uint8_t const subtype = 42;
j.get_binary().set_subtype(subtype); j.get_binary().set_subtype(subtype);
// create expected byte vector (hack: create string first) // create expected byte vector (hack: create string first)
@ -1261,7 +1261,7 @@ TEST_CASE("MessagePack")
// create JSON value with string containing of N * 'x' // create JSON value with string containing of N * 'x'
const auto s = std::vector<uint8_t>(N, 'x'); const auto s = std::vector<uint8_t>(N, 'x');
json j = json::binary(s); json j = json::binary(s);
std::uint8_t subtype = 42; std::uint8_t const subtype = 42;
j.get_binary().set_subtype(subtype); j.get_binary().set_subtype(subtype);
// create expected byte vector (hack: create string first) // create expected byte vector (hack: create string first)
@ -1398,7 +1398,7 @@ TEST_CASE("MessagePack")
SECTION("from float32") SECTION("from float32")
{ {
auto given = std::vector<uint8_t>({0xca, 0x41, 0xc8, 0x00, 0x01}); auto given = std::vector<uint8_t>({0xca, 0x41, 0xc8, 0x00, 0x01});
json j = json::from_msgpack(given); json const j = json::from_msgpack(given);
CHECK(j.get<double>() == Approx(25.0000019073486)); CHECK(j.get<double>() == Approx(25.0000019073486));
} }
@ -1511,7 +1511,7 @@ TEST_CASE("MessagePack")
SECTION("strict mode") SECTION("strict mode")
{ {
std::vector<uint8_t> vec = {0xc0, 0xc0}; std::vector<uint8_t> const vec = {0xc0, 0xc0};
SECTION("non-strict mode") SECTION("non-strict mode")
{ {
const auto result = json::from_msgpack(vec, false); const auto result = json::from_msgpack(vec, false);
@ -1531,21 +1531,21 @@ TEST_CASE("MessagePack")
{ {
SECTION("start_array(len)") SECTION("start_array(len)")
{ {
std::vector<uint8_t> v = {0x93, 0x01, 0x02, 0x03}; std::vector<uint8_t> const v = {0x93, 0x01, 0x02, 0x03};
SaxCountdown scp(0); SaxCountdown scp(0);
CHECK(!json::sax_parse(v, &scp, json::input_format_t::msgpack)); CHECK(!json::sax_parse(v, &scp, json::input_format_t::msgpack));
} }
SECTION("start_object(len)") SECTION("start_object(len)")
{ {
std::vector<uint8_t> v = {0x81, 0xa3, 0x66, 0x6F, 0x6F, 0xc2}; std::vector<uint8_t> const v = {0x81, 0xa3, 0x66, 0x6F, 0x6F, 0xc2};
SaxCountdown scp(0); SaxCountdown scp(0);
CHECK(!json::sax_parse(v, &scp, json::input_format_t::msgpack)); CHECK(!json::sax_parse(v, &scp, json::input_format_t::msgpack));
} }
SECTION("key()") SECTION("key()")
{ {
std::vector<uint8_t> v = {0x81, 0xa3, 0x66, 0x6F, 0x6F, 0xc2}; std::vector<uint8_t> const v = {0x81, 0xa3, 0x66, 0x6F, 0x6F, 0xc2};
SaxCountdown scp(1); SaxCountdown scp(1);
CHECK(!json::sax_parse(v, &scp, json::input_format_t::msgpack)); CHECK(!json::sax_parse(v, &scp, json::input_format_t::msgpack));
} }
@ -1557,7 +1557,7 @@ TEST_CASE("single MessagePack roundtrip")
{ {
SECTION("sample.json") SECTION("sample.json")
{ {
std::string filename = TEST_DATA_DIRECTORY "/json_testsuite/sample.json"; std::string const filename = TEST_DATA_DIRECTORY "/json_testsuite/sample.json";
// parse JSON file // parse JSON file
std::ifstream f_json(filename); std::ifstream f_json(filename);
@ -1817,7 +1817,7 @@ TEST_CASE("MessagePack roundtrips" * doctest::skip())
INFO_WITH_TEMP(filename + ": output to output adapters"); INFO_WITH_TEMP(filename + ": output to output adapters");
// parse JSON file // parse JSON file
std::ifstream f_json(filename); std::ifstream f_json(filename);
json j1 = json::parse(f_json); json const j1 = json::parse(f_json);
// parse MessagePack file // parse MessagePack file
auto packed = utils::read_binary_file(filename + ".msgpack"); auto packed = utils::read_binary_file(filename + ".msgpack");

View File

@ -46,7 +46,7 @@ TEST_CASE("ordered_json")
CHECK(oj.dump() == "{\"element3\":3,\"element2\":2}"); CHECK(oj.dump() == "{\"element3\":3,\"element2\":2}");
// There are no dup keys cause constructor calls emplace... // There are no dup keys cause constructor calls emplace...
json multi {{"z", 1}, {"m", 2}, {"m", 3}, {"y", 4}, {"m", 5}}; json const multi {{"z", 1}, {"m", 2}, {"m", 3}, {"y", 4}, {"m", 5}};
CHECK(multi.size() == 3); CHECK(multi.size() == 3);
CHECK(multi.dump() == "{\"m\":2,\"y\":4,\"z\":1}"); CHECK(multi.dump() == "{\"m\":2,\"y\":4,\"z\":1}");

View File

@ -19,7 +19,7 @@ TEST_CASE("ordered_map")
SECTION("constructor from iterator range") SECTION("constructor from iterator range")
{ {
std::map<std::string, std::string> m {{"eins", "one"}, {"zwei", "two"}, {"drei", "three"}}; std::map<std::string, std::string> m {{"eins", "one"}, {"zwei", "two"}, {"drei", "three"}};
ordered_map<std::string, std::string> om(m.begin(), m.end()); ordered_map<std::string, std::string> const om(m.begin(), m.end());
CHECK(om.size() == 3); CHECK(om.size() == 3);
} }
@ -281,8 +281,8 @@ TEST_CASE("ordered_map")
SECTION("const value_type&") SECTION("const value_type&")
{ {
ordered_map<std::string, std::string>::value_type vt1 {"eins", "1"}; ordered_map<std::string, std::string>::value_type const vt1 {"eins", "1"};
ordered_map<std::string, std::string>::value_type vt4 {"vier", "four"}; ordered_map<std::string, std::string>::value_type const vt4 {"vier", "four"};
auto res1 = om.insert(vt1); auto res1 = om.insert(vt1);
CHECK(res1.first == om.begin()); CHECK(res1.first == om.begin());

View File

@ -33,7 +33,7 @@ TEST_CASE("README" * doctest::skip())
{ {
// redirect std::cout for the README file // redirect std::cout for the README file
auto* old_cout_buffer = std::cout.rdbuf(); auto* old_cout_buffer = std::cout.rdbuf();
std::ostringstream new_stream; std::ostringstream const new_stream;
std::cout.rdbuf(new_stream.rdbuf()); std::cout.rdbuf(new_stream.rdbuf());
{ {
// create an empty structure (null) // create an empty structure (null)
@ -61,7 +61,7 @@ TEST_CASE("README" * doctest::skip())
j["object"] = { {"currency", "USD"}, {"value", 42.99} }; j["object"] = { {"currency", "USD"}, {"value", 42.99} };
// instead, you could also write (which looks very similar to the JSON above) // instead, you could also write (which looks very similar to the JSON above)
json j2 = json const j2 =
{ {
{"pi", 3.141}, {"pi", 3.141},
{"happy", true}, {"happy", true},
@ -84,13 +84,13 @@ TEST_CASE("README" * doctest::skip())
{ {
// ways to express the empty array [] // ways to express the empty array []
json empty_array_implicit = {{}}; json const empty_array_implicit = {{}};
CHECK(empty_array_implicit.is_array()); CHECK(empty_array_implicit.is_array());
json empty_array_explicit = json::array(); json const empty_array_explicit = json::array();
CHECK(empty_array_explicit.is_array()); CHECK(empty_array_explicit.is_array());
// a way to express the empty object {} // a way to express the empty object {}
json empty_object_explicit = json::object(); json const empty_object_explicit = json::object();
CHECK(empty_object_explicit.is_object()); CHECK(empty_object_explicit.is_object());
// a way to express an _array_ of key/value pairs [["currency", "USD"], ["value", 42.99]] // a way to express an _array_ of key/value pairs [["currency", "USD"], ["value", 42.99]]
@ -103,7 +103,7 @@ TEST_CASE("README" * doctest::skip())
{ {
// create object from string literal // create object from string literal
json j = "{ \"happy\": true, \"pi\": 3.141 }"_json; // NOLINT(modernize-raw-string-literal) json const j = "{ \"happy\": true, \"pi\": 3.141 }"_json; // NOLINT(modernize-raw-string-literal)
// or even nicer with a raw string literal // or even nicer with a raw string literal
auto j2 = R"({ auto j2 = R"({
@ -115,7 +115,7 @@ TEST_CASE("README" * doctest::skip())
auto j3 = json::parse(R"({"happy": true, "pi": 3.141})"); auto j3 = json::parse(R"({"happy": true, "pi": 3.141})");
// explicit conversion to string // explicit conversion to string
std::string s = j.dump(); // {\"happy\":true,\"pi\":3.141} std::string const s = j.dump(); // {\"happy\":true,\"pi\":3.141}
// serialization with pretty printing // serialization with pretty printing
// pass in the amount of spaces to indent // pass in the amount of spaces to indent
@ -178,82 +178,82 @@ TEST_CASE("README" * doctest::skip())
} }
{ {
std::vector<int> c_vector {1, 2, 3, 4}; std::vector<int> const c_vector {1, 2, 3, 4};
json j_vec(c_vector); json const j_vec(c_vector);
// [1, 2, 3, 4] // [1, 2, 3, 4]
std::deque<float> c_deque {1.2f, 2.3f, 3.4f, 5.6f}; std::deque<float> const c_deque {1.2f, 2.3f, 3.4f, 5.6f};
json j_deque(c_deque); json const j_deque(c_deque);
// [1.2, 2.3, 3.4, 5.6] // [1.2, 2.3, 3.4, 5.6]
std::list<bool> c_list {true, true, false, true}; std::list<bool> const c_list {true, true, false, true};
json j_list(c_list); json const j_list(c_list);
// [true, true, false, true] // [true, true, false, true]
std::forward_list<int64_t> c_flist {12345678909876, 23456789098765, 34567890987654, 45678909876543}; std::forward_list<int64_t> const c_flist {12345678909876, 23456789098765, 34567890987654, 45678909876543};
json j_flist(c_flist); json const j_flist(c_flist);
// [12345678909876, 23456789098765, 34567890987654, 45678909876543] // [12345678909876, 23456789098765, 34567890987654, 45678909876543]
std::array<unsigned long, 4> c_array {{1, 2, 3, 4}}; std::array<unsigned long, 4> const c_array {{1, 2, 3, 4}};
json j_array(c_array); json const j_array(c_array);
// [1, 2, 3, 4] // [1, 2, 3, 4]
std::set<std::string> c_set {"one", "two", "three", "four", "one"}; std::set<std::string> const c_set {"one", "two", "three", "four", "one"};
json j_set(c_set); // only one entry for "one" is used json const j_set(c_set); // only one entry for "one" is used
// ["four", "one", "three", "two"] // ["four", "one", "three", "two"]
std::unordered_set<std::string> c_uset {"one", "two", "three", "four", "one"}; std::unordered_set<std::string> const c_uset {"one", "two", "three", "four", "one"};
json j_uset(c_uset); // only one entry for "one" is used json const j_uset(c_uset); // only one entry for "one" is used
// maybe ["two", "three", "four", "one"] // maybe ["two", "three", "four", "one"]
std::multiset<std::string> c_mset {"one", "two", "one", "four"}; std::multiset<std::string> const c_mset {"one", "two", "one", "four"};
json j_mset(c_mset); // both entries for "one" are used json const j_mset(c_mset); // both entries for "one" are used
// maybe ["one", "two", "one", "four"] // maybe ["one", "two", "one", "four"]
std::unordered_multiset<std::string> c_umset {"one", "two", "one", "four"}; std::unordered_multiset<std::string> const c_umset {"one", "two", "one", "four"};
json j_umset(c_umset); // both entries for "one" are used json const j_umset(c_umset); // both entries for "one" are used
// maybe ["one", "two", "one", "four"] // maybe ["one", "two", "one", "four"]
} }
{ {
std::map<std::string, int> c_map { {"one", 1}, {"two", 2}, {"three", 3} }; std::map<std::string, int> const c_map { {"one", 1}, {"two", 2}, {"three", 3} };
json j_map(c_map); json const j_map(c_map);
// {"one": 1, "two": 2, "three": 3} // {"one": 1, "two": 2, "three": 3}
std::unordered_map<const char*, float> c_umap { {"one", 1.2f}, {"two", 2.3f}, {"three", 3.4f} }; std::unordered_map<const char*, float> const c_umap { {"one", 1.2f}, {"two", 2.3f}, {"three", 3.4f} };
json j_umap(c_umap); json const j_umap(c_umap);
// {"one": 1.2, "two": 2.3, "three": 3.4} // {"one": 1.2, "two": 2.3, "three": 3.4}
std::multimap<std::string, bool> c_mmap { {"one", true}, {"two", true}, {"three", false}, {"three", true} }; std::multimap<std::string, bool> const c_mmap { {"one", true}, {"two", true}, {"three", false}, {"three", true} };
json j_mmap(c_mmap); // only one entry for key "three" is used json const j_mmap(c_mmap); // only one entry for key "three" is used
// maybe {"one": true, "two": true, "three": true} // maybe {"one": true, "two": true, "three": true}
std::unordered_multimap<std::string, bool> c_ummap { {"one", true}, {"two", true}, {"three", false}, {"three", true} }; std::unordered_multimap<std::string, bool> const c_ummap { {"one", true}, {"two", true}, {"three", false}, {"three", true} };
json j_ummap(c_ummap); // only one entry for key "three" is used json const j_ummap(c_ummap); // only one entry for key "three" is used
// maybe {"one": true, "two": true, "three": true} // maybe {"one": true, "two": true, "three": true}
} }
{ {
// strings // strings
std::string s1 = "Hello, world!"; std::string const s1 = "Hello, world!";
json js = s1; json const js = s1;
auto s2 = js.get<std::string>(); auto s2 = js.get<std::string>();
// Booleans // Booleans
bool b1 = true; bool const b1 = true;
json jb = b1; json const jb = b1;
bool b2{jb}; bool b2{jb};
CHECK(b2 == true); CHECK(b2 == true);
// numbers // numbers
int i = 42; int const i = 42;
json jn = i; json const jn = i;
double f{jn}; double f{jn};
CHECK(f == 42); CHECK(f == 42);
// etc. // etc.
std::string vs = js.get<std::string>(); std::string const vs = js.get<std::string>();
bool vb = jb.get<bool>(); bool vb = jb.get<bool>();
CHECK(vb == true); CHECK(vb == true);
int vi = jn.get<int>(); int vi = jn.get<int>();
@ -274,14 +274,14 @@ TEST_CASE("README" * doctest::skip())
// "two" // "two"
// a JSON patch (RFC 6902) // a JSON patch (RFC 6902)
json j_patch = R"([ json const j_patch = R"([
{ "op": "replace", "path": "/baz", "value": "boo" }, { "op": "replace", "path": "/baz", "value": "boo" },
{ "op": "add", "path": "/hello", "value": ["world"] }, { "op": "add", "path": "/hello", "value": ["world"] },
{ "op": "remove", "path": "/foo"} { "op": "remove", "path": "/foo"}
])"_json; ])"_json;
// apply the patch // apply the patch
json j_result = j_original.patch(j_patch); json const j_result = j_original.patch(j_patch);
// { // {
// "baz": "boo", // "baz": "boo",
// "hello": ["world"] // "hello": ["world"]

View File

@ -418,15 +418,15 @@ TEST_CASE("adl_serializer specialization" * doctest::test_suite("udt"))
{ {
SECTION("to_json") SECTION("to_json")
{ {
udt::legacy_type lt{"4242"}; udt::legacy_type const lt{"4242"};
json j = lt; json const j = lt;
CHECK(j.get<int>() == 4242); CHECK(j.get<int>() == 4242);
} }
SECTION("from_json") SECTION("from_json")
{ {
json j = 4242; json const j = 4242;
auto lt = j.get<udt::legacy_type>(); auto lt = j.get<udt::legacy_type>();
CHECK(lt.number == "4242"); CHECK(lt.number == "4242");
} }
@ -459,7 +459,7 @@ struct adl_serializer<std::vector<float>>
TEST_CASE("even supported types can be specialized" * doctest::test_suite("udt")) TEST_CASE("even supported types can be specialized" * doctest::test_suite("udt"))
{ {
json j = std::vector<float> {1.0, 2.0, 3.0}; json const j = std::vector<float> {1.0, 2.0, 3.0};
CHECK(j.dump() == R"("hijacked!")"); CHECK(j.dump() == R"("hijacked!")");
auto f = j.get<std::vector<float>>(); auto f = j.get<std::vector<float>>();
// the single argument from_json method is preferred // the single argument from_json method is preferred
@ -644,14 +644,14 @@ TEST_CASE("custom serializer for pods" * doctest::test_suite("udt"))
std::int64_t, std::uint64_t, double, std::allocator, pod_serializer>; std::int64_t, std::uint64_t, double, std::allocator, pod_serializer>;
auto p = udt::small_pod{42, '/', 42}; auto p = udt::small_pod{42, '/', 42};
custom_json j = p; custom_json const j = p;
auto p2 = j.get<udt::small_pod>(); auto p2 = j.get<udt::small_pod>();
CHECK(p == p2); CHECK(p == p2);
auto np = udt::non_pod{{"non-pod"}}; auto np = udt::non_pod{{"non-pod"}};
custom_json j2 = np; custom_json const j2 = np;
auto np2 = j2.get<udt::non_pod>(); auto np2 = j2.get<udt::non_pod>();
CHECK(np == np2); CHECK(np == np2);
} }
@ -681,8 +681,8 @@ TEST_CASE("custom serializer that does adl by default" * doctest::test_suite("ud
{ {
auto me = udt::person{{23}, {"theo"}, udt::country::france}; auto me = udt::person{{23}, {"theo"}, udt::country::france};
json j = me; json const j = me;
custom_json cj = me; custom_json const cj = me;
CHECK(j.dump() == cj.dump()); CHECK(j.dump() == cj.dump());
@ -694,21 +694,21 @@ TEST_CASE("different basic_json types conversions")
{ {
SECTION("null") SECTION("null")
{ {
json j; json const j;
custom_json cj = j; custom_json cj = j;
CHECK(cj == nullptr); CHECK(cj == nullptr);
} }
SECTION("boolean") SECTION("boolean")
{ {
json j = true; json const j = true;
custom_json cj = j; custom_json cj = j;
CHECK(cj == true); CHECK(cj == true);
} }
SECTION("discarded") SECTION("discarded")
{ {
json j(json::value_t::discarded); json const j(json::value_t::discarded);
custom_json cj; custom_json cj;
CHECK_NOTHROW(cj = j); CHECK_NOTHROW(cj = j);
CHECK(cj.type() == custom_json::value_t::discarded); CHECK(cj.type() == custom_json::value_t::discarded);
@ -716,35 +716,35 @@ TEST_CASE("different basic_json types conversions")
SECTION("array") SECTION("array")
{ {
json j = {1, 2, 3}; json const j = {1, 2, 3};
custom_json cj = j; custom_json const cj = j;
CHECK((cj == std::vector<int> {1, 2, 3})); CHECK((cj == std::vector<int> {1, 2, 3}));
} }
SECTION("integer") SECTION("integer")
{ {
json j = 42; json const j = 42;
custom_json cj = j; custom_json cj = j;
CHECK(cj == 42); CHECK(cj == 42);
} }
SECTION("float") SECTION("float")
{ {
json j = 42.0; json const j = 42.0;
custom_json cj = j; custom_json cj = j;
CHECK(cj == 42.0); CHECK(cj == 42.0);
} }
SECTION("unsigned") SECTION("unsigned")
{ {
json j = 42u; json const j = 42u;
custom_json cj = j; custom_json cj = j;
CHECK(cj == 42u); CHECK(cj == 42u);
} }
SECTION("string") SECTION("string")
{ {
json j = "forty-two"; json const j = "forty-two";
custom_json cj = j; custom_json cj = j;
CHECK(cj == "forty-two"); CHECK(cj == "forty-two");
} }
@ -761,7 +761,7 @@ TEST_CASE("different basic_json types conversions")
SECTION("object") SECTION("object")
{ {
json j = {{"forty", "two"}}; json const j = {{"forty", "two"}};
custom_json cj = j; custom_json cj = j;
auto m = j.get<std::map<std::string, std::string>>(); auto m = j.get<std::map<std::string, std::string>>();
CHECK(cj == m); CHECK(cj == m);
@ -769,7 +769,7 @@ TEST_CASE("different basic_json types conversions")
SECTION("get<custom_json>") SECTION("get<custom_json>")
{ {
json j = 42; json const j = 42;
custom_json cj = j.get<custom_json>(); custom_json cj = j.get<custom_json>();
CHECK(cj == 42); CHECK(cj == 42);
} }
@ -857,8 +857,8 @@ class no_iterator_type
TEST_CASE("compatible array type, without iterator type alias") TEST_CASE("compatible array type, without iterator type alias")
{ {
no_iterator_type vec{1, 2, 3}; no_iterator_type const vec{1, 2, 3};
json j = vec; json const j = vec;
} }
DOCTEST_GCC_SUPPRESS_WARNING_POP DOCTEST_GCC_SUPPRESS_WARNING_POP

View File

@ -349,7 +349,7 @@ TEST_CASE_TEMPLATE("Serialization/deserialization of classes with 26 public/priv
{ {
{ {
T obj1; T obj1;
nlohmann::json j = obj1; //via json object nlohmann::json const j = obj1; //via json object
T obj2; T obj2;
j.get_to(obj2); j.get_to(obj2);
bool ok = (obj1 == obj2); bool ok = (obj1 == obj2);
@ -358,9 +358,9 @@ TEST_CASE_TEMPLATE("Serialization/deserialization of classes with 26 public/priv
{ {
T obj1; T obj1;
nlohmann::json j1 = obj1; //via json string nlohmann::json const j1 = obj1; //via json string
std::string s = j1.dump(); std::string const s = j1.dump();
nlohmann::json j2 = nlohmann::json::parse(s); nlohmann::json const j2 = nlohmann::json::parse(s);
T obj2; T obj2;
j2.get_to(obj2); j2.get_to(obj2);
bool ok = (obj1 == obj2); bool ok = (obj1 == obj2);
@ -369,9 +369,9 @@ TEST_CASE_TEMPLATE("Serialization/deserialization of classes with 26 public/priv
{ {
T obj1; T obj1;
nlohmann::json j1 = obj1; //via msgpack nlohmann::json const j1 = obj1; //via msgpack
std::vector<uint8_t> buf = nlohmann::json::to_msgpack(j1); std::vector<uint8_t> const buf = nlohmann::json::to_msgpack(j1);
nlohmann::json j2 = nlohmann::json::from_msgpack(buf); nlohmann::json const j2 = nlohmann::json::from_msgpack(buf);
T obj2; T obj2;
j2.get_to(obj2); j2.get_to(obj2);
bool ok = (obj1 == obj2); bool ok = (obj1 == obj2);
@ -380,9 +380,9 @@ TEST_CASE_TEMPLATE("Serialization/deserialization of classes with 26 public/priv
{ {
T obj1; T obj1;
nlohmann::json j1 = obj1; //via bson nlohmann::json const j1 = obj1; //via bson
std::vector<uint8_t> buf = nlohmann::json::to_bson(j1); std::vector<uint8_t> const buf = nlohmann::json::to_bson(j1);
nlohmann::json j2 = nlohmann::json::from_bson(buf); nlohmann::json const j2 = nlohmann::json::from_bson(buf);
T obj2; T obj2;
j2.get_to(obj2); j2.get_to(obj2);
bool ok = (obj1 == obj2); bool ok = (obj1 == obj2);
@ -391,9 +391,9 @@ TEST_CASE_TEMPLATE("Serialization/deserialization of classes with 26 public/priv
{ {
T obj1; T obj1;
nlohmann::json j1 = obj1; //via cbor nlohmann::json const j1 = obj1; //via cbor
std::vector<uint8_t> buf = nlohmann::json::to_cbor(j1); std::vector<uint8_t> const buf = nlohmann::json::to_cbor(j1);
nlohmann::json j2 = nlohmann::json::from_cbor(buf); nlohmann::json const j2 = nlohmann::json::from_cbor(buf);
T obj2; T obj2;
j2.get_to(obj2); j2.get_to(obj2);
bool ok = (obj1 == obj2); bool ok = (obj1 == obj2);
@ -402,9 +402,9 @@ TEST_CASE_TEMPLATE("Serialization/deserialization of classes with 26 public/priv
{ {
T obj1; T obj1;
nlohmann::json j1 = obj1; //via ubjson nlohmann::json const j1 = obj1; //via ubjson
std::vector<uint8_t> buf = nlohmann::json::to_ubjson(j1); std::vector<uint8_t> const buf = nlohmann::json::to_ubjson(j1);
nlohmann::json j2 = nlohmann::json::from_ubjson(buf); nlohmann::json const j2 = nlohmann::json::from_ubjson(buf);
T obj2; T obj2;
j2.get_to(obj2); j2.get_to(obj2);
bool ok = (obj1 == obj2); bool ok = (obj1 == obj2);