Skip to content

Commit

Permalink
Merge pull request #2244 from matthewbauer/tag-cbor
Browse files Browse the repository at this point in the history
Tag binary values in cbor if set
  • Loading branch information
nlohmann authored Jul 17, 2020
2 parents e65cc9d + dd08f77 commit a10d486
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions include/nlohmann/detail/output/binary_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,12 @@ class binary_writer

case value_t::binary:
{
if (j.m_value.binary->has_subtype())
{
write_number(static_cast<std::uint8_t>(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)
Expand Down
6 changes: 6 additions & 0 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12795,6 +12795,12 @@ class binary_writer

case value_t::binary:
{
if (j.m_value.binary->has_subtype())
{
write_number(static_cast<std::uint8_t>(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)
Expand Down
2 changes: 2 additions & 0 deletions test/src/unit-cbor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2503,6 +2503,8 @@ TEST_CASE("examples from RFC 7049 Appendix A")
std::vector<uint8_t> expected((std::istreambuf_iterator<char>(f_bin)),
std::istreambuf_iterator<char>());
CHECK(j == json::binary(expected));

CHECK(json::to_cbor(json::binary(std::vector<uint8_t> {}, 0x42)) == std::vector<uint8_t> {0xd8, 0x42, 0x40});
}

SECTION("arrays")
Expand Down

0 comments on commit a10d486

Please sign in to comment.