question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make JSON encoding of DAML-LF variants easier to pattern match on

See original GitHub issue

Currently, 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:closed
  • Created 4 years ago
  • Comments:13 (13 by maintainers)

github_iconTop GitHub Comments

2reactions
leo-dacommented, Dec 19, 2019

The following DAML

data Qux =  Quux | Quuz

gets compiled into DAML LF Enum:

JsonEncodingTest:Qux -> Normal(DefDataType(ImmArray.ImmArraySeq(),Enum(ImmArray.ImmArraySeq(Quux, Quuz)))),

We currently encode Enums as JsStrings. I am changing it, to keep it consistent with Variants. So, it will be encoded as:

{"tag": "Quux"}
0reactions
hurryabitcommented, Dec 20, 2019

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found