-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Feat/explicit conversion operator #1559
Feat/explicit conversion operator #1559
Conversation
edc99df
to
5ec90c8
Compare
Where is the LGTM config file? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, sorry I didn't had the time to submit the patch. Thanks for the submission!
My proposed solution was not as perfect as I thought, see my comment. Other than that, the diff seems fine for me!
#ifndef JSON_USE_EXPLICIT_CONVERSIONS | ||
#define JSON_EXPLICIT | ||
#ifndef JSON_USE_IMPLICIT_CONVERSIONS | ||
#warning Implicit conversions are being deprecated and will be \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realized that the #warning
statement is not standard, and not supported on msvc. There is some solution like #pragma message()
but it's not a real warning.
I think we could use [[deprecated("...")]]
or the msvc non standard __declspec(deprecated("..."))
for earlier versions in that case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added JSON_DEPRECATED_MSG, and tried building the tests by removing the Wno-deprecated
warnings, it works \o/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, the patch is great now. I would just wait for @nlohmann to do a review too and merge it!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the idea of having a switch for the conversions, but I am still not convinced that implicit conversions should be deprecated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The main reason is because it is broken today. The pile of issues related to #958 is a good indicator of that. Not to mention the why is quite hard to grasp.
The required changes are quite minimal as well:
std::string s;
s = j;
// becomes
std::string s;
j.get_to(s);
std::string s = j;
// becomes
std::string s(j);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After thinking about it I think @nlohmann is right on not deprecate right now (I still think it should eventually). The compile switch must be there for a while before any deprecation, and the documentation should also rely on explicit conversion and it should recommend enabling the switch.
I would like to see the implicit operator be deprecated too but I also agree that even though the change in the syntax is minimal, its use is widespread so it must be done very gradually.
Edit: moved the example into the issue
5ec90c8
to
fb833af
Compare
There is no config file. Here is a link to the log: https://lgtm.com/projects/g/nlohmann/json/logs/rev/pr-e7a2ac9be19e57d05404374d62bf6a8ab3794bfe/lang:cpp/stage:Build%20merge_d94187b754143b1064d1f7bcd3000e3db0419555. Strange that the test fails. |
But where do you define how to build the library? |
54a0f6b
to
79639b5
Compare
Ok, the LGTM build uses default CMake arguments, therefore it builds with the implicit conversions. @nlohmann do you wish to do like with |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
It would be nice to merge this PR and discuss what is the long-term plan after. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
@nlohmann Shall we merge this PR now that 3.7.0 has been released? |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Since tackling the conversion operator is being candidate for version 4.0.0, I'd vote to add the deprecation back when If we want to use explicit conversion on the next major version, deprecation and the switch is the first step to transition the community. |
8ce2e52
to
ba4ee31
Compare
@nlohmann Seems good finally 🙏 |
CMakeLists.txt
Outdated
@@ -24,6 +24,7 @@ endif () | |||
option(JSON_BuildTests "Build the unit tests when BUILD_TESTING is enabled." ON) | |||
option(JSON_Install "Install CMake targets during install step." ON) | |||
option(JSON_MultipleHeaders "Use non-amalgamated version of the library." OFF) | |||
option(JSON_ExplicitConversions "Enable explicit conversion operator" OFF) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still struggling with the wording. To me, the switch "enable explicit conversion" sounds like an additional feature. Instead, it breaks existing code, because the assumed implicit conversions do not work any longer. If the code already only used explicit conversions, then flipping the switch does not change anything.
@nlohmann About your last comment: I can swap the CMake option definition |
a3e6f5c
to
cc74efb
Compare
wrap implicit conversions tests around the JSON_USE_IMPLICIT_CONVERSIONS macro
cc74efb
to
7973293
Compare
@nlohmann GitHub was unreachable again, sigh. Can you restart the job please? 🙏 (https://travis-ci.org/github/nlohmann/json/jobs/710669524#L5359) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
@nlohmann It seems appveyor is stuck as well 🤔 |
No, it's just slow... |
@@ -24,6 +24,7 @@ endif () | |||
option(JSON_BuildTests "Build the unit tests when BUILD_TESTING is enabled." ON) | |||
option(JSON_Install "Install CMake targets during install step." ON) | |||
option(JSON_MultipleHeaders "Use non-amalgamated version of the library." OFF) | |||
option(JSON_ImplicitConversions "Enable implicit conversions" ON) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe put period at end of description for consistency's sake.
I wonder if this can be removed to re-enable conversions to
I think it was disabled because of a problem with string views and visual studio, but I'm not sure. If someone can find the issue/pull request, that would be awesome. |
@gracicot Was this changed in this PR? |
No. The pull request mentioned that this hasn't been done for |
I see. Let's handle this in a follow-up issue. This PR has been open for too long... |
Thank you all so much! |
🔖 Release itemThis issue/PR will be part of the next release of the library. This template helps preparing the release notes. Type
Description
|
@gracicot I think all these type of explicit SFINAE disabling can be removed when using explicit conversions. IIRC there is one for |
Thank you so much! 🎉 |
Thanks @theodelrieu for driving this! |
Here it is!
Thanks to @gracicot, who found a good solution to #958, I'm finally attempting to solve it.
This PR introduces a CMake option:
JSON_ExplicitConversions
, defaulted toOFF
.To help users move away from the implicit conversion operator, a warning will be issued each time it is used. This warning can be silenced by users defining
JSON_USE_IMPLICIT_CONVERSIONS
.On the other hand, defining
JSON_USE_EXPLICIT_CONVERSIONS
will very likely break code, but will ease the transition to the next major version, where only explicit conversion operator will be kept.Meanwhile, I've removed every implicit conversion from the library's code, including tests.
It might be a good idea to add a short
ifdef
ed test case, which tests implicit conversions (to avoid duplicating a lot of test code).With explicit conversions, we can also remove the constraints that are present today, e.g. forbidding conversion from
char
,std::initializer_list<char>
. Note that this has not been done in this PR.I think this change is more suited to be released in a minor version, so we might release 3.6.2 first?
Pull request checklist
Read the Contribution Guidelines for detailed information.
include/nlohmann
directory, runmake amalgamate
to create the single-header filesingle_include/nlohmann/json.hpp
. The whole process is described here.