rascal icon indicating copy to clipboard operation
rascal copied to clipboard

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

Open DavyLandman opened this issue 1 year ago • 0 comments

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.

DavyLandman avatar Jun 20 '24 19:06 DavyLandman