Skip to content

Instantly share code, notes, and snippets.

@TwistingTwists
Created March 26, 2019 13:14
Show Gist options
  • Save TwistingTwists/2c9d4d255d012709e73d1e5a48a1e8be to your computer and use it in GitHub Desktop.
Save TwistingTwists/2c9d4d255d012709e73d1e5a48a1e8be to your computer and use it in GitHub Desktop.
testing_decoder_throway
module TestDecoder exposing (Node(..), input, main)
import Browser
import Html exposing (Html, button, div, text)
import Html.Events exposing (onClick)
import Json.Decode as Decode exposing (..)
import Json.Decode.Pipeline exposing (custom, hardcoded, optional, required)
type Node
= Node String (List Node)
main : Html msg
main =
text <| Debug.toString (Decode.decodeString (field "node" (list decodeNode)) input)
-- text <| Debug.toString (Decode.decodeString (Decode.at [ "map", "node" ] (list decodeNode)) input)
decodeNode : Decoder Node
decodeNode =
Decode.map2 Node
(field "@TEXT" string)
nodeChildren
nodeChildren : Decoder (List Node)
nodeChildren =
oneOf
[ field "node" (list (lazy (\_ -> decodeNode)))
, field "node" (map List.singleton (lazy (\_ -> decodeNode)))
, succeed []
]
input =
"""{
"node": [
{
"@TEXT": "1",
"node": [
{
"@TEXT": "1.1"
},
{
"@TEXT": "1.2"
}
]
},
{
"@TEXT": "2",
"node": {
"@TEXT": "2.1"
}
}
]
}
"""
-- this was created here : https://elmlang.slack.com/archives/C192T0Q1E/p1553603706828400
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment