Skip to content

Instantly share code, notes, and snippets.

@Warry
Last active May 31, 2019 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Warry/c0ac59dd6747c324cbc3491fda07a768 to your computer and use it in GitHub Desktop.
Save Warry/c0ac59dd6747c324cbc3491fda07a768 to your computer and use it in GitHub Desktop.
unsafe elm json encoder+decoder
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()
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
{
"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