GithubHelp home page GithubHelp logo

eeue56 / json-to-elm Goto Github PK

View Code? Open in Web Editor NEW
278.0 9.0 23.0 92 KB

Create Elm type aliases and decoders based on JSON input

Home Page: http://json2elm.com

License: BSD 3-Clause "New" or "Revised" License

Python 16.79% JavaScript 10.51% Elm 72.17% CSS 0.48% Shell 0.05%

json-to-elm's Introduction

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"

json-to-elm's People

Contributors

andys8 avatar eeue56 avatar ento avatar joneshf avatar knewter avatar mgold avatar tmcw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

json-to-elm's Issues

Complex Type

I am trying to generate the elm decoder for my jsonresume.

I get the type "ComplexType", which is not defined. What does it mean? Is this JSON to complex for the tool, or is this a bug?

This is the json in question: (resulting elm below)

{
  "basics": {
    "name": "Georg Hartmann",
    "label": "Software Engineer",
    "phone": "",
    "website": "",
    "summary": "",
    "location": {
      "address": "",
      "postalCode": "",
      "city": "",
      "countryCode": "",
      "region": ""
    },
    "profiles": []
  },
  "work": [
    {
      "company": "Q1 Energie AG",
      "position": "Software Engineer",
      "website": "",
      "startDate": "2016-01-01",
      "summary": "zahlz Mobile Payments, Backend, Golang, Go, RESTful Services, Grafana, Monitoring, Docker, Technical Consulting",
      "highlights": [],
      "endDate": "2017-01-01"
    },
    {
      "company": "Georg Hartmann IT-Dienstleistungen",
      "position": "Freiberuflicher Softwareentwickler",
      "website": "",
      "startDate": "2015-04-01",
      "summary": "Design und Implementierung von Backend Services for Mobile Payment in Golang, Technisches Consulting, Web-Entwicklung.",
      "highlights": [],
      "endDate": "2015-12-01"
    },
    {
      "company": "High Performance Computing Laboratory, PUCRS",
      "position": "Praktikant",
      "website": "",
      "startDate": "2014-09-01",
      "summary": "3 Monate Praktikum, 3 Monate schreiben der Bachelorarbeit auf Englisch mit dem Thema: \nUsage Statistics for the clusters with Node.js and MongoDB",
      "highlights": [],
      "endDate": "2015-02-01"
    },
    {
      "company": "Hochschule Osnabrueck (University of Applied Sciences)",
      "position": "Tutor für Mathematik",
      "website": "",
      "startDate": "2014-03-01",
      "summary": "",
      "highlights": [],
      "endDate": "2014-07-01"
    },
    {
      "company": "Recifesites",
      "position": "Praktikant",
      "website": "",
      "startDate": "2012-08-01",
      "summary": "Frontend web development mit HTML, CSS und JS (jQuery)",
      "highlights": [],
      "endDate": "2012-09-01"
    },
    {
      "company": "Grundschule Matancitas",
      "position": "Freiwilliger",
      "website": "",
      "startDate": "2011-03-01",
      "summary": "Freiwilliger in Vollzeit im Rahmen eines Freiwilligen Sozialen Jahres (FSJ)",
      "highlights": [],
      "endDate": "2011-09-01"
    },
    {
      "company": "Barraca da Amizade, Social Project",
      "position": "Freiwilliger",
      "website": "",
      "startDate": "2010-07-01",
      "summary": "Freiwilliger in Vollzeit im Rahmen eines Freiwilligen Sozialen Jahres (FSJ).\nLeitung eines Recycingworkshops",
      "highlights": [],
      "endDate": "2011-02-01"
    }
  ],
  "education": [
    {
      "institution": "Freie Waldorfschule Balingen",
      "area": "",
      "studyType": "Abitur",
      "startDate": "1997-01-01",
      "gpa": "",
      "courses": [],
      "endDate": "2010-01-01"
    },
    {
      "institution": "Hochschule Osnabrück",
      "area": "",
      "studyType": "Bachelor of Science (BS)",
      "startDate": "2012-01-01",
      "gpa": "",
      "courses": [],
      "endDate": "2015-01-01"
    }
  ],
  "skills": [
    {
      "name": "CI",
      "level": "",
      "keywords": []
    },
    {
      "name": "Elm",
      "level": "",
      "keywords": []
    },
    {
      "name": "Web Development",
      "level": "",
      "keywords": []
    },
    {
      "name": "Cascading Style Sheets (CSS)",
      "level": "",
      "keywords": []
    },
    {
      "name": "jQuery",
      "level": "",
      "keywords": []
    },
    {
      "name": "Microsoft Office",
      "level": "",
      "keywords": []
    },
    {
      "name": "MongoDB",
      "level": "",
      "keywords": []
    },
    {
      "name": "Golang",
      "level": "",
      "keywords": []
    },
    {
      "name": "Java",
      "level": "",
      "keywords": []
    },
    {
      "name": "C++",
      "level": "",
      "keywords": []
    },
    {
      "name": "Node.js",
      "level": "",
      "keywords": []
    },
    {
      "name": "JavaScript",
      "level": "",
      "keywords": []
    },
    {
      "name": "HTML 5",
      "level": "",
      "keywords": []
    },
    {
      "name": "Linux",
      "level": "",
      "keywords": []
    },
    {
      "name": "Git",
      "level": "",
      "keywords": []
    },
    {
      "name": "Software Development",
      "level": "",
      "keywords": []
    },
    {
      "name": "MySQL",
      "level": "",
      "keywords": []
    },
    {
      "name": "Representational State Transfer (REST)",
      "level": "",
      "keywords": []
    },
    {
      "name": "Monitoring",
      "level": "",
      "keywords": []
    }
  ],
  "languages": [
    {
      "language": "Spanish",
      "fluency": "Limited working"
    },
    {
      "language": "Portuguese",
      "fluency": "Full professional"
    },
    {
      "language": "French",
      "fluency": "Elementary"
    },
    {
      "language": "German",
      "fluency": "Native or bilingual"
    },
    {
      "language": "English",
      "fluency": "Full professional"
    }
  ]
}

This is the result:

import Json.Encode
import Json.Decode exposing (field)

type alias Something =
    { basics : SomethingBasics
    , work : List ComplexType
    , education : List ComplexType
    , skills : List ComplexType
    , languages : List ComplexType
    }

type alias SomethingBasicsLocation =
    { address : String
    , postalCode : String
    , city : String
    , countryCode : String
    , region : String
    }

type alias SomethingBasics =
    { name : String
    , label : String
    , phone : String
    , website : String
    , summary : String
    , location : SomethingBasicsLocation
    , profiles : List ComplexType
    }

decodeSomething : Json.Decode.Decoder Something
decodeSomething =
    Json.Decode.map5 Something
        (field "basics" decodeSomethingBasics)
        (field "work" Json.Decode.list decodeComplexType)
        (field "education" Json.Decode.list decodeComplexType)
        (field "skills" Json.Decode.list decodeComplexType)
        (field "languages" Json.Decode.list decodeComplexType)

decodeSomethingBasicsLocation : Json.Decode.Decoder SomethingBasicsLocation
decodeSomethingBasicsLocation =
    Json.Decode.map5 SomethingBasicsLocation
        (field "address" Json.Decode.string)
        (field "postalCode" Json.Decode.string)
        (field "city" Json.Decode.string)
        (field "countryCode" Json.Decode.string)
        (field "region" Json.Decode.string)

decodeSomethingBasics : Json.Decode.Decoder SomethingBasics
decodeSomethingBasics =
    Json.Decode.Pipeline.decode SomethingBasics
        |> Json.Decode.Pipeline.required "name" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "label" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "phone" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "website" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "summary" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "location" (decodeSomethingBasicsLocation)
        |> Json.Decode.Pipeline.required "profiles" (Json.Decode.list decodeComplexType)

encodeSomething : Something -> Json.Encode.Value
encodeSomething record =
    Json.Encode.object
        [ ("basics",  encodeSomethingBasics <| record.basics)
        , ("work",  Json.Encode.list <| List.map encodeComplexType <| record.work)
        , ("education",  Json.Encode.list <| List.map encodeComplexType <| record.education)
        , ("skills",  Json.Encode.list <| List.map encodeComplexType <| record.skills)
        , ("languages",  Json.Encode.list <| List.map encodeComplexType <| record.languages)
        ]

