Skip to content

Instantly share code, notes, and snippets.

@plcstevens
Created August 20, 2018 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plcstevens/ee68fdd7b87403e489c61656e19c76f8 to your computer and use it in GitHub Desktop.
Save plcstevens/ee68fdd7b87403e489c61656e19c76f8 to your computer and use it in GitHub Desktop.
A basic example of trying to create a schema ignorant JSON decoder in ELM
import Dict exposing (Dict)
import Json.Decode as Decode
type SchemaData
= SchemaObject (Dict String SchemaData)
| SchemaList (List SchemaData)
| SchemaString String
| SchemaInt Int
| SchemaBool Bool
| SchemaNull
decodeSchema : Decode.Decoder SchemaData
decodeSchema =
Decode.oneOf
[ Decode.keyValuePairs (Decode.lazy (\_ -> decodeSchema)) |> Decode.map makeObject
, Decode.list (Decode.lazy (\_ -> decodeSchema)) |> Decode.map makeList
, Decode.string |> Decode.map SchemaString
, Decode.int |> Decode.map SchemaInt
, Decode.bool |> Decode.map SchemaBool
, Decode.null SchemaNull
]
makeList : List SchemaData -> SchemaData
makeList items =
SchemaList items
makeObject : List ( String, SchemaData ) -> SchemaData
makeObject fields =
SchemaObject (Dict.fromList fields)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment