Created
August 20, 2018 15:40
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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