Skip to content

Allow setting of fallback values in a json.parse call using annotations #4681

@ymatto

Description

@ymatto

Description

In cases where I want my script to accept external json, I may want to use json.parse not only only with annotations using optional types (as is currently supported), but set fallback values that I want for my own script execution. Having to do this outside of the json.parse statement is a bit laborious and potentially redundant.

Preferable solution

It would be preferable to have some way to specify fallback values directly in the annotations for the json.parse call so I can, in one place, fully describe my expected json structure as well as what values to use if the input fails to fit that structure.

Alternative solutions

Currently I can do things record-by-record like this:

let json.parse x = my_json_file
an_int = x.foo ?? 0

However, this becomes laborious when the json structure is more complex.

Also, using annotations in the json.parse call is redundant, like this:

let json.parse ( x : { foo : int? }? ) = my_json_file
an_int = x.foo ?? 0

This allows any json to be used as input and my fallback value will be used if anything is wrong with the input json such that my expected record isn't present (or is the wrong type). However I essentially have to specify my expected type structure twice.

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions