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

nlohmann::to_json method not acting as expected with nlohmann::adl_serializer specialization. #3340

Closed
3 of 5 tasks
NovaSmoof opened this issue Feb 18, 2022 · 3 comments
Closed
3 of 5 tasks
Labels
state: needs more info the author of the issue needs to provide more details

Comments

@NovaSmoof
Copy link

I could not get the nlohmann::to_json method to work consistently with specializations of the nlohmann::adl_serializer which I assumed would work from the way described in the readme.

What is the issue you have?

I was using the How do I convert third-party types explanation and I specialized the adl_serializer in the nlohmann namespace with my third party types, Eigen's Vectors and Quaternions, I also did for some of my own types because they were move-constructible only. It sort of works. I would attempt to call nlohmann::to_json on my class's to to_json them. Because that appeared to work. (I looked at the how to make your own serializer for a small amount of guidance on implementing the functions) I did the nlohmann::to_json method because it was intuitive to me but it doesn't compile with nlohmann::to_json(json, Quaternion{}), things appear to compile when I do json = Quaternion{} though so I'm going to use that for now.

Please describe the steps to reproduce the issue.

  1. Define a specialization of the nlohmann::adl_serializer with some type
  2. Call the nlohmann::to_json on a type defined like this.
  3. Coin flip if it will compile or not.

Can you provide a small but working code example?

This is a repository I set up very quickly to show off the smallest case I had of this occurring. It should be cross platform in terms of files. It's simply a main file that includes your include directory and eigen's include directory.
https://github.com/NovaSmoof/Eigen-nlohmann-conflict

What is the expected behavior?

that I can call nlohmann::to_json after specializing the adl_serializer.

And what is the actual behavior instead?

That I could not.

Which compiler and operating system are you using?

  • Compiler: MSVC C++ from Visual Studio Community 2019 version 16.11.10
  • Operating system: Windows 10

Which version of the library did you use?

I test both. Neither worked for MSVC.

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

I tested with both and it failed with both.

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

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

I didn't get a compilation error, but my project's settings are really weird and it wouldn't build your unit tests right and your instructions didn't work. I tried it for an hour. And if you would like me to I'd be more than happy to attempt again, but as of right now I can't get many of them to run. I had difficulties downloading the data.

@nlohmann
Copy link
Owner

nlohmann commented Apr 3, 2022

You're not supposed to call to_json or from_json directly. That is, instead of calling

nlohmann::adl_serializer<Thing>::to_json(json["Thing"], thing);
nlohmann::to_json(json["Thing"], thing);
json["Thing"] = thing;

call

json["Thing"] = thing;

Making these changes let me compile your example code. As the main is empty, I have no idea what to expect running it though.

@nlohmann nlohmann added state: needs more info the author of the issue needs to provide more details and removed kind: bug labels Jul 29, 2022
@nlohmann
Copy link
Owner

@NovaSmoof Do you need further assistance?

@falbrechtskirchinger
Copy link
Contributor

Note that Eigen had some issues with versions 3.10.4 and 3.10.5 (#3267). I think it had to do with the string type constraints, i.e., a similar issue as #3602.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: needs more info the author of the issue needs to provide more details
Projects
None yet
Development

No branches or pull requests

3 participants