Last active
March 19, 2019 17:04
-
-
Save TwistingTwists/3849dcf8f41e182df47ef0cf58d2280b to your computer and use it in GitHub Desktop.
Decoders in Elm
This file contains 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
[deprecated]. See : https://gist.github.com/TwistingTwists/1bf880bea1300a26966ddccd9a2c7c8e | |
-- using Json.Decoder.Pipeline -- elm install noredink/elm-decode-pipeline | |
{- | |
Please take note that in | |
type Map = Map {version: String, node : Node} | |
is not `List` of `Node` type. It is RootNode. Does the following make more sense? | |
type Map = Map {version: String, root_node : Node} | |
instead of line 6 ? | |
-} | |
module Main exposing (Map(..)) | |
-- import Json.Encode | |
import Element exposing (Color) | |
import Html | |
import Json.Decode as Decode exposing (Decoder, decodeString, float, int, lazy, list, nullable, string) | |
import Json.Decode.Pipeline exposing (hardcoded, optional, required) | |
import Time | |
type Map | |
= Map | |
{ version : String | |
-- root node | |
, node : Node | |
} | |
type Node | |
= Node String NodeAttributes (List Node) | |
type alias NodeAttributes = | |
{ style : Maybe String | |
, folded : Bool | |
, id : Maybe String | |
, created : Maybe Time.Posix | |
, modified : Maybe Time.Posix | |
-- , position : Maybe Position | |
} | |
-- Decoders | |
timestamp : Decoder Time.Posix | |
timestamp = | |
Decode.map Time.millisToPosix Decode.int | |
decodeMindMap : Decoder Map | |
decodeMindMap = | |
Decode.succeed Map | |
|> required "@version" string | |
-- should there be a separate `decodeRootNode` | |
|> required "node" decodeNode | |
decodeNode : Decoder Node | |
decodeNode = | |
let | |
decodeNodeAttributes = | |
Decode.succeed NodeAttributes | |
|> required "@LOCALIZEDSTYLEREF" (nullable string) | |
|> required "@FOLDED" bool | |
|> required "@ID" (nullable string) | |
|> required "@CREATED" (nullable timestamp) | |
|> required "@MODIFIED" (nullable timestamp) | |
in | |
Decode.succeed Node | |
|> required "@TEXT" string | |
|> ???? decodeNodeAttributes | |
|> required "node" (list (lazy (\_ -> decodeNode))) | |
main = | |
Html.text (Debug.toString Node) |
This file contains 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
{ | |
"map": { | |
"@version": "freeplane 1.7.0", | |
"node": | |
{ | |
"@TEXT": "UPSC", | |
"@LOCALIZED_STYLE_REF": "default", | |
"@FOLDED": "false", | |
"@ID": "ID_523629448", | |
"@CREATED": "1487759666321", | |
"@MODIFIED": "1489498611522", | |
"node": [ | |
{ | |
"@TEXT": "TIME", | |
"@ID": "ID_414266408", | |
"@CREATED": "1490340755453", | |
"@MODIFIED": "1490340755929" | |
}, | |
{ | |
"@TEXT": "CLASSES", | |
"@ID": "ID_1943867701", | |
"@CREATED": "1490340779136", | |
"@MODIFIED": "1490340918221" | |
}, | |
{ | |
"@TEXT": "Magnify", | |
"@ID": "ID_1818534457", | |
"@CREATED": "1490340772926", | |
"@MODIFIED": "1490341014628" | |
} | |
] | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment