alexkorban / json-to-elm / 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:

    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


type DecoderStyle
    = ApplicativeDecoders ImportSpec
    | PipelineDecoders ImportSpec
    | PlainDecoders

Defines the style of decoders to generate:


type ExposingSpec
    = 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


type alias ImportSpec =
{ importAlias : String
, exposingSpec : ExposingSpec 
}

Details of an import


type NamingStyle
    = VerbNaming
    | NounNaming

Defines function naming style:


type alias Output =
{ imports : List String
, types : List String
, decoders : List String
, encoders : List String 
}

Output Elm strings produced by fromJsonSample