Repository navigation
Description
operator T() considered harmful
There's two subjects of importance that I'd like to tackle.
This is the first one, I'll open another issue quickly (hopefully).
The problem
There's been several issues in the past related to the implicit conversion operator that the library provides.
The current Readme demonstrates the following use-case:
// conversion: json -> person
ns::person p2 = j;This will call json::operator T() with T = ns::person, so no problems here.
You'd expect the following code to always work too:
ns::person p2;
p2 = j;And it works! Sometimes.
If ns::person has a user-defined operator=, it will break with the following message:
t.cpp: In function ‘int main(int, const char**)’:
t.cpp:14:7: error: ambiguous overload for ‘operator=’ (operand types are ‘ns::person’ and ‘nlohmann::json {aka nlohmann::basic_json<>}’)
p2 = j;
^
t.cpp:6:6: note: candidate: ns::person& ns::person::operator=(int)
ns::person& operator=(int) { return *this; }
^~~~~~~~
t.cpp:3:8: note: candidate: constexpr ns::person& ns::person::operator=(const ns::person&)
struct ns::person
^
t.cpp:3:8: note: candidate: constexpr ns::person& ns::person::operator=(ns::person&&)Hard to understand that error, and it's not something that can be fixed by the library.
It's triggered by the compiler before any template machinery on our side.
Now with such code:
void need_precise_measurement(Milliseconds);
// later on
json j = json::parse(config);
need_precise_measurement(j);It can fail in two cases:
- Someone adds an overload for
need_precise_measurement. Compiler error. - Someone changes the type of the argument to
Nanoseconds. Runtime error at best.
Implicit conversions
Implicit conversions make the library pleasant to use. Especially when implicitely converting to JSON.
That's because we have a templated non-explicit constructor with lots of constraints to correctly handle user types.
There's also a single type to convert to: nlohmann::json, so everything works.
However, when converting implicitely from json, it's the complete opposite. If the compiler cannot decide which type it should implicit convert the nlohmann::json value to, you get a compiler error.
And it could happen in a codebase months after writing the code, (e.g. when adding an operator= to a type).
In the end the only consistent way to convert from a json value is to call json.get<T>.
Proposed change
I propose to simply and completely remove operator T().
It will obviously break code, so it can only be done in a major version.
Of course this change cannot be adopted right away, so I propose to add a deprecation warning inside operator T(), as we did for basic_json::basic_json(std::istream&) overloads.
Then it would be reasonable to remove it in a future major version.
Activity
nlohmann commentedon Feb 4, 2018
Thanks for opening a separate issue and separating this from #951.
I think I understand the problem, and this really may be something to tackle with 4.0.0. The question is whether there could be some middle ground, for instance a "whitelist" of supported types for which we would still have an implicit conversion. Or an extension point to let users add their desired implicit conversions. Or ...
theodelrieu commentedon Feb 4, 2018
First thing I don't like about having a whitelist is that it prevents consistency. Everyone has to remember which types are supported and nobody will.
Secondly, if types that we support get a new
operator=: Ka-Boom.Thirdly, it doesn't solve the second example I've shown with
MillisecondsandNanoseconds.Now for the extension point, the best way to do that is to define another constructor or
operator=to user types.But if users want to have an extension point for
std::string, that's not possible. As I said, compilation fails before we can do anything. It will fail before the extension point mechanism gets called as well.I don't think we can have a middle-ground, but I might be wrong.
theodelrieu commentedon Feb 9, 2018
I think the customization point way can work, I'll try to experiment this weekend.
theodelrieu commentedon Feb 9, 2018
I had some time to experiment before lunch:
But the problem remains, customization point or not, it does not solve anything...
As for the whitelist, we already have a blacklist inside
operator T(), where we explicitely disableconst char*,std::initializer_list<...>etc... just to makestd::stringwork.But it does not make
std::vector, nor any user type with customoperator=work.About a macro that would conditionally disable
operator T(), I find that very dangerous. Just imagine aLibAwhich uses aLibB, both havejsonas a dependency, but not with the same#define...If
LibBimplementation relies on-DJSON_DISABLE_OPERATOR_T=ON, andLibAdoes not, everything will break.My conclusion: I fear that the
operator T()feature is broken by design.stale commentedon Mar 11, 2018
theodelrieu commentedon Mar 15, 2018
stale commentedon Apr 14, 2018
match41 commentedon Apr 24, 2018
I vote for the removal of the implicit converter.
This is the same reason that C++ std::string does not convert to const char* implicitly.
scinart commentedon May 8, 2018
I have a case that supports such removal. Having
operator ValueType() constmakes the following code very confusing.There is nothing wrong with
from_json_tuple, but rather, tuple has this strange behavior:So when it is int, it requires an json->int conversion by using the implicit tuple constructor.
Perhaps it would be something like
json_cast<T>(...)that does the explicit conversion, and throws exception on cast error?stale commentedon Jun 7, 2018
69 remaining items
AlexandreBossard commentedon Jul 15, 2020
I do, be it for feature or bug fix. And again, even if I keep json to a "stable" release, there is still a possibility a code-base change will trigger an unwanted operator T shenanigan.
nlohmann commentedon Jul 15, 2020
I understand your concerns, and I think the first step should definitely be a preprocessor switch to switch off implicit conversions. I can make a PR - is anything in addition to
operator T()to be taken care of?theodelrieu commentedon Jul 15, 2020
@nlohmann The PR is already there: #1559, it needs a rebase though
nlohmann commentedon Jul 15, 2020
@theodelrieu Oh, sorry, totally forgot about #1559. Can you rebase?
maddanio commentedon Jul 15, 2020
I would also like the list initalization to be switchable. It has similat issues.
nlohmann commentedon Jul 15, 2020
What do you mean? What do you want to switch on/off?
theodelrieu commentedon Jul 15, 2020
@nlohmann I just rebased