Great! Thanks a lot.
And if we already stared talking about decoders I have one last (absolutely
academic) question.
Is is possible to read a JSON which is array of objects? Like [ {}, {}, {}
]? In my example I have { "shows" : [ {}, {}, {} ] } and I'm catching a
field by name but I'm wondering if it is possible to capture root array
node.
On Wednesday, November 16, 2016 at 10:16:12 PM UTC+1, Peter Damoc wrote:
>
>
> On Wed, Nov 16, 2016 at 10:50 PM, Tim Bezhashvyly <[email protected]
> <javascript:>> wrote:
>
>> Makes sense but doesn't change much:
>>
>> (|>) is expecting the right side to be a: Http.Request Shows -> a But
>>> the right side is: Http.Request (Maybe Shows) -> Cmd Msg
>>
>>
> Ok, now we're cooking.
>
> You have a Json decoder that produces Shows and it needs to produce Maybe
> Shows.
>
> If you use Just instead of identity you should be fine:
>
> decoderColl : Json.Decoder (Maybe Shows)
> decoderColl =
> Json.map Just
> (field "shows" (Json.list decoder))
>
>
>
> Just to clarify a little bit more around this. If you throw bad JSON at a
> decoder you will get a BadPayload Http.Error as the result of the request.
> If you want to capture the failure of the decoder within the decoder you
> need something like this:
>
> decoderColl : Json.Decoder (Maybe Shows)
> decoderColl =
> Json.maybe (field "shows" (Json.list decoder))
>
> This second decoder will not throw a BadPayload because it will be able to
> decode every bad JSON to Nothing.
> This is useful in some ways but BadPayload errors are more informative.
>
>
>
>
> --
> There is NO FATE, we are the creators.
> blog: http://damoc.ro/
>
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.