Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental support for binary type and CBOR (de)serialization #862

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,15 @@ namespace nlohmann
template<typename = void, typename = void>
struct adl_serializer;

// TODO: Probably some wrapper around std::string
class SomeBinaryType;

// forward declaration of basic_json (required to split the class)
template<template<typename, typename, typename...> class ObjectType = std::map,
template<typename, typename...> 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,
Expand All @@ -144,13 +149,14 @@ class basic_json;
#define NLOHMANN_BASIC_JSON_TPL_DECLARATION \
template<template<typename, typename, typename...> class ObjectType, \
template<typename, typename...> 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<typename> class AllocatorType, \
template<typename, typename = void> class JSONSerializer>

#define NLOHMANN_BASIC_JSON_TPL \
basic_json<ObjectType, ArrayType, StringType, BooleanType, \
basic_json<ObjectType, ArrayType, StringType, BinaryType, BooleanType, \
NumberIntegerType, NumberUnsignedType, NumberFloatType, \
AllocatorType, JSONSerializer>

Expand Down Expand Up @@ -519,6 +525,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)
Expand Down Expand Up @@ -8022,6 +8029,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
Expand All @@ -8042,6 +8050,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)
Expand Down
25 changes: 25 additions & 0 deletions test/src/unit-cbor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1967,3 +1967,28 @@ TEST_CASE("examples from RFC 7049 Appendix A")
CHECK(json::parse("{\"Fun\": true, \"Amt\": -2}") == json::from_cbor(std::vector<uint8_t>({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<uint8_t> cbor_value = json::to_cbor(obj);

std::vector<uint8_t> 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);
}