Last active
May 31, 2019 12:39
-
-
Save Warry/c0ac59dd6747c324cbc3491fda07a768 to your computer and use it in GitHub Desktop.
unsafe elm json encoder+decoder
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
const compiler = require("node-elm-compiler") | |
const fs = require("async-file") | |
async function build() { | |
const code = await compiler.compileToString("./src/Main.elm", { output: "main.js" }) | |
const patched = code | |
.replace(`(jsonEncodeElmValue) { | |
return author$project$MagicJson$jsonEncodeFakeValue;`, | |
`(jsonEncodeElmValue) { | |
return jsonEncodeElmValue;`) | |
.replace(`(jsonDecodeJsonValue) { | |
return author$project$MagicJson$jsonDecodeFakeValue;`, | |
`(jsonDecodeJsonValue) { | |
return elm$core$Maybe$Just(jsonDecodeJsonValue);`) | |
return fs.writeFile('main.js', patched) | |
.catch((err) => { console.log("can't write file", err) }) | |
} | |
build() |
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 MagicJson exposing (encode, decode, decoder) | |
import Json.Encode as Encode exposing (Value) | |
import Json.Decode as Decode exposing (Decoder) | |
import Http | |
encode : a -> Value | |
encode jsonEncodeElmValue = | |
jsonEncodeFakeValue | |
decode : Value -> Maybe a | |
decode jsonDecodeJsonValue = | |
jsonDecodeFakeValue | |
decoder : Decoder a | |
decoder = | |
Decode.map decode Decode.value | |
|> Decode.andThen (\d -> | |
case d of | |
Just v -> Decode.succeed v | |
Nothing -> Decode.fail "Impossible decoding of json" | |
) | |
jsonDecodeFakeValue : Maybe a | |
jsonDecodeFakeValue = | |
Nothing | |
jsonEncodeFakeValue : Value | |
jsonEncodeFakeValue = | |
Encode.null |
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
{ | |
"scripts": { | |
"build": "node build.js" | |
}, | |
"dependencies": { | |
"async-file": "^2.0.2", | |
"node-elm-compiler": "^5.0.3" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment