Skip to content

Instantly share code, notes, and snippets.

@Warry Warry/MagicJson.elm
Last active May 31, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.