mirror of
https://github.com/nlohmann/json.git
synced 2026-02-10 12:02:28 +00:00
💥 change serialization of binary values
This commit is contained in:
@@ -209,52 +209,114 @@ TEST_CASE_TEMPLATE("serialization for extreme integer values", T, int32_t, uint3
|
||||
|
||||
TEST_CASE("dump with binary values")
|
||||
{
|
||||
SECTION("serialize_binary = false")
|
||||
{
|
||||
auto binary = json::binary_array({1, 2, 3, 4});
|
||||
auto binary_empty = json::binary_array({});
|
||||
json object = {{"key", binary}};
|
||||
json array = {"value", 1, binary};
|
||||
auto binary = json::binary_array({1, 2, 3, 4});
|
||||
auto binary_empty = json::binary_array({});
|
||||
auto binary_with_subtype = json::binary_array({1, 2, 3, 4}, 128);
|
||||
auto binary_empty_with_subtype = json::binary_array({}, 128);
|
||||
|
||||
CHECK_THROWS_AS(binary.dump(), json::type_error);
|
||||
CHECK_THROWS_AS(binary_empty.dump(), json::type_error);
|
||||
CHECK_THROWS_AS(object.dump(), json::type_error);
|
||||
CHECK_THROWS_AS(array.dump(), json::type_error);
|
||||
CHECK_THROWS_WITH(binary.dump(), "[json.exception.type_error.317] cannot serialize binary data to text JSON");
|
||||
CHECK_THROWS_WITH(binary_empty.dump(), "[json.exception.type_error.317] cannot serialize binary data to text JSON");
|
||||
CHECK_THROWS_WITH(object.dump(), "[json.exception.type_error.317] cannot serialize binary data to text JSON");
|
||||
CHECK_THROWS_WITH(array.dump(), "[json.exception.type_error.317] cannot serialize binary data to text JSON");
|
||||
json object = {{"key", binary}};
|
||||
json object_empty = {{"key", binary_empty}};
|
||||
json object_with_subtype = {{"key", binary_with_subtype}};
|
||||
json object_empty_with_subtype = {{"key", binary_empty_with_subtype}};
|
||||
|
||||
json array = {"value", 1, binary};
|
||||
json array_empty = {"value", 1, binary_empty};
|
||||
json array_with_subtype = {"value", 1, binary_with_subtype};
|
||||
json array_empty_with_subtype = {"value", 1, binary_empty_with_subtype};
|
||||
|
||||
SECTION("normal")
|
||||
{
|
||||
CHECK(binary.dump() == "{\"bytes\":[1,2,3,4],\"subtype\":null}");
|
||||
CHECK(binary_empty.dump() == "{\"bytes\":[],\"subtype\":null}");
|
||||
CHECK(binary_with_subtype.dump() == "{\"bytes\":[1,2,3,4],\"subtype\":128}");
|
||||
CHECK(binary_empty_with_subtype.dump() == "{\"bytes\":[],\"subtype\":128}");
|
||||
|
||||
CHECK(object.dump() == "{\"key\":{\"bytes\":[1,2,3,4],\"subtype\":null}}");
|
||||
CHECK(object_empty.dump() == "{\"key\":{\"bytes\":[],\"subtype\":null}}");
|
||||
CHECK(object_with_subtype.dump() == "{\"key\":{\"bytes\":[1,2,3,4],\"subtype\":128}}");
|
||||
CHECK(object_empty_with_subtype.dump() == "{\"key\":{\"bytes\":[],\"subtype\":128}}");
|
||||
|
||||
CHECK(array.dump() == "[\"value\",1,{\"bytes\":[1,2,3,4],\"subtype\":null}]");
|
||||
CHECK(array_empty.dump() == "[\"value\",1,{\"bytes\":[],\"subtype\":null}]");
|
||||
CHECK(array_with_subtype.dump() == "[\"value\",1,{\"bytes\":[1,2,3,4],\"subtype\":128}]");
|
||||
CHECK(array_empty_with_subtype.dump() == "[\"value\",1,{\"bytes\":[],\"subtype\":128}]");
|
||||
}
|
||||
|
||||
SECTION("serialize_binary = true")
|
||||
SECTION("pretty-printed")
|
||||
{
|
||||
auto binary = json::binary_array({1, 2, 3, 4});
|
||||
auto binary_empty = json::binary_array({});
|
||||
json object = {{"key", binary}};
|
||||
json array = {"value", 1, binary};
|
||||
|
||||
CHECK(binary.dump(-1, ' ', false, json::error_handler_t::strict, true) == "b[1,2,3,4]");
|
||||
CHECK(binary_empty.dump(-1, ' ', false, json::error_handler_t::strict, true) == "b[]");
|
||||
CHECK(object.dump(-1, ' ', false, json::error_handler_t::strict, true) == "{\"key\":b[1,2,3,4]}");
|
||||
CHECK(array.dump(-1, ' ', false, json::error_handler_t::strict, true) == "[\"value\",1,b[1,2,3,4]]");
|
||||
}
|
||||
|
||||
SECTION("serialize_binary = true, pretty-printed")
|
||||
{
|
||||
auto binary = json::binary_array({1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20});
|
||||
auto binary_empty = json::binary_array({});
|
||||
json object = {{"key", binary}};
|
||||
json array = {"value", 1, binary};
|
||||
|
||||
CHECK(binary.dump(4, ' ', false, json::error_handler_t::strict, true) == "b[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]");
|
||||
CHECK(binary_empty.dump(4, ' ', false, json::error_handler_t::strict, true) == "b[]");
|
||||
CHECK(object.dump(4, ' ', false, json::error_handler_t::strict, true) == "{\n"
|
||||
" \"key\": b[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]\n"
|
||||
CHECK(binary.dump(4) == "{\n"
|
||||
" \"bytes\": [1, 2, 3, 4],\n"
|
||||
" \"subtype\": null\n"
|
||||
"}");
|
||||
CHECK(array.dump(4, ' ', false, json::error_handler_t::strict, true) == "[\n"
|
||||
CHECK(binary_empty.dump(4) == "{\n"
|
||||
" \"bytes\": [],\n"
|
||||
" \"subtype\": null\n"
|
||||
"}");
|
||||
CHECK(binary_with_subtype.dump(4) == "{\n"
|
||||
" \"bytes\": [1, 2, 3, 4],\n"
|
||||
" \"subtype\": 128\n"
|
||||
"}");
|
||||
CHECK(binary_empty_with_subtype.dump(4) == "{\n"
|
||||
" \"bytes\": [],\n"
|
||||
" \"subtype\": 128\n"
|
||||
"}");
|
||||
|
||||
CHECK(object.dump(4) == "{\n"
|
||||
" \"key\": {\n"
|
||||
" \"bytes\": [1, 2, 3, 4],\n"
|
||||
" \"subtype\": null\n"
|
||||
" }\n"
|
||||
"}");
|
||||
CHECK(object_empty.dump(4) == "{\n"
|
||||
" \"key\": {\n"
|
||||
" \"bytes\": [],\n"
|
||||
" \"subtype\": null\n"
|
||||
" }\n"
|
||||
"}");
|
||||
CHECK(object_with_subtype.dump(4) == "{\n"
|
||||
" \"key\": {\n"
|
||||
" \"bytes\": [1, 2, 3, 4],\n"
|
||||
" \"subtype\": 128\n"
|
||||
" }\n"
|
||||
"}");
|
||||
CHECK(object_empty_with_subtype.dump(4) == "{\n"
|
||||
" \"key\": {\n"
|
||||
" \"bytes\": [],\n"
|
||||
" \"subtype\": 128\n"
|
||||
" }\n"
|
||||
"}");
|
||||
|
||||
CHECK(array.dump(4) == "[\n"
|
||||
" \"value\",\n"
|
||||
" 1,\n"
|
||||
" b[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]\n"
|
||||
" {\n"
|
||||
" \"bytes\": [1, 2, 3, 4],\n"
|
||||
" \"subtype\": null\n"
|
||||
" }\n"
|
||||
"]");
|
||||
CHECK(array_empty.dump(4) == "[\n"
|
||||
" \"value\",\n"
|
||||
" 1,\n"
|
||||
" {\n"
|
||||
" \"bytes\": [],\n"
|
||||
" \"subtype\": null\n"
|
||||
" }\n"
|
||||
"]");
|
||||
CHECK(array_with_subtype.dump(4) == "[\n"
|
||||
" \"value\",\n"
|
||||
" 1,\n"
|
||||
" {\n"
|
||||
" \"bytes\": [1, 2, 3, 4],\n"
|
||||
" \"subtype\": 128\n"
|
||||
" }\n"
|
||||
"]");
|
||||
CHECK(array_empty_with_subtype.dump(4) == "[\n"
|
||||
" \"value\",\n"
|
||||
" 1,\n"
|
||||
" {\n"
|
||||
" \"bytes\": [],\n"
|
||||
" \"subtype\": 128\n"
|
||||
" }\n"
|
||||
"]");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user