mirror of
https://github.com/nlohmann/json.git
synced 2026-10-12 10:13:13 +00:00
#5787 missed the instantiations spelled as `basic_json <` (astyle's formatting) or ending in the CustomBaseClass argument. Convert them, including the binary_t.md example pointed out in review. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
88 lines
3.3 KiB
C++
88 lines
3.3 KiB
C++
// __ _____ _____ _____
|
|
// __| | __| | | | JSON for Modern C++ (supporting code)
|
|
// | | |__ | | | | | | version 3.12.0
|
|
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
|
|
//
|
|
// SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
#include "doctest_compatibility.h"
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
#include <cstdint>
|
|
#include <functional>
|
|
#include <map>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#ifdef JSON_HAS_CPP_17
|
|
#include <cstddef>
|
|
#endif
|
|
|
|
namespace
|
|
{
|
|
|
|
// a BinaryType whose value type is signed: the elements must still be
|
|
// processed as the numbers 0..255
|
|
using char_binary_json = nlohmann::json::with_binary_t<std::vector<char>>;
|
|
|
|
#ifdef JSON_HAS_CPP_17
|
|
// a BinaryType whose value type is not an integer type at all
|
|
using byte_binary_json = nlohmann::json::with_binary_t<std::vector<std::byte>>;
|
|
#endif
|
|
|
|
} // namespace
|
|
|
|
TEST_CASE("binary type whose value type is not std::uint8_t")
|
|
{
|
|
SECTION("a signed value type does not dump negative numbers")
|
|
{
|
|
const std::vector<char> chars{'\0', '\x01', '\xFF'};
|
|
CHECK(char_binary_json::binary(chars).dump() == R"({"bytes":[0,1,255],"subtype":null})");
|
|
CHECK(char_binary_json::binary(chars, 42).dump() == R"({"bytes":[0,1,255],"subtype":42})");
|
|
CHECK(char_binary_json::binary({}).dump() == R"({"bytes":[],"subtype":null})");
|
|
}
|
|
|
|
SECTION("a value is converted to the binary type if it is binary or an array")
|
|
{
|
|
const std::vector<char> chars{'\0', '\x01', '\x7F'};
|
|
CHECK(char_binary_json::binary(chars).get<std::vector<char>>() == chars);
|
|
CHECK(char_binary_json({0, 1, 127}).get<std::vector<char>>() == chars);
|
|
CHECK_THROWS_WITH_AS(char_binary_json(1).get<std::vector<char>>(),
|
|
"[json.exception.type_error.302] type must be binary or array, but is number",
|
|
char_binary_json::type_error&);
|
|
}
|
|
|
|
SECTION("the default binary type is unchanged")
|
|
{
|
|
CHECK(nlohmann::json::binary({0, 1, 255}, 42).dump() == R"({"bytes":[0,1,255],"subtype":42})");
|
|
}
|
|
|
|
#ifdef JSON_HAS_CPP_17
|
|
SECTION("dumping a value type that is not an integer")
|
|
{
|
|
const std::vector<std::byte> bytes{std::byte{0}, std::byte{1}, std::byte{0xFF}};
|
|
CHECK(byte_binary_json::binary(bytes).dump() == R"({"bytes":[0,1,255],"subtype":null})");
|
|
CHECK(byte_binary_json::binary(bytes, 42).dump() == R"({"bytes":[0,1,255],"subtype":42})");
|
|
CHECK(byte_binary_json::binary({}).dump() == R"({"bytes":[],"subtype":null})");
|
|
}
|
|
|
|
SECTION("hashing and the binary formats")
|
|
{
|
|
const std::vector<std::byte> bytes{std::byte{0}, std::byte{1}, std::byte{0xFF}};
|
|
const auto j = byte_binary_json::binary(bytes);
|
|
|
|
CHECK(std::hash<byte_binary_json> {}(j) == std::hash<byte_binary_json> {}(j));
|
|
CHECK(byte_binary_json::from_cbor(byte_binary_json::to_cbor(j)) == j);
|
|
CHECK(byte_binary_json::from_msgpack(byte_binary_json::to_msgpack(j)) == j);
|
|
|
|
// UBJSON has no binary type, so binary values are written as an array
|
|
CHECK(byte_binary_json::from_ubjson(byte_binary_json::to_ubjson(j)) == byte_binary_json({0, 1, 255}));
|
|
// the same holds for BON8
|
|
CHECK(byte_binary_json::from_bon8(byte_binary_json::to_bon8(j)) == byte_binary_json({0, 1, 255}));
|
|
}
|
|
#endif
|
|
}
|