partial JSON decoding with defaults

139 views
Skip to first unread message

Peter Damoc

unread,
Jul 6, 2015, 11:54:24 AM7/6/15
to elm-d...@googlegroups.com
Hello kind people, 

Is there a way to decode a piece of JSON to a value with some defaults provided?

e.g. let's say we have the following type and JSON decoder: 

type alias UserData = 
  {name: String
  , avatar_url: String
  , repos_url: String
  , languages: List String
  }

decodeUser : Json.Decoder (UserData)
decodeUser = Json.object3 UserData
    ("name" := Json.string) 
    ("avatar_url" := Json.string)
    ("repos_url" := Json.string)

I want to decode the JSON data into the type BUT the languages will be extracted at a later stage and I want to be able to just provide an empty list to the decoder in order to be able to provide a valid value. 



--
There is NO FATE, we are the creators.
blog: http://damoc.ro/

Joseph Hager

unread,
Jul 6, 2015, 12:05:42 PM7/6/15
to elm-d...@googlegroups.com
You should be able to use http://package.elm-lang.org/packages/elm-lang/core/2.1.0/Json-Decode#succeed

I haven't tested this, but perhaps:

decodeUser = Json.object3 UserData
    ("name" := Json.string) 
    ("avatar_url" := Json.string)
    ("repos_url" := Json.string)
    (succeed [])

Joseph Hager

unread,
Jul 6, 2015, 12:06:33 PM7/6/15
to elm-d...@googlegroups.com
Sorry, needs to be object4...

decodeUser = Json.object4 UserData
    ("name" := Json.string) 
    ("avatar_url" := Json.string)
    ("repos_url" := Json.string)
    (succeed [])

Peter Damoc

unread,
Jul 6, 2015, 12:24:02 PM7/6/15
to elm-d...@googlegroups.com
Hmm... it does not work for me. 

The code I'm using is here:
https://github.com/pdamoc/elmChallenges/blob/master/challenge4.elm
I've tried replacing UserData with User and add succeed but it just gives a type error related to succeed.

Maybe I'm missing something.  


--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Max Goldstein

unread,
Jul 6, 2015, 2:03:02 PM7/6/15
to elm-d...@googlegroups.com
Try this:

decodeUser : Json.Decoder User

decodeUser = Json.object4 User

    ("name" := Json.string)

    ("avatar_url" := Json.string)

    ("repos_url" := Json.string)

    (Json.oneOf

        [ "languages" := Json.list Json.string

        , Json.succeed []

        ])

Peter Damoc

unread,
Jul 7, 2015, 4:36:39 AM7/7/15
to elm-d...@googlegroups.com
Hi Max, 

I tried it at first as Joseph suggested and it failed because I was using just succeed and it was the Task.succeed :) 

Now it works. 

Thank you both, 
Peter


--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages