Decoding a json property with different types of values

215 views
Skip to first unread message

Thiago Temple

unread,
Nov 17, 2017, 10:19:55 AM11/17/17
to Elm Discuss
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

Aaron VonderHaar

unread,
Nov 17, 2017, 10:41:56 AM11/17/17
to elm-d...@googlegroups.com
The simplest approach would be to use Json.Decode.oneOf along with Json.Decode.map:

dataValueDecoder : Json.Decode.Decoder DataValue
dataValueDecoder =
    Json.Decode.oneOf
        [ Json.Decode.string |> Json.Decode.map Val
        , ... decode your KeyType record ...  |> Json.Decode.map Key
        ]

(Note that if you have control over the JSON format, for more complicated cases, it's often best to add a "type" field to your JSON, so you can decode the "type" field first, and then you Json.Decode.andThen to switch off of the type, which would be more performant if you have lots of cases, but in your example probably wouldn't be worth the complexity.)


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

Rupert Smith

unread,
Nov 20, 2017, 5:44:24 AM11/20/17
to Elm Discuss
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





Reply all
Reply to author
Forward
0 new messages