diff --git a/src/json.hpp b/src/json.hpp index 5b0b0ea5b..c40919a51 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -127,10 +127,15 @@ namespace nlohmann template struct adl_serializer; +// TODO: Probably some wrapper around std::string +class SomeBinaryType; + // forward declaration of basic_json (required to split the class) template class ObjectType = std::map, template class ArrayType = std::vector, - class StringType = std::string, class BooleanType = bool, + class StringType = std::string, + class BinaryType = SomeBinaryType, + class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, @@ -144,13 +149,14 @@ class basic_json; #define NLOHMANN_BASIC_JSON_TPL_DECLARATION \ template class ObjectType, \ template class ArrayType, \ - class StringType, class BooleanType, class NumberIntegerType, \ - class NumberUnsignedType, class NumberFloatType, \ + class StringType, class BinaryType, class BooleanType, \ + class NumberIntegerType, class NumberUnsignedType, \ + class NumberFloatType, \ template class AllocatorType, \ template class JSONSerializer> #define NLOHMANN_BASIC_JSON_TPL \ - basic_json @@ -518,6 +524,7 @@ enum class value_t : uint8_t object, ///< object (unordered set of name/value pairs) array, ///< array (ordered collection of values) string, ///< string value + binary, ///< binary value boolean, ///< boolean value number_integer, ///< number value (signed integer) number_unsigned, ///< number value (unsigned integer) @@ -8042,6 +8049,7 @@ class basic_json object | object | pointer to @ref object_t array | array | pointer to @ref array_t string | string | pointer to @ref string_t + binary | binary | pointer to @ref binary_t boolean | boolean | @ref boolean_t number | number_integer | @ref number_integer_t number | number_unsigned | @ref number_unsigned_t @@ -8062,6 +8070,8 @@ class basic_json array_t* array; /// string (stored with pointer to save storage) string_t* string; + /// binary (stored with pointer to save storage) + binary_t* binary; /// boolean boolean_t boolean; /// number (integer) diff --git a/test/src/unit-cbor.cpp b/test/src/unit-cbor.cpp index 1e37a9821..d566c08f8 100644 --- a/test/src/unit-cbor.cpp +++ b/test/src/unit-cbor.cpp @@ -1967,3 +1967,28 @@ TEST_CASE("examples from RFC 7049 Appendix A") CHECK(json::parse("{\"Fun\": true, \"Amt\": -2}") == json::from_cbor(std::vector({0xbf, 0x63, 0x46, 0x75, 0x6e, 0xf5, 0x63, 0x41, 0x6d, 0x74, 0x21, 0xff}))); } } + +TEST_CASE("binary_data") { + json obj; + obj.emplace("age", 12); + obj.emplace("name", "Bob"); + obj.emplace("secret", json::binary_t{'h', 'e', 'l', 'l', 'o'}); + + std::vector cbor_value = json::to_cbor(obj); + + std::vector expected_value{ + 0b10100011, // map with 3 pairs + 0b01100011, 'a', 'g', 'e', // "age" key + 0b00001100, // 12 value + 0b01100100, 'n', 'a', 'm', 'e', // "name" key + 0b01100011, 'B', 'o', 'b', // "Bob" value + 0b01100110, 's', 'e', 'c', 'r', 'e', 't', // "secret" key + 0b01000101, 'h', 'e', 'l', 'l', 'o' // binary value + }; + + CHECK(cbor_value == expected_value); + + json obj_parsed = json::from_cbor(cbor_value); + + CHECK(obj == obj_parsed); +} \ No newline at end of file