Skip to content

from_cbor incompatible with std::vector<std::byte> as binary_t #4804

@burnpanck

Description

@burnpanck

Description

Trying to use json::from_cbor with a binary_t set to std::vector<std::byte> will fail because the decoder appears to be hard-coded to push_back elements of std::uint8_t.

It appears to be related to #4756 where they report essentially the same issue for from_msgpack; although that one was fixed in #4760 by adding a specialisation to char_traits. It appears that type trait is not being used in the implementation of from_cbor.

Reproduction steps

Attempt to compile the below minimal example.

Expected vs. actual results

Expected to compile.

Minimal code example

#include <nlohmann/json.hpp>

using json = nlohmann::basic_json<std::map,        // ObjectType
                                  std::vector,     // ArrayType
                                  std::string,     // StringType
                                  bool,            // BooleanType
                                  std::int64_t,    // NumberIntegerType
                                  std::uint64_t,   // NumberUnsignedType
                                  double,          // NumberFloatType
                                  std::allocator,  // AllocatorType
                                  nlohmann::adl_serializer,  // JSONSerializer
                                  std::vector<std::byte>,    // BinaryType
                                  void                       // CustomBaseClass
                                  >;

int main(){
    std::vector<char> data;
    auto decoded = json::from_cbor(data);
    return 0;
}

See also on Compiler Explorer.

Error messages

On Clang 20:

/opt/compiler-explorer/libs/nlohmann_json/trunk/single_include/nlohmann/json.hpp:12768:20: error: no matching member function for call to 'push_back'
 12768 |             result.push_back(static_cast<std::uint8_t>(current));

Compiler and operating system

Clang 20, GCC 15, both on Compiler Explorer (i.e. linux?)

Library version

trunk

Validation

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions