Stream: beginners

Topic: json decoding null


view this post on Zulip Oskar Hahn (Jul 10 2023 at 20:35):

I try to decode Str.toUtf8 "null" using json. But I don't know in which roc type I can put the decoded value. I though, I could use {} since this means kind of nothing in roc. But it does not seem to fit:

# This fails
expect
    null = Str.toUtf8 "null"
    decoded = fromBytesPartial null json
    decoded.result == Ok {}

I don't have to use json null. I could use another json value to represent "nothing". It seems that the roc value {} will be decoded to the json value {}

# This passes
expect
    v = {}
    got = toBytes v json
    expectV = Str.toUtf8 "{}"
    got == expectV

But if I decode it again, it fails:

# This fails
expect
    null = Str.toUtf8 "{}"
    decoded = fromBytesPartial null json
    decoded.result == Ok {}

I use this json-lib: "https://github.com/lukewilliamboswell/roc-json/releases/download/0.1.0/xbO9bXdHi7E9ja6upN5EJXpDoYm7lwmJ8VzL7a5zhYE.tar.br"

Is this a bug or am I doing something wrong?

view this post on Zulip Luke Boswell (Jul 10 2023 at 21:34):

I think for now that package just decodes a json null into a Roc Str, and there is no way to encode a null from Roc atm. Open to any suggestions or PRs to change it, I just did the first thing to get it working as a first pass.

view this post on Zulip Richard Feldman (Jul 10 2023 at 21:58):

how about [Null, NonNull a]?

view this post on Zulip Notification Bot (Jul 11 2023 at 09:15):

7 messages were moved from this topic to #introductions > introductions by Anton.


Last updated: Jul 05 2025 at 12:14 UTC