Discussion:
[elm-discuss] Decoding a json property with different types of values
Thiago Temple
2017-11-15 19:14:38 UTC
Permalink
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
--
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.
Aaron VonderHaar
2017-11-17 15:41:32 UTC
Permalink
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.)
Post by Thiago Temple
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.
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
--
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
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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
'Rupert Smith' via Elm Discuss
2017-11-20 10:44:24 UTC
Permalink
Post by Thiago Temple
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.
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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...