nlohmann / json

JSON for Modern C++
https://json.nlohmann.me
MIT License
41.46k stars 6.59k forks source link

NLOHMANN_DEFINE_TYPE_INTRUSIVE with nlohmann::json::json_pointer #4104

Open risa2000 opened 11 months ago

risa2000 commented 11 months ago

Description

Using NLOHMANN_DEFINE_TYPE_INTRUSIVE (or non-intrusive) macro to define conversions for struct with nlohmann::json::json_pointer member fails to compile (MSVC v19.3 - VS 2022), with or without explicit conversion.

Reproduction steps

Compile the sample code. Here is a Godbolt link https://godbolt.org/z/fdrPrcT7T

Expected vs. actual results

Expecting it will compile.

Minimal code example

#include <nlohmann/json.hpp>
#include <fmt/format.h>
#include <string>

inline void to_json(nlohmann::json& j, const nlohmann::json::json_pointer& crj)
{
    j = nlohmann::json(crj.to_string());
}

inline void from_json(const nlohmann::json& cj, nlohmann::json::json_pointer& rj)
{
    rj = nlohmann::json::json_pointer(cj.get<std::string>());
}

struct Test {
    nlohmann::json::json_pointer ptr;
//    std::string ptr;
    nlohmann::json value;
    NLOHMANN_DEFINE_TYPE_INTRUSIVE(Test, ptr, value);
};

// Type your code here, or load an example.
int main() {
    Test test{nlohmann::json::json_pointer("/test"), 10};
    nlohmann::json jtest = test;
    fmt::print("jtest={}\n", jtest.dump());
}

Error messages

example.cpp
<source>(19): error C2672: 'nlohmann::json_abi_v3_11_2::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::json_abi_v3_11_2::adl_serializer,std::vector<uint8_t,std::allocator<uint8_t>>>::get_to': no matching overloaded function found
C:/data/libraries/installed/x64-windows/include\nlohmann/json.hpp(1809): note: could be 'Array nlohmann::json_abi_v3_11_2::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::json_abi_v3_11_2::adl_serializer,std::vector<uint8_t,std::allocator<uint8_t>>>::get_to(T (&)[N]) noexcept(<expr>) const'
<source>(19): note: 'Array nlohmann::json_abi_v3_11_2::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::json_abi_v3_11_2::adl_serializer,std::vector<uint8_t,std::allocator<uint8_t>>>::get_to(T (&)[N]) noexcept(<expr>) const': could not deduce template argument for 'T (&)[N]' from 'nlohmann::json_abi_v3_11_2::json_pointer<StringType>'
        with
        [
            StringType=std::string
        ]
C:/data/libraries/installed/x64-windows/include\nlohmann/json.hpp(1809): note: see declaration of 'nlohmann::json_abi_v3_11_2::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::json_abi_v3_11_2::adl_serializer,std::vector<uint8_t,std::allocator<uint8_t>>>::get_to'
C:/data/libraries/installed/x64-windows/include\nlohmann/json.hpp(1798): note: or       'ValueType &nlohmann::json_abi_v3_11_2::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::json_abi_v3_11_2::adl_serializer,std::vector<uint8_t,std::allocator<uint8_t>>>::get_to(ValueType &) const'
<source>(19): note: 'ValueType &nlohmann::json_abi_v3_11_2::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::json_abi_v3_11_2::adl_serializer,std::vector<uint8_t,std::allocator<uint8_t>>>::get_to(ValueType &) const': could not deduce template argument for '__formal'
C:/data/libraries/installed/x64-windows/include\nlohmann/json.hpp(1798): note: see declaration of 'nlohmann::json_abi_v3_11_2::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::json_abi_v3_11_2::adl_serializer,std::vector<uint8_t,std::allocator<uint8_t>>>::get_to'
C:/data/libraries/installed/x64-windows/include\nlohmann/json.hpp(1785): note: or       'ValueType &nlohmann::json_abi_v3_11_2::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::json_abi_v3_11_2::adl_serializer,std::vector<uint8_t,std::allocator<uint8_t>>>::get_to(ValueType &) noexcept(<expr>) const'
<source>(19): note: 'ValueType &nlohmann::json_abi_v3_11_2::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::json_abi_v3_11_2::adl_serializer,std::vector<uint8_t,std::allocator<uint8_t>>>::get_to(ValueType &) noexcept(<expr>) const': could not deduce template argument for '__formal'
C:/data/libraries/installed/x64-windows/include\nlohmann/json.hpp(1785): note: see declaration of 'nlohmann::json_abi_v3_11_2::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::json_abi_v3_11_2::adl_serializer,std::vector<uint8_t,std::allocator<uint8_t>>>::get_to'
Compiler returned: 2

Compiler and operating system

MSVC v19.3 - VS 2022

Library version

3.11.2

Validation