encodeSomethingBasicsLocation : SomethingBasicsLocation -> Json.Encode.Value
encodeSomethingBasicsLocation record =
    Json.Encode.object
        [ ("address",  Json.Encode.string <| record.address)
        , ("postalCode",  Json.Encode.string <| record.postalCode)
        , ("city",  Json.Encode.string <| record.city)
        , ("countryCode",  Json.Encode.string <| record.countryCode)
        , ("region",  Json.Encode.string <| record.region)
        ]

encodeSomethingBasics : SomethingBasics -> Json.Encode.Value
encodeSomethingBasics record =
    Json.Encode.object
        [ ("name",  Json.Encode.string <| record.name)
        , ("label",  Json.Encode.string <| record.label)
        , ("phone",  Json.Encode.string <| record.phone)
        , ("website",  Json.Encode.string <| record.website)
        , ("summary",  Json.Encode.string <| record.summary)
        , ("location",  encodeSomethingBasicsLocation <| record.location)
        , ("profiles",  Json.Encode.list <| List.map encodeComplexType <| record.profiles)
        ]

Should be runnable through the cli

generate <json as string> --name <some name> should run print_everything on the json as a string where some name is the name for the toplevel type alias

camel casing is not maintained in encoder and decoder:

eg.

{
  "name" : "Aaron",
  "coolFactor" : 11
}
type alias Aaron =
    { name : String
    , coolFactor : IntType
    }

decodeAaron : Json.Decode.Decoder Aaron
decodeAaron =
    Json.Decode.succeed Aaron
        |: ("name" := Json.Decode.string)
        |: ("coolfactor" := decodeIntType)

encodeAaron : Aaron -> Json.Encode.Value
encodeAaron record =
    Json.Encode.object
        [ ("name",  Json.Encode.string record.name)
        , ("coolfactor",  encodeIntType record.coolfactor)
        ]

Note on encoder:
coolfactor instead of coolFactor

(I may make a pr this weekend)

Top-level alias and extra aliases may override one another

For both Elm and Python version. This might not happen often (or at all) in practice, but it puzzled me a bit when playing with the online demo.

  • Elm:
    • toplevel alias name: Foo
    • json: {"foo": {"foo": {"foo": "deeply nested"}}}
type alias Foo =
    { foo : Foo
    }

type alias Foo =
    { foo : Foo
    }

type alias Foo =
    { foo : String
    }
  • Python: generating from a foo.json with content {"foo": {"foo": {"foo": "deeply nested"}}} gives:
type alias Foo =
    { foo : String
    }


type alias Foo =
    { foo : Foo
    }


type alias Foo =
    { foo : Foo
    }
  • Note the fact in readme
  • or raise if there is a type alias with the same name but with a different definition
  • or rename automatically

Elm 0.19

Any plans to update for Elm 0.19?

Elm package installer typos

elm-package install -- yes noredink/elm-decode-pipeline is what's in there right now. There's an extra space before yes, and NRI needs to have proper capitalization. Just wanted to report it!

Broken by Grammerly plugin for chrome

While this is totally not your fault, I have Grammarly installed and it really messes up the website. I'm not sure if there's a lot you could do to fix that. I'll at least leave it here for posterity's sake.

Weird gibberish inlined

Given the input file Common.elm:

module Common exposing (..)

type alias Id =
    Int

type alias EmpireId =
    Id

type alias Empire =
    { id : EmpireId
    , name : String
    }

Running

python generate.py Common.elm

yields

decodeId =    Inttype alias EmpireId =    Idtype alias Empire : Json.Decode.Decoder Id =    Inttype alias EmpireId =    Idtype alias Empire
decodeId =    Inttype alias EmpireId =    Idtype alias Empire =
    Json.Decode.succeed Id =    Inttype alias EmpireId =    Idtype alias Empire
        |: ("id" := decodeEmpireid)
        |: ("name" := Json.Decode.string)
encodeId =    Inttype alias EmpireId =    Idtype alias Empire : Id =    Inttype alias EmpireId =    Idtype alias Empire -> Json.Encode.Value
encodeId =    Inttype alias EmpireId =    Idtype alias Empire record =
    Json.Encode.object
        [ ("id", encodeEmpireid record.id)
        , ("name", Json.Encode.string record.name)
        ]

Not sure if I'm doing something wrong o_O

Deploy on Github Pages

Super useful tool 👍 Would be nice if the latest version is always accessible online.

type field should compile to "type_"

When generating code for a JSON struct that has a "type" member, the Elm code won't be happy due to reserved words being found in the generated code.
It'd be nice to issue at least a warning when generating the code. Better even to rename to something that actually compiles.

Cli tool compiles to 0.17 and doesn't include imports

When trying to compile a json file with the following command

python json-to-elm/generate.py src/shared/person.json > src/client/decoders/Person.elm

It generates the following:

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

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

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

decodePersonLocation : Json.Decode.Decoder PersonLocation
decodePersonLocation =
    Json.Decode.succeed PersonLocation
        |: ("name" := Json.Decode.string)
        |: ("days" := Json.Decode.int)

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

encodePersonLocation : PersonLocation -> Json.Encode.Value
encodePersonLocation record =
    Json.Encode.object
        [ ("name",  Json.Encode.string <| record.name)
        , ("days",  Json.Encode.int <| record.days)
        ]

whereas I would expect it to compile to the latest version as follows:

import Json.Encode
import Json.Decode
import Json.Decode.Pipeline

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

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

decodePerson : Json.Decode.Decoder Person
decodePerson =
    Json.Decode.Pipeline.decode Person
        |> Json.Decode.Pipeline.required "name" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "age" (Json.Decode.int)
        |> Json.Decode.Pipeline.required "location" (decodePersonLocation)

decodePersonLocation : Json.Decode.Decoder PersonLocation
decodePersonLocation =
    Json.Decode.Pipeline.decode PersonLocation
        |> Json.Decode.Pipeline.required "name" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "days" (Json.Decode.int)

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

encodePersonLocation : PersonLocation -> Json.Encode.Value
encodePersonLocation record =
    Json.Encode.object
        [ ("name",  Json.Encode.string <| record.name)
        , ("days",  Json.Encode.int <| record.days)
        ]

or at least add options to include imports and which version to compile to.

`original` decoder uses `Json.Decode.Pipeline` without importing it

Thanks for a great service!

This JSON gives some funny results.

Test case

{"cpus":4
   ,"port":8080
   ,"localprefix":"localfolder"
   ,"name":"myjob"
   ,"hash":"edebf0f9966282ea565cf6e3b0cf1eff"
   ,"source-folder":"myfolder"
   ,"load-timestamp":1496932552590
}

Generated Elm code (as of 2017-06-09 from http://eeue56.github.io/json-to-elm/, toplevel alias "Job"):

import Json.Encode
import Json.Decode exposing (field)

type alias Job =
    { cpus : Int
    , port : Int
    , localprefix : String
    , name : String
    , hash : String
    , sourceFolder : String
    , loadTimestamp : Int
    }

decodeJob : Json.Decode.Decoder Job
decodeJob =
    Json.Decode.Pipeline.decode Job
        |> Json.Decode.Pipeline.required "cpus" (Json.Decode.int)
        |> Json.Decode.Pipeline.required "port" (Json.Decode.int)
        |> Json.Decode.Pipeline.required "localprefix" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "name" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "hash" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "sourceFolder" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "loadTimestamp" (Json.Decode.int)

encodeJob : Job -> Json.Encode.Value
encodeJob record =
    Json.Encode.object
        [ ("cpus",  Json.Encode.int <| record.cpus)
        , ("port",  Json.Encode.int <| record.port)
        , ("localprefix",  Json.Encode.string <| record.localprefix)
        , ("name",  Json.Encode.string <| record.name)
        , ("hash",  Json.Encode.string <| record.hash)
        , ("sourceFolder",  Json.Encode.string <| record.sourceFolder)
        , ("loadTimestamp",  Json.Encode.int <| record.loadTimestamp)
        ]

Unexpected things

  1. The decoder uses Json.Decode.Pipeline without importing it
  2. The decoder uses a pipeline even if the setting says original

Walkaround

After adding import Json.Decode.Pipeline to the top of the file and running elm package install NoRedInk/elm-decode-pipeline, code compiles.

unhappy to convert json lists

it seems to be unhappy to transform my nested structure containing a list:

➜ json-to-elm git:(master) ✗>python generate.py form.json 
Generating type alias, decoders and encoders from form.json
Traceback (most recent call last):
  File "generate.py", line 131, in <module>
    main()
  File "generate.py", line 127, in main
    print_everything(f.read(), name)
  File "generate.py", line 55, in print_everything
    aliases = create_type_alias(stuff, type_alias_name=alias_name)
  File "/home/muelli/vcs/json-to-elm/type_alias.py", line 49, in create_type_alias
    for name, value in stuff.items():
AttributeError: 'list' object has no attribute 'items'
➜ json-to-elm git:(master) ✗>

The structure looks like this:

[
    {
        "id": 1,
        "name": "form1",
        "form": {
            "tabs": [
                {
                    "id": 1,
                    "name": "some topic",
                    "fields": [
                        {
                            "key": "02",
                            "name": "q1",
                            "description": "foo?",
                            "is_required": true,
                            "default_value": null,
                            "state": "active"
                        }
                    ]
                }
            ]
        }
    }
]

null runtime exception

Not a big issue, but I noticed if you type null, we get a runtime exception and the page seems to break.
i.e. you can select multiple radio buttons and changing the toplevel alias name didn't do anything.

Union types generate incorrect code

This input

type InterestLevel
    = NotAtAllInteresting
    | NotVeryInteresting
    | Neutral
    | Interesting
    | VeryInteresting

yields

fromStringInterestLevel : InterestLevel -> Result String String
fromStringInterestLevel string = 
    case string of
        "Interesting" -> Result.Ok Interesting
        "Neutral" -> Result.Ok Neutral
        "NotAtAllInteresting" -> Result.Ok NotAtAllInteresting
        "NotVeryInteresting" -> Result.Ok NotVeryInteresting
        "VeryInteresting" -> Result.Ok VeryInteresting
        _ -> Result.Err ("Not valid pattern for decoder to InterestLevel. Pattern: " ++ (toString string))

decodeInterestLevel : Json.Decode.Decoder InterestLevel
decodeInterestLevel =
    Json.Decode.string `Json.Decode.andThen` fromStringInterestLevel

encodeInterestLevel : InterestLevel -> Json.Value
encodeInterestLevel =
 toString >> Json.Encode.string

which incorrectly has the type of fromStringInterestLevel : InterestLevel -> Result String String. It should be fromStringInterestLevel : String -> Result String InterestLevel.

Also, the generated code for decodeInterestLevel should be decodeInterestLevel = Json.Decode.customDecoder Json.Decode.string fromStringInterestLevel

Tuples do not convert correctly

type alias ConvertedModel =
    { pickerMode : Int
    , activeColor : ( Float, Float, Float )
    }

will return

decodeConvertedModel : Json.Decode.Decoder ConvertedModel
decodeConvertedModel =
    Json.Decode.map4 ConvertedModel
        (field "pickerMode" Json.Decode.int)
        (field "activeColor" decode( Json.Decode.float)
        (field "Float" decode_Unknown)
        (field "Float )" decode_Unknown)

Support decoding and encoding Dicts

It would be great if, when fed with a type alias aliasing to a Dict, the tool would produce decoders and encoders that make use of Json.Decode.dict and Json.Encode.object.

Nested Records do not convert correctly.

type alias ConvertedModel =
    { sliderOptions :
        { rgbo : Bool
        , rgbl : Bool
        , hsvo : Bool
        , hsvl : Bool
        }
    }

Converts to

decodeConvertedModel : Json.Decode.Decoder ConvertedModel
decodeConvertedModel =
    Json.Decode.map4 ConvertedModel
        (field "sliderOptions" decode{ decodeRgbo)
        (field "rgbl" Json.Decode.bool)
        (field "hsvo" Json.Decode.bool)
        (field "hsvl" Json.Decode.bool decode decode decode decode decode decode decode decode})

Camel-case-ize properties with spaces

An input of

{ "hi there": true }

Produces a record type of

type alias Something =
    { hi there : Bool
    }

Which is invalid Elm syntax - what will actually be generated is

type alias Something =
    { hiThere : Bool
    }

Happy to contribute a PR, but currently stuck on elm 0.17 compatibility - this package is currently for 0.16.x. Updating to 0.17 in that line of elm-package.json produces a generic

Error: Unable to find a set of packages that will work with your constraints.

So I'm not sure what would be necessary to make the packages fit the constraints.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.