Created
February 3, 2016 07:21
-
-
Save noprompt/44a0c9839d57b7a6f1eb to your computer and use it in GitHub Desktop.
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
module Json.Decode.Generic where | |
import Json.Decode as Decode exposing (Decoder, (:=)) | |
import Array exposing (Array) | |
import Dict exposing (Dict) | |
type JsonValue = JsonNull | |
| JsonBool Bool | |
| JsonString String | |
| JsonFloat Float | |
| JsonInt Int | |
| JsonArray (Array JsonValue) | |
| JsonObject (Dict String JsonValue) | |
jsonNull : Decoder JsonValue | |
jsonNull = Decode.null JsonNull | |
jsonBool : Decoder JsonValue | |
jsonBool = Decode.map JsonBool Decode.bool | |
jsonString : Decoder JsonValue | |
jsonString = Decode.map JsonString Decode.string | |
jsonInt : Decoder JsonValue | |
jsonInt = Decode.map JsonInt Decode.int | |
jsonFloat : Decoder JsonValue | |
jsonFloat = Decode.map JsonFloat Decode.float | |
jsonArray' : () -> Decoder JsonValue | |
jsonArray' _ = | |
Decode.succeed () | |
`Decode.andThen` | |
(json' >> Decode.array >> Decode.map JsonArray) | |
jsonObject' : () -> Decoder JsonValue | |
jsonObject' _ = | |
Decode.succeed () | |
`Decode.andThen` | |
(json' >> Decode.dict >> Decode.map JsonObject) | |
json' : () -> Decoder JsonValue | |
json' _ = | |
Decode.oneOf [ jsonArray' () | |
, jsonObject' () | |
, jsonString | |
, jsonInt | |
, jsonFloat | |
, jsonBool | |
] | |
json : Decoder JsonValue | |
json = json' () | |
decodeJson : String -> Result String JsonValue | |
decodeJson = | |
Decode.decodeString json |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated for 0.18:
https://ellie-app.com/97hx6N9h6a1/1