json-to-elm

Create Elm type aliases and decoders based on JSON input

  • 所有者: eeue56/json-to-elm
  • 平台:
  • 许可证: BSD 3-Clause "New" or "Revised" License
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

json-to-elm

Create Elm type aliases and decoders based on JSON input

This project allows you to automate the creation of:

  • type aliases from JSON data
  • decoders from type aliases and some union types
  • encoders from type aliases and some union types

You can run this program as a command line tool:

Noahs-MacBook-Pro:json-to-elm noah$ python generate.py ../elm-static-site/examples/Users.elm
decodeUser : Decoder User
decodeUser =
    succeed User, : ("name" := string), : ("location" := string), : ("age" := int)
encodeUser : User -> Json.Encode.Value
encodeUser record =
    object
        [ ("name", string record.name)
        , ("location", string record.location)
        , ("age", int record.age)
        ]

You can also give it a json file.

or you can use the following functions:

print_everything takes in a string containing a JSON object, and a top-level name for the alias it will generate.

It will then recursively generate aliases and decoders for all the JSON objects in the json object.

create_type_alias takes a json object, and returns a list of type aliases needed for that object

create_decoder takes a type alias as a string, along with a prefix to use for custom decoders, and if the encoded fields have snakecase or not, and generates just the decoder for that type alias

create_encoder takes a type alias as a string, along with a prefix to use for custom encoders, and if the encoded fields have snakecase or not, and generates just the encoder for that type alias

create_union_type_decoder takes a union type definition as a string, and will generate the decoder needed if the json value is a string

create_union_type_encoder takes a union type definition as a string, and will generate the encoder needed if the json value is a string

Example:


print_everything(
"""
 { "name" : "Noah"
 , "age" : 23
 , "location" :
    { "name" : "sweden"
    , "days" : 45
    }
 }
"""
    , alias_name = "Person")

generates


type alias Location =
    { name : String
    , days : Int
    }


type alias Person =
    { age : Int
    , name : String
    , location : Location
    }

decodeLocation : Json.Decode.Decoder Location
decodeLocation =
    Json.Decode.succeed Location, : ("name" := Json.Decode.string), : ("days" := Json.Decode.int)

decodePerson : Json.Decode.Decoder Person
decodePerson =
    Json.Decode.succeed Person, : ("age" := Json.Decode.int), : ("name" := Json.Decode.string), : ("location" := decodeLocation)

encodeLocation : Location -> Json.Encode.Value
encodeLocation record =
    Json.Encode.object
        [ ("name", Json.Decode.string record.name)
        , ("days", Json.Decode.int record.days)
        ]

encodePerson : Person -> Json.Encode.Value
encodePerson record =
    Json.Encode.object
        [ ("age", Json.Decode.int record.age)
        , ("name", Json.Decode.string record.name)
        , ("location", encodeLocation record.location)
        ]

for more examples of this, see the test function

Union types

print(create_union_type_decoder('type Suit = Hearts, Diamonds, Spades, Clubs'))
print(create_union_type_encoder('type Suit = Hearts, Diamonds, Spades, Clubs'))

will print

decodeSuit : Json.Decode.Decoder Suit
decodeSuit =
    let
        decodeToType string =
            case string of
                "Hearts" -> Result.Ok Hearts
                "Diamonds" -> Result.Ok Diamonds
                "Spades" -> Result.Ok Spades
                "Clubs" -> Result.Ok Clubs
                _ -> Result.Err ("Not valid pattern for decoder to Suit. Pattern: " ++ (toString string))
    in
        Json.Decode.customDecoder Json.Decode.string decodeToType

encodeSuit : Suit -> Json.Encode.Value
encodeSuit item =
    case item of
        Hearts -> Json.Encode.string "Hearts"
        Diamonds -> Json.Encode.string "Diamonds"
        Spades -> Json.Encode.string "Spades"
        Clubs -> Json.Encode.string "Clubs"

主要指标

概览
名称与所有者eeue56/json-to-elm
主编程语言Elm
编程语言Python (语言数: 5)
平台
许可证BSD 3-Clause "New" or "Revised" License
所有者活动
创建于2016-02-21 19:36:17
推送于2018-04-13 21:42:53
最后一次提交2017-10-30 12:36:25
发布数0
用户参与
星数277
关注者数8
派生数23
提交数78
已启用问题?
问题数25
打开的问题数13
拉请求数8
打开的拉请求数3
关闭的拉请求数1
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?