Actually you need width, height and radius to be numbers not strings.
Otherwise you'll get parsing errors.
{ "tag": "rectangle", "width": 2, "height": 3}
{ "tag": "circle", "radius": 2}

On Wed, May 18, 2016 at 10:36 PM Mario Sangiorgio <mariosangior...@gmail.com>
wrote:

> No, the json explicitly has a tag attribute to let your code know what is
> the shape type it encodes followed by the attributes for that shape.
>
> Something like:
> { "tag": "rectangle", "width": "2", "height": "3"}
> { "tag": "circle", "radius": "2"}
>
> Note that there is no nesting.
>
> Mario
>
> On Wed, May 18, 2016 at 10:29 PM Eelco Hoekema <ehoek...@gmail.com> wrote:
>
>> Hi,
>>
>> I'm a bit stuck in decoding some json. In
>> http://package.elm-lang.org/packages/elm-lang/core/4.0.0/Json-Decode#andThen
>> there is an example of decoding a tagged union for a shape.
>>
>> type Shape
>>     = Rectangle Float Float
>>     | Circle Float
>> shape : Decoder Shapeshape =
>>   ("tag" := string) `andThen` shapeInfo
>> shapeInfo : String -> Decoder ShapeshapeInfo tag =
>>   case tag of
>>     "rectangle" ->
>>         object2 Rectangle
>>           ("width" := float)
>>           ("height" := float)
>>
>>     "circle" ->
>>         object1 Circle
>>           ("radius" := float)
>>
>>     _ ->
>>         fail (tag ++ " is not a recognized tag for shapes")
>>
>>
>> How would the corresponding JSON look like? Should that be
>>
>> {"rectangle" : {"width" : "2", "height": "3"}}
>>
>>
>>
>> eelco
>>
>> --
>> 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+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>>
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to