Skip to content

Cannot assign from ordered_json vector<CustomStruct> to value in not ordered json #2528

@kotori2

Description

@kotori2

What is the issue you have?

I created a custom struct in unordered_json with vector, then assign it to an ordered json, it will throw error.

Please describe the steps to reproduce the issue.

Check the snippet below.
Actually it works if both of them are ordered json or unordered json, or I removed the vector.

Can you provide a small but working code example?

I defined my own NLOHMANN_DEFINE_TYPE_INTRUSIVE just to conveniently define them in unordered json.

using nlohmann::json;
using ordered_json = nlohmann::ordered_json;

#define _NLOHMANN_DEFINE_TYPE_INTRUSIVE(Type, ...)  \
    friend void to_json(ordered_json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
    friend void from_json(const ordered_json& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) }

class releaseInfo {
public:
    uint64_t id = 0;
    std::string key;
    _NLOHMANN_DEFINE_TYPE_INTRUSIVE(releaseInfo, id, key);
};

int main() {
    std::vector<releaseInfo> test;
    json parent;
    parent["test"] = test;
    return 0;
}

What is the expected behavior?

No error happened.

And what is the actual behavior instead?

/Users/kotori0/code/untitled/main.cpp:21:15: error: no viable overloaded '='
    a["test"] = test;
    ~~~~~~~~~ ^ ~~~~
/Users/kotori0/code/untitled/cmake-build-debug/_deps/json-src/single_include/nlohmann/json.hpp:18662:17: note: candidate function not viable: no known conversion from 'std::vector<releaseInfo>' to 'nlohmann::basic_json<std::map, std::vector, std::__1::basic_string<char>, bool, long long, unsigned long long, double, std::allocator, adl_serializer, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > >' for 1st argument
    basic_json& operator=(basic_json other) noexcept (
                ^
1 error generated.

Which compiler and operating system are you using?

  • Compiler: Apple clang version 12.0.0 (clang-1200.0.32.27)
  • Operating system: Darwin Hackintosh 20.1.0 Darwin Kernel Version 20.1.0: Sat Oct 31 00:07:11 PDT 2020; root:xnu-7195.50.7~2/RELEASE_X86_64 x86_64

Which version of the library did you use?

  • latest release version 3.9.1
  • other release - please state the version: ___
  • the develop branch

If you experience a compilation error: can you compile and run the unit tests?

  • yes
  • no - please copy/paste the error message below

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind: bugsolution: proposed fixa fix for the issue has been proposed and waits for confirmation

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions