Skip to content

Instantly share code, notes, and snippets.

@noprompt
Created February 3, 2016 07:21
Show Gist options
  • Save noprompt/44a0c9839d57b7a6f1eb to your computer and use it in GitHub Desktop.
Save noprompt/44a0c9839d57b7a6f1eb to your computer and use it in GitHub Desktop.
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
@arian
Copy link

arian commented Oct 23, 2017

Updated for 0.18:

module Main exposing (decodeJson)

import Html exposing (text)
import Array exposing (Array)
import Dict exposing (Dict)
import Json.Decode as Decode exposing (Decoder)


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
        , jsonNull
        ]


json : Decoder JsonValue
json =
    json_ ()


decodeJson : String -> Result String JsonValue
decodeJson =
    Decode.decodeString json

https://ellie-app.com/97hx6N9h6a1/1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment