Skip to content
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 basic_json converting constructor yielding wrong value type and mark constructor JSON_EXPLICIT #3518

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from

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