New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json.Decode dislikes recursion #664

Closed
surprisetalk opened this Issue Jul 15, 2016 · 2 comments

Comments

Projects
None yet
3 participants
@surprisetalk

surprisetalk commented Jul 15, 2016

It seems that Json.Decode can't be used recursively.

Consider the following example:

[
  { "whatever": 1
  , "something": 2
  , "things": 
    [  { "whatever": 3
       , "something": 4
       , "things": []
       }
    ,  { "whatever": 5
       , "something": 6
       , "things": []
       }
    ] 
  }
]
decodeThings : Json.Decoder (List Thing)
decodeThings =
  Json.list decodeThing

decodeThing : Json.Decoder Thing
decodeThing =
  Json.object3 Thing
    ("whatever" := Json.int)
    ("something" := Json.int)
    ("things" := decodeThings)

The web console has the following complaint: TypeError: undefined is not an object (evaluating 'decoder.tag').

The error seems to be occurring in runHelp in https://github.com/elm-lang/core/blob/master/src/Native/Json.js.
If you place a console.log( decoder, value ) before switch( decoder.tag ) in a compiled file, you'll see that the parent function doesn't pass a decoder object to the recursed child.

My current workaround (😅) is something like:

decodeThings1 : Json.Decoder (List Thing)
decodeThings1 =
  Json.list decodeThing1

decodeThing1 : Json.Decoder Thing
decodeThing1 =
  Json.object3 Thing
    ("whatever" := Json.int)
    ("something" := Json.int)
    ("things" := decodeThings2)

decodeThings2 : Json.Decoder (List Thing)
decodeThings2 =
  Json.list decodeThing2

decodeThing2 : Json.Decoder Thing
decodeThing2 =
  Json.object2 Thing
    ("whatever" := Json.int)
    ("something" := Json.int)

I'm willing fix this issue, but I would love some guidance on where to look!

Thank you friends.

ts

@process-bot

This comment has been minimized.

Show comment
Hide comment
@process-bot

process-bot Jul 15, 2016

Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it!

Here is what to expect next, and if anyone wants to comment, keep these things in mind.

process-bot commented Jul 15, 2016

Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it!

Here is what to expect next, and if anyone wants to comment, keep these things in mind.

@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

jvoigtlaender Jul 15, 2016

Contributor

This is a known compiler issue. See elm/compiler#873 and the discussion there (explicitly considering JSON decoding in the comments). The current workaround is to make use of lazy from http://package.elm-lang.org/packages/elm-community/json-extra/1.0.0/Json-Decode-Extra#lazy.

Contributor

jvoigtlaender commented Jul 15, 2016

This is a known compiler issue. See elm/compiler#873 and the discussion there (explicitly considering JSON decoding in the comments). The current workaround is to make use of lazy from http://package.elm-lang.org/packages/elm-community/json-extra/1.0.0/Json-Decode-Extra#lazy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment