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
Description
Trying to use
json::from_cborwith abinary_tset tostd::vector<std::byte>will fail because the decoder appears to be hard-coded topush_backelements ofstd::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 tochar_traits. It appears that type trait is not being used in the implementation offrom_cbor.Reproduction steps
Attempt to compile the below minimal example.
Expected vs. actual results
Expected to compile.
Minimal code example
See also on Compiler Explorer.
Error messages
On Clang 20:
Compiler and operating system
Clang 20, GCC 15, both on Compiler Explorer (i.e. linux?)
Library version
trunk
Validation
developbranch is used.