-
Notifications
You must be signed in to change notification settings - Fork 359
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
Runtime error when decoding Json #624
Comments
The problem is the recursive dependency of To work around the issue currently, you should use |
Something like this should work: decodeTweets : Json.Decoder (List TweetModel)
decodeTweets =
force tweetDetails |> Json.list
tweetDetails : Lazy (Json.Decoder TweetModel)
tweetDetails = lazy <| \_ ->
Json.object3 TweetModel
("id" := Json.float)
("text" := Json.string)
(Json.maybe ("retweeted_status" := force tweetDetails |> Json.map RetweetedStatus)) |
Thanks @jvoigtlaender 👍 |
I used this approach and now I am getting But what I am curious about, I got to this code by following this hint about recursive types/type aliases and if I would be expecting a response Json of shape used in example there (messages that can have response messages and those can have response messages and so on), what would be the working approach there? I would need to use some kind of recursive decoder there like I was doing with the tweets and lazy approach just produces a different runtime error. Thanks for pointing me to the right direction, still new to Elm :) |
The working approach should be the one using type Tree = Leaf Int | Node Tree Tree So this approach is not unworking per se. Why it does not work in your specific case is difficult to diagnose without having an actual http://sscce.org/, including a call with a concrete JSON input that you expect to be able to decode but get the runtime error on. |
Coming back to this since it was pointed out in elm/elm-lang.org#578 that decodeTweets : Json.Decoder (List TweetModel)
decodeTweets =
lazy (\_ -> tweetDetails) |> Json.list
tweetDetails : Json.Decoder TweetModel
tweetDetails =
Json.object3 TweetModel
("id" := Json.float)
("text" := Json.string)
(Json.maybe ("retweeted_status" := lazy (\_ -> tweetDetails) |> Json.map RetweetedStatus))
lazy : (() -> Json.Decoder a) -> Json.Decoder a
lazy thunk =
Json.customDecoder Json.value
(\js -> Json.decodeValue (thunk ()) js) It's possible the |
Thanks 👍 |
I found this via the same path as @patrik-piskay — via working through recursive type aliases advice and then hitting the runtime error. For me using (I leave the note for the future. 🙂) |
Yes, |
I am getting
Uncaught TypeError: Cannot read property 'tag' of undefined
(https://github.com/elm-lang/core/blob/32b8fd9b3f2f1f6ef2c1d3a7914427e7a646770c/src/Native/Json.js#L313) when decoding Json.Repo with minimum code required to replicate the issue with Json response hardcoded here
Main parts:
The issue seems to be related to the
RetweetedStatus
type and and its usage in decoder. Without it, everything works fine.In twitter response, in each tweet record there may or may not be a
retweeted_status
key, hence theJson.Decode.maybe
The text was updated successfully, but these errors were encountered: