I would like to use roc-json to decode some json where an object can represent one of several types, which have different properties. Is there any way to do this? I'd like to write something like this (but this doesn't work):
app "json-test"
packages {
pf: "https://github.com/roc-lang/basic-cli/releases/download/0.7.0/bkGby8jb0tmZYsy2hg1E_B2QrCgcSTxdUlHtETwm5m4.tar.br",
json: "https://github.com/lukewilliamboswell/roc-json/releases/download/0.6.0/hJySbEhJV026DlVCHXGOZNOeoOl7468y9F9Buhj0J18.tar.br",
}
imports [
pf.Stdout,
json.Core.{ json },
Decode.{ fromBytes },
]
provides [main] to pf
jsonStr = Str.toUtf8 "{\"id\": \"http://example.org/A\"}"
Concept : [AtomicConcept { id : Str }, ExistentialRestriction { role : Str }]
result : Result Concept _
result = jsonStr |> Decode.fromBytes json
main =
Stdout.line (Inspect.toStr result)
@Luke Boswell could know this
Unfortunately this isn’t supported right now. The compiler needs to be updated to support decoding into tag unions in general
This is the issue https://github.com/roc-lang/roc/issues/3816
I implemented the first part of it but am not currently working on it
Ah, okay, thanks!
Last updated: Jul 05 2025 at 12:14 UTC