Fix basic_json converting constructor yielding wrong value type and mark constructor JSON_EXPLICIT #3518
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is an attempt to fix #3425.
The desired value type is communicated to the serializer by wrapping the source value in one of
string_type_wrapper
,object_type_wrapper
, orarray_type_wrapper
.It started fairly simple, but the ability to supply a custom serializer means this solution will only work with
nlohmann::adl_serializer
(and requires an indirection to compile with a custom serializer). The wrapper types could be made part of the public API to allow users of custom serializers to handle these conversions as well.Additionally, the PR addresses aspects of #2649 (as discussed here), by making the converting constructor explicit depending on
JSON_USE_IMPLICIT_CONVERSIONS
.The converting
basic_json
constructor is kind of broken and with the current serializer API, I couldn't think of a better way to convey the destination type to the serializer. A well-designed solution should be an item for 4.0.In the meantime, I'm rather uncertain as to how to proceed. At least this PR improves the situation for
adl_serializer
users, which is probably the vast majority of users anyway.@nlohmann @gregmarr I'd appreciate some general feedback on the idea. The code isn't ready for a thorough review.
It also currently fails to build on MSVC due to the serializer's SFINAE template parameter, I'll look into that depending on feedback.