-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
Fix to_json
for enums when the enum has an unsigned underlying type.
#4237
Conversation
Add a new enum type with uint64_t as the underlying type. Use it in the overall UDT. Not strictly needed, but it helps exercise its expected usage. Create an object of this enum type with a large value (negative if cast to int64_t). Perform several checks on this object as converted to `json`, which fail without the fix.
Select the correct json type depending on the signedness of the enum's underlying type. This fixes the new checks in the unit test.
I ran `make pretty` but that modified 20 files, performing a significant amount of indentation changes, none of them related to my change. I ran `make amalgamate`, but that did nothing. Apparently, the make rule won't run if the single_include files have already been updated by `make pretty`. I forced `make amalgamate` to do the work by touching the file with the fix. I then decided to keep just the minimal needed change: the addition of the fix to the single_include file. I just am not conversant enough in Linux to know whether I installed astyle correctly (had to clone the source from a beta branch and build, in order to get support for `--squeeze-lines`).
The |
Thanx! Looking through the codebase I saw this is typically not done. Also, I saw that |
If bare |
@gregmarr: TBH I'm considering it. It looks like <stdint.h> is being included via the Hedley library. Trivia (which we have had to contend with recently 😊):
As for this json library: it includes both (via Hedley) so it provides both. Arguably, Thanx! |
Yes, please always add the std part. |
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.
Some minor things. Please have a look at the failing CI jobs.
The fix was relying on implicit conversions in the non-taken branch. - Ordinarily (work on a C++20 codebase) I would have used `if constexpr` here, sidestepping the issue, but that's not available on C++11 so I didn't bother. - So instead of an `if` statement, I used a compile-time constant to select the correct overload. - This is arguably better in this case, anyway. I was using function-style casts for typed constants, which I consider superior for constants, but the CI checks disagree, so changed all to `static_cast`. - For some reason, the CI checks didn't point at all of them, so I hope I caught them all myself. Built with clang14 and all unit tests pass.
@nlohmann thanx for the review. I hope I got everything proper this time. |
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.
Thanks a lot! |
My pleasure. This library is a neat piece of work, thanks! |
This fixes issue #4236.
The fix is simple. In the relevant
to_json
overload, we just need to select the correct json type depending on the signedness of the enum's underlying type.The unit-udt test is also updated to cover this corner case in a way that fails without the fix.
Note that amalgamating the source was a bit of a story for me:
make pretty
but that modified 20 files, performing a significant amount of indentation changes, none of them related to my change.make amalgamate
, but that did nothing. Apparently, the make rule won't run if the single_include files have already been updated bymake pretty
. This makes sense but it can be confusing.make amalgamate
to do the work by touching the file that had the fix.I just am not conversant enough in Linux to know whether I installed astyle correctly (had to clone the source from a beta branch and build, in order to get support for
--squeeze-lines
). I decided to just let more accustomed contributors deal with this if needed.Pull request checklist
Read the Contribution Guidelines for detailed information.
include/nlohmann
directory, runmake amalgamate
to create the single-header filessingle_include/nlohmann/json.hpp
andsingle_include/nlohmann/json_fwd.hpp
. The whole process is described here.Please don't
#ifdef
s or other means.