json
json copied to clipboard
Fix basic_json converting constructor yielding wrong value type and mark constructor JSON_EXPLICIT
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
, or array_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.
Coverage remained the same at 100.0% when pulling 140943b18e46be9c051f3c004dad8b70c9f6d312 on falbrechtskirchinger:basic_json-conversion-issue3425 into 7a6e28acfaeb52ba322116e188cb2b7ce1603dc2 on nlohmann:develop.
@nlohmann Do we have a timeframe for a major version where we can address some of these issues that are being help up by backwards compatibility requirements? Is it soon enough that we should just fix this properly instead of trying to fit it in within the current API?