diff --git a/include/nlohmann/detail/output/binary_writer.hpp b/include/nlohmann/detail/output/binary_writer.hpp index 467346467a..342cb47845 100644 --- a/include/nlohmann/detail/output/binary_writer.hpp +++ b/include/nlohmann/detail/output/binary_writer.hpp @@ -279,6 +279,12 @@ class binary_writer case value_t::binary: { + if (j.m_value.binary->has_subtype()) + { + write_number(static_cast(0xd8)); + write_number(j.m_value.binary->subtype()); + } + // step 1: write control byte and the binary array size const auto N = j.m_value.binary->size(); if (N <= 0x17) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index cf41f741af..1d8b5fb612 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -12795,6 +12795,12 @@ class binary_writer case value_t::binary: { + if (j.m_value.binary->has_subtype()) + { + write_number(static_cast(0xd8)); + write_number(j.m_value.binary->subtype()); + } + // step 1: write control byte and the binary array size const auto N = j.m_value.binary->size(); if (N <= 0x17) diff --git a/test/src/unit-cbor.cpp b/test/src/unit-cbor.cpp index 6af06bf60f..2aeac10d46 100644 --- a/test/src/unit-cbor.cpp +++ b/test/src/unit-cbor.cpp @@ -2503,6 +2503,8 @@ TEST_CASE("examples from RFC 7049 Appendix A") std::vector expected((std::istreambuf_iterator(f_bin)), std::istreambuf_iterator()); CHECK(j == json::binary(expected)); + + CHECK(json::to_cbor(json::binary(std::vector {}, 0x42)) == std::vector {0xd8, 0x42, 0x40}); } SECTION("arrays")