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

Problem with serialization of my custom template doubly-linked list #1881

Closed
blazejwylegly opened this issue Dec 26, 2019 · 6 comments
Closed

Comments

@blazejwylegly
Copy link

blazejwylegly commented Dec 26, 2019

Im trying to write a function responsible for saving my custom list into json file.

The list i want to save into file consists of Node objects (Node is inner class of List). The list i need to save uses Doctor objects as T ( List is template class defined in single header file).

Following the guide, I've implemented toJson method for Doctor, and toJson method for my list:
`
template < class T >
void List::toJson(nlohmann::json& j, const List< T >& list) {

j = json::array();
for (Node* current = list.head; current != nullptr; current = current->getNext()) {
	nlohmann::json temp = current->getData();
	j.push_back(temp);
}

}`

(*) getData() method returns reference to Doctor object stored in Node.

Now, in separate class JsonWriter i use method (List.h is included in JsonWriter.h)

void JsonWriter::saveDoctorList(const List< Doctor >& doctorList)
{

file.open("doctorList.json", std::ios::out);
if (file.good()) {
	nlohmann::json myArray = doctorList;
	
}			

}'
...and that's the point where i get stuck. Inside if statement, the compilers signals that
"no suitable user-defined conversion from "const List< Doctor >" to "nlohmann::json" exists".

When i try to do the same nlohmann::json myArray = doctorList; inside my List.h file it compiles without an error.

I've tried to find solution on both github and StackOverflow. I've also looked through issues here, but I can't find anything that solves my problems. Can anybody tell me what am I missing?

Using VS 2017 and Windows

Thanks for any suggestions.

@nlohmann
Copy link
Owner

You need to implement a free function to_json. See https://github.com/nlohmann/json/blob/develop/README.md#arbitrary-types-conversions for more information.

@blazejwylegly
Copy link
Author

Sadly I don't think, that I understand what do you mean by "Free function". I've tried to follow the guide you have linked before posting the issue. I've implemented to_json function for both Doctor and List, similiar to those presented in documentation. I assume, that my List::to_json(...) function is not visible in other files. What am I missing?

@nlohmann
Copy link
Owner

It must not be a member function.

@blazejwylegly
Copy link
Author

blazejwylegly commented Dec 27, 2019

I think i've understood in correctly, but im still struggling to solve this. Here's what ive got so far:

  • friend void toJson(nlohmann::json& j, const Doctor& obj); declared in Doctor.h file, and definied in Doctor.cpp file.
  • friend void toJson(nlohmann::json& j, const List<T>& list); declared in List.h, defined in List.h as following:
    `
    template < class T >

void toJson(nlohmann::json& j, const List< T >& list)
{
j = nlohmann::json::array();
for (List::Node* current = list.head; current != nullptr; current = current->getNext()) {
nlohmann::json temp = current->getData();
j.push_back(temp);
}
};`

  • static void saveDoctorList(const List<Doctor>& doc); declared in JsonWriter.h file, defined in JsonWriter.cpp as following:
    `

void JsonWriter::saveDoctorList(const List& doc){
file.open("doctorList.json", std::ios::out);
if (file.good())
{
nlohmann::json obj = doc;
file << obj;
file.close();
}
}
`
Both toJson methods for Doctor and List are defined outside of their class declaration.

Line nlohmann::json obj = doc produces the same error i have mentioned above.
no suitable user-defined conversion from "const List< Doctor >" to "nlohmann::json" exists.

When I try to test same functionality for Doctor in main.cpp
Doctor* doc = new Doctor(...); const Doctor& docRef = *doc;
I get same error:
no suitable user-defined conversion from "const Doctor" to "nlohmann::json" exists.
And that brings me to another question:
Why does line above produce error, and line nlohmann::json temp = current->getData(); from void toJson(nlohmann::json& j, const List< T >& list) compile without an error?

I suppose that my problem is trivial but i can't get over it.

Btw. sorry for poor code pasting.

@nlohmann
Copy link
Owner

Please have a look at the README. The function must be called to_json just as it is described there.

@blazejwylegly
Copy link
Author

Oh... I completely missed it. Didn't think of it at all. Changing function name solved problem. Thank you for your time @nlohmann .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants