Skip to content

Conversation

@falbrechtskirchinger
Copy link
Contributor

@falbrechtskirchinger falbrechtskirchinger commented Jun 4, 2022

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.

Add wrapper types to encode conversion target value types and to_json
overloads to perform the conversions.

Fixes nlohmann#3425.
Make JSON_USE_IMPLICIT_CONVERSIONS affect the basic_json converting
constructor.
@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 140943b on falbrechtskirchinger:basic_json-conversion-issue3425 into 7a6e28a on nlohmann:develop.

@gregmarr
Copy link
Contributor

gregmarr commented Jun 5, 2022

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Conversion from alt_json to json produces incorrect result

4 participants