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

Use parameter names to choose the right constructor for the JSON deserialization. #1984

Open
DavyLandman opened this issue Jun 20, 2024 · 0 comments

Comments

@DavyLandman
Copy link
Member

DavyLandman commented Jun 20, 2024

Is your feature request related to a problem? Please describe.
It's related to the discussion in #1861, Say I have this ADT:

data X = y(int a) | z(str b);

I cannot serialize that to json and back, as it looses the constructor name (there is not reasonable place for it).

Describe the solution you'd like
I want to propose we extend the heuristic, to gather the names of the parameters found in json, and based on that, pick the constructor that has those parameters.

We would have to (in the JsonValueReader) calculate the union of all field names of the alternatives for the ADT, and buildup that positional & kw parameters based on that, and only at the end build the positional & kw params maps that goes into the constructor.

Describe alternatives you've considered

Update the documentation to say that an arbitrary constructor will be chose, and you have no control over it. The current documentation is ambiguous, and seems to suggest the names of parameters are considered.

Additional context

Yes, this is not fool proof, but it will make the json deserialization work in more cases for the confused end user.

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

No branches or pull requests

1 participant