Make JSON encoding of DAML-LF variants easier to pattern match on
See original GitHub issueCurrently, values of DAML-LF variant types like
variant Foo =
| Bar Int
| Baz Foo.Baz
record Foo.Baz = {baz : Text}
get encoded to JSON values like
{ "Bar": 5 }
{ "Baz": {"baz": "text"} }
“Pattern matching” on this representation in JavaScript/TypeScript is a bit inconvenient since you have to build a chain of if
-statements and add a final unreachable else
branch.
I suggest we rather follow the approach described in the TypeScript handbook, i.e., represent the same values as
{ "type": "Bar", "data": "Int" }
{ "type": "Baz", "data": {"baz": "text"} }
I’m also open to inlining the record in the second case, i.e.
{ "type": "Baz", "baz": "text" }
but I don’t care too much.
Issue Analytics
- State:
- Created 4 years ago
- Comments:13 (13 by maintainers)
Top Results From Across the Web
A beginner's guide to JSON, the data format for the internet
JSON.parse(string) takes a string of valid JSON and returns a JavaScript object. For example, it can be called on the body of an...
Read more >Parsing JSON into variant based on the data shape
parse for the data on the message event, I get an object with no TAG. This causes pattern matching on the result doesn't...
Read more >Using JSON in Go: A guide with examples - LogRocket Blog
JSON is one of the most-used formats for storing data. In this guide, learn how to work with JSON in Go using the...
Read more >Which JSON content type do I use? - Stack Overflow
The MIME media type for JSON text is application/json . The default encoding is UTF-8. For JSONP (runnable JavaScript) with callback: application/javascript.
Read more >Provide guidance on how to format the affected/notaffected ...
Short answer: it requires encoding the entire set (or at least actively maintained set) of versions as a directed acyclic graph. Pattern ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The following DAML
gets compiled into DAML LF Enum:
We currently encode Enums as JsStrings. I am changing it, to keep it consistent with Variants. So, it will be encoded as:
@leo-da Please keep enums as strings. That seems to be idiomatic JavaScript/TypeScript. There’s no problem with enums and variants being different. That was pretty much why we introduced enums in the first place.