Closed NeroBurner closed 2 years ago
Can you try this:
template<typename BasicJsonType>
void from_json(const BasicJsonType &j, MyClass &obj)
{
j.at("name").get_to(obj.name);
}
@gregmarr the workaround works! Although I don't want to use the templated function, as I'd have to move all my from_json()
implementations from the cpp
file into the hpp
header files. But thanks for taking the time and providing a workaround :bow:
Are you currently putting extern void from_json(const nlohmann::json &j, MyClass &obj);
in your header files? If not, and it's currently working with nlohmann::json
, then you don't need to move the implementations to get the templated version to work with nlohmann::unordered_json
.
If you are doing that, you can just add a second version just like the first:
void from_json(const nlohmann::unordered_json &j, MyClass &obj)
{
j.at("name").get_to(obj.name);
}
If you don't want to duplicate the whole body, you can do something like this:
template<typename BasicJsonType>
void from_json_internal(const BasicJsonType &j, MyClass &obj)
{
j.at("name").get_to(obj.name);
}
void from_json(const nlohmann::json &j, MyClass &obj)
{
from_json_internal(j, obj);
}
void from_json(const nlohmann::unordered_json &j, MyClass &obj)
{
from_json_internal(j, obj);
}
@nlohmann Do we need to update the guides to tell people to use the templatized version if they're going to be using the other json
variants?
@nlohmann Do we need to update the guides to tell people to use the templatized version if they're going to be using the other
json
variants?
Yes, I think this was forgotten when ordered_json
was introduced.
so it was just a bug, that I was able to use it like that until now? (before C++20)
@NeroBurner Not sure why it worked, there may have been some temporaries being created that probably aren't desired.
@nlohmann It's not just ordered_json
, it's any variation in the template parameters.
that's unfortunate, but you're probably right. Thanks for the workarounds. Should I close issue, or do you want to keep it open until documentation is updated for people to find this open issue?
I would leave it open for the docs. Thanks.
Out of curiosity I did a git bisect
and found the following commit 0e694b4060ed55df980eaaebc2398b0ff24530d4 (between v3.10.3 and v3.10.4) to introduce this "non-bug"
0e694b4060ed55df980eaaebc2398b0ff24530d4 is the first bad commit
commit 0e694b4060ed55df980eaaebc2398b0ff24530d4
Author: Théo DELRIEU <theo.delrieu@tanker.io>
Date: Thu Oct 14 19:19:46 2021 +0200
fix std::filesystem::path regression (#3073)
* meta: rework is_compatible/is_constructible_string_type
These type traits performed an incorrect and insufficient check.
Converting to a std::filesystem::path used to work by accident thanks to
these brittle constraints, but the clean-up performed in #3020 broke them.
* support std::filesystem::path
Fixes #3070
include/nlohmann/detail/conversions/from_json.hpp | 18 ++-
include/nlohmann/detail/conversions/to_json.hpp | 13 ++
include/nlohmann/detail/meta/type_traits.hpp | 39 ++----
single_include/nlohmann/json.hpp | 71 ++++++-----
test/src/unit-regression2.cpp | 139 ++++++++++++----------
5 files changed, 161 insertions(+), 119 deletions(-)
Awesome!! Thank you so much for fixing this. I thought it won't be fixed because it was unintentional behaviour and creates temporary copies of the json
object if the function doesn't match. But now it is fixed! :tada:
When possible I'll do the right fix with the templated from_json
functions, but it is great to consciously keep the inefficient source code and still be able to update nlohmann_json
Thank you! :bow:
What is the issue you have?
Parsing a custom class with a
std::string
member in it usingnlohmann::ordered_json
with C++20 starting with v3.10.4 results in a compilation error.Using v3.10.3 compiles fine
Probably related issue: https://github.com/nlohmann/json/issues/3207
Please describe the steps to reproduce the issue.
Godbolt link with example showing that g++ 11.2.0 fails to compile when C++20 is enabled
https://godbolt.org/z/MsYe45qP9
std::string
(custom class with just an int works)from_json
function for custom classordered_json
to class (normal unordered json works)Example code, which I'd like to add to unittests, but don't know where. (for the error message I added it to the bottom of
unit-deserialization.cpp
)Can you provide a small but working code example?
see above
What is the expected behavior?
Compile without error
And what is the actual behavior instead?
Compillation error:
Error message when compiling with Visual Studio 16 2019 in C++20 mode:
Which compiler and operating system are you using?
Also on Visual Studio 16 2019 in C++20 mode
Which version of the library did you use?
develop
branchIf you experience a compilation error: can you compile and run the unit tests?
edit: the unittest
test-comparison
fails in C++20 mode, as reported in issue: https://github.com/nlohmann/json/issues/3207