Stream: beginners

Topic: decode alternative types in roc-json


view this post on Zulip Jim Balhoff (Dec 22 2023 at 13:51):

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)

view this post on Zulip Anton (Dec 22 2023 at 14:07):

@Luke Boswell could know this

view this post on Zulip Isaac Van Doren (Dec 22 2023 at 14:51):

Unfortunately this isn’t supported right now. The compiler needs to be updated to support decoding into tag unions in general

view this post on Zulip Isaac Van Doren (Dec 22 2023 at 14:52):

This is the issue https://github.com/roc-lang/roc/issues/3816

view this post on Zulip Isaac Van Doren (Dec 22 2023 at 14:53):

I implemented the first part of it but am not currently working on it

view this post on Zulip Jim Balhoff (Dec 22 2023 at 14:57):

Ah, okay, thanks!


Last updated: Jul 05 2025 at 12:14 UTC