alexkorban/json-to-elm/1.0.0

NOTE: the latest version is 1.1.0

ElmCodeGenerator

This package helps generate Elm JSON decoders and encoders from a JSON sample.

Generator function

fromJsonSample : GeneratorOptions -> JsonString -> Result String Output

Generate Elm code from a supplied JSON sample string using the supplied options.

The output consists of:

  • a set of imports
  • a set of type and type alias definitions
  • a set of decoder defintions
  • a set of encoder definitions.
    ElmCodeGenerator.fromJsonSample
        { rootTypeName = topLevelTypeName
        , decodeImport = { importAlias = "Json.Decode", exposingSpec = ElmCodeGenerator.ExposingNone }
        , encodeImport = { importAlias = "Json.Encode", exposingSpec = ElmCodeGenerator.ExposingNone }
        , decoderStyle = ElmCodeGenerator.PlainDecoders
        , namingStyle = ElmCodeGenerator.NounNaming
        }
        "{"a": 1, "b": "str"}"

Types

= ApplicativeDecoders ImportSpec
| PipelineDecoders ImportSpec
| PlainDecoders

Defines the style of decoders to generate:

  • Applicative decoders use andMap from elm-community/json-extra
  • Pipeline decoders use functions from NoRedInk/elm-json-decode-pipeline
  • Plain decoders only use elm/json functions
= ExposingAll
| ExposingNone
| ExposingSome (List String)

Defines what's exposed for an import

type alias GeneratorOptions =
{ decodeImport : ImportSpec
, encodeImport : ImportSpec
, decoderStyle : DecoderStyle
, namingStyle : NamingStyle
, rootTypeName : String
}

Options for code generation given to fromJsonSample

= VerbNaming
| NounNaming

Defines function naming style:

  • Verb naming will produce names like decodeThing
  • Noun naming will produce names like thingDecoder
type alias Output =
{ imports : List String
, types : List String
, decoders : List String
, encoders : List String
}

Output Elm strings produced by fromJsonSample