From e54f03f73ba6a2710fad457a299590ade22c3477 Mon Sep 17 00:00:00 2001 From: Matthew Bauer Date: Thu, 2 Jul 2020 17:40:02 -0400 Subject: [PATCH 1/2] Tag binary values in cbor if set CBOR has tags, which work similarly to "subtype"s: https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml Unsure if this makes sense. Note that the subtype must just be one byte wide. --- include/nlohmann/detail/output/binary_writer.hpp | 6 ++++++ single_include/nlohmann/json.hpp | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/include/nlohmann/detail/output/binary_writer.hpp b/include/nlohmann/detail/output/binary_writer.hpp index 3bac02270..580bc2e54 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 7ab24c844..9269df1a2 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -12641,6 +12641,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) From dd08f7705f621894a657aaef45ec73062c71398e Mon Sep 17 00:00:00 2001 From: Matthew Bauer Date: Mon, 6 Jul 2020 18:06:10 -0400 Subject: [PATCH 2/2] Add simple test for cbor byte --- test/src/unit-cbor.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/src/unit-cbor.cpp b/test/src/unit-cbor.cpp index b1c1a43e6..49dc72018 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")