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

Deserializing to a struct as shown on the project homepage throws compile time errors #2665

Closed
1 of 3 tasks
kitattyor opened this issue Mar 5, 2021 · 2 comments
Closed
1 of 3 tasks
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@kitattyor
Copy link

Can you provide a small but working code example?

Code:

static void throwaway2() {
  // a simple struct to model a person
  struct person {
    std::string name;
    std::string address;
    int age;
  };
  // create a person
  person p{"Ned Flanders", "744 Evergreen Terrace", 60};

  // conversion: person -> json
  auto j = R"({
	"name": "Ned Flanders",
	"address": "744 Evergreen Terrace",
	"age": 60
		})"_json;

  std::cout << j << std::endl;

  auto p2 = j.get<person>(); // Error here 

  assert(p == p2);
}

Error:

error C2672: 'nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer,std::vector<uint8_t,std::allocator<uint8_t>>>::get': no matching overloaded function found 

error C2783: 'nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer,std::vector<uint8_t,std::allocator<uint8_t>>> nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer,std::vector<uint8_t,std::allocator<uint8_t>>>::get(void) const': could not deduce template argument for '__formal' 

[build] D:\Libs\vcpkg\installed\x64-windows\include\nlohmann/json.hpp(2848): message : see declaration of 'nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer,std::vector<uint8_t,std::allocator<uint8_t>>>::get' 
  • Compiler: ___ Visual Studio 2017 x64
  • Operating system: ___ Windows 10

Which version of the library did you use?

  • latest release version 3.9.1
  • other release - please state the version: ___
  • the develop branch
@nlohmann
Copy link
Owner

nlohmann commented Mar 5, 2021

It's described here: https://github.com/nlohmann/json#basic-usage:

To make this work with one of your types, you only need to provide two functions:

using nlohmann::json;

namespace ns {
    void to_json(json& j, const person& p) {
        j = json{{"name", p.name}, {"address", p.address}, {"age", p.age}};
    }

    void from_json(const json& j, person& p) {
        j.at("name").get_to(p.name);
        j.at("address").get_to(p.address);
        j.at("age").get_to(p.age);
    }
} // namespace ns

@nlohmann nlohmann added solution: proposed fix a fix for the issue has been proposed and waits for confirmation and removed kind: bug labels Mar 5, 2021
@kitattyor
Copy link
Author

kitattyor commented Mar 5, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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