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

to_bson member function ignores BinaryType template parameter #4072

Open
1 of 2 tasks
danielalves opened this issue Jul 12, 2023 · 1 comment
Open
1 of 2 tasks

to_bson member function ignores BinaryType template parameter #4072

danielalves opened this issue Jul 12, 2023 · 1 comment
Labels
aspect: binary formats BSON, CBOR, MessagePack, UBJSON kind: bug solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@danielalves
Copy link

Description

Currrently to_bson is implemented like this:

static std::vector<std::uint8_t> to_bson(const basic_json& j)
{
    std::vector<std::uint8_t> result;
    to_bson(j, result);
    return result;
}

which ignores the BinaryType template parameter received by basic_json.

It should use the BinaryType instead:

static BinaryType to_bson(const basic_json& j)
{
    BinaryType result;
    to_bson(j, result);
    return result;
}

Reproduction steps

#include <type_traits>

typedef nlohmann::basic_json<
    std::map,
    std::vector,
    std::string,
    bool,
    std::int64_t,
    std::uint64_t,
    double,
    std::allocator,
    nlohmann::adl_serializer,
    std::vector<std::byte>,
    void
> CustomJson;

void test() {
    CustomJson oauthDataJson = {
        {"key", "value"}
    };
    auto result = CustomJson::to_bson(oauthDataJson);

    // This won't compile because static_assert will fail 
    static_assert(std::is_same_v<CustomJson::binary_t, decltype(result)>);
}

Expected vs. actual results

  • Expected:

    • static_assert(std::is_same_v<CustomJson::binary_t, decltype(result)>) shouldn't fail
  • Actual

    • static_assert(std::is_same_v<CustomJson::binary_t, decltype(result)>) fails

Minimal code example

// Same as `Reproduction Steps`

#include <type_traits>

typedef nlohmann::basic_json<
    std::map,
    std::vector,
    std::string,
    bool,
    std::int64_t,
    std::uint64_t,
    double,
    std::allocator,
    nlohmann::adl_serializer,
    std::vector<std::byte>,
    void
> CustomJson;

void test() {
    CustomJson oauthDataJson = {
        {"key", "value"}
    };
    auto result = CustomJson::to_bson(oauthDataJson);

    // This won't compile because static_assert will fail 
    static_assert(std::is_same_v<CustomJson::binary_t, decltype(result)>);
}

Error messages

No response

Compiler and operating system

all

Library version

3.11.2

Validation

@nlohmann nlohmann added the aspect: binary formats BSON, CBOR, MessagePack, UBJSON label Nov 29, 2024
@nlohmann
Copy link
Owner

The BinaryType is not meant to control the return type of the to_xyz functions, but is used for the binary types which are part of the binary formats, such as fixext1 in MessagePack, see https://json.nlohmann.me/api/basic_json/binary_t.

To write to a container other than std::vector<std::uint8_t>, use overload (2) (see https://json.nlohmann.me/api/basic_json/to_bson/).

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Jan 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
aspect: binary formats BSON, CBOR, MessagePack, UBJSON kind: bug solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants