On Friday, November 17, 2017 at 3:19:55 PM UTC, Thiago Temple wrote: > > I have situation where I have to a jso object similar to this > > { "level": 1, "displayValue": "some text", "dataValue": "a string with > the value" } > > { "level": 1, "displayValue": "some text", "dataValue": { "name": "xxx", > "scope": "xxxx" } } > > Both cases for dataValue are valid, it can either have a string or an > object. > > I have created types as such: > > type DataValue > = Val String > | Key KeyType > > type alias KeyType = > { name: String, source: String } > > type alias MyObj = > { level: Int, displayValue: String, dataValue: DataValue} > > How can I decode dataValue for MyObj? > > Thanks >
I think the Decode.oneOf suggestion from Aaron VonderHaar is most likely to be useful to you. Just to give another way, here is some decoder code for a JSON structure where I also added an explicit @type field to indicate which type it is to be decoded to. This is a little more complex, and in my case required the back-end to play nicely and insert the @type fields too. It does avoid the trial and error approach of 'oneOf' too: {-| Describes the ContentModel view type. -} type ContentModel = TitledAsContentModel Titled | MdContentAsContentModel MdContent | PanelAsContentModel Panel {-| A JSON encoder for the ContentModel type. -} contentModelEncoder : ContentModel -> Encode.Value contentModelEncoder model = case model of TitledAsContentModel titled -> titledEncoder titled MdContentAsContentModel mdContent -> mdContentEncoder mdContent PanelAsContentModel panel -> panelEncoder panel {-| A JSON decoder for the ContentModel type. -} contentModelDecoder : Decoder ContentModel contentModelDecoder = let toContentModel typeName = case typeName of "Titled" -> map TitledAsContentModel titledDecoder "MdContent" -> map MdContentAsContentModel mdContentDecoder "Panel" -> map PanelAsContentModel panelDecoder _ -> Decode.fail ("unknown type: " ++ typeName) in field "@type" Decode.string |> andThen toContentModel -- 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.