Go-to-elm-json - Elm JSON codec generator for Go backends

I wrote a Go tool to generate Elm JSON codecs from Go struct definitions. It’s not feature complete, but I am finding it useful already. Feedback welcomed!

for example, it will take this Go:

package foo

type UserJSON struct {
	Name    string   `json:"name"`
	UserID  int      `json:"userID"`
	Friends []string `json:"friends"`
	Enabled bool     `json:"enabled"`
}

and output this Elm:

module User exposing (User, decoder, encode)

import Json.Decode as D
import Json.Decode.Pipeline as P
import Json.Encode as E


type alias User =
    { name : String
    , userId : Int
    , friends : List String
    , enabled : Bool
    }


decoder : D.Decoder User
decoder =
    D.succeed User
        |> P.required "name" D.string
        |> P.required "userID" D.int
        |> P.required "friends" (D.list D.string)
        |> P.required "enabled" D.bool


encode : User -> E.Value
encode r =
    E.object
        [ ("name", E.string r.name)
        , ("userID", E.int r.userId)
        , ("friends", (E.list E.string) r.friends)
        , ("enabled", E.bool r.enabled)
        ]

A more complex example with nested structs from another project:

Go:

Generated Elm:

6 Likes

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.