• Stars
    star
    279
  • Rank 147,967 (Top 3 %)
  • Language
    Elm
  • License
    BSD 3-Clause "New...
  • Created almost 9 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Create Elm type aliases and decoders based on JSON input

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"

More Repositories

1

take-home

A take-home application server written in Elm and only Elm
Elm
359
star
2

derw

An Elm-inspired language that transpiles to TypeScript
TypeScript
330
star
3

servelm

Server-side Elm
Elm
117
star
4

elm-blogger

A blogging platform written in Elm + Elixir
Elixir
102
star
5

elm-server-side-renderer

Render elm on the server
Elm
99
star
6

elm-static-html

Statically render html from Elm!
JavaScript
89
star
7

elm-static-site

Static site generator using Elm
JavaScript
87
star
8

elm-phoenix

Phoenix helpers for Elm
Elm
74
star
9

elm-html-test

Test elm-html in Elm!
Elm
68
star
10

haskell-to-elm

68
star
11

elm-for-web-developers

A collection of tips for people using Elm from a web-dev background
56
star
12

elm-static-html-lib

TypeScript
56
star
13

elm-ffi

An FFI interface for Elm
Elm
50
star
14

elmxir

Interop tools for working with Elm in Elixir
Elixir
46
star
15

elm-all-dict

A dict for Elm which can store any type
Elm
37
star
16

elm-bash-completion

Bash completion for Elm
Shell
27
star
17

elm-init-scripts

Python
25
star
18

elm-fuse

Fuse support in Elm
Elm
24
star
19

elm-sketch-importer

Generate Elm code from Sketch files!
Elm
22
star
20

elm-proper-install

Properly install things from Github for Elm
JavaScript
16
star
21

make-your-own-tea

A step-by-step guide to making your own Elm architecture in TypeScript
Shell
15
star
22

coed

Elm-style library for writing modern frontends
TypeScript
15
star
23

neuro-lingo

A language where you only implement comments
TypeScript
13
star
24

elm-flat-matrix

Flat matrix implementation for Elm
Elm
12
star
25

elm-html-in-elm

Elm
12
star
26

elm-alternative-json

An alternative to core's JSON decoder
Elm
12
star
27

elm-xml

xml parser for elm
Elm
11
star
28

hiraeth

Shell
10
star
29

elm-lazy

Elm
10
star
30

elm-run-worker

Run Elm files in worker mode from the CLI
JavaScript
8
star
31

elm-dom-tests

Test dom things
JavaScript
8
star
32

elm-flasked

Elm
8
star
33

sass-to-elm

Elm
7
star
34

slack-today-i-did

Python
7
star
35

meta-elm

Examples of doing meta programming Elm, which you shouldn't do
Elm
7
star
36

elm-http-server

A HTTP server for Elm
Elm
7
star
37

elm-debug-json-view

A package for debugging a decoders with Json in Elm
Elm
6
star
38

stalk

stack-based language written in Elm
Elm
6
star
39

relm

Use react alongside elm
JavaScript
6
star
40

elm-help

JavaScript
6
star
41

elm-debug-decoders

A tool for debugging decoders
JavaScript
6
star
42

elm-package-for-production

Python
5
star
43

pycho

A game engine with inbuilt AI support written for Python 2 and 3.
Python
5
star
44

elm-node-elm-compiler

Elm compiler via Node
Elm
5
star
45

idris-hangman

Example of hangman in Idris
Idris
5
star
46

adeilad

Ensure JSON has the correct structure at runtime
TypeScript
4
star
47

elm-html-query

Elm
4
star
48

the-elm-report

A presentation on the state of Elm
JavaScript
4
star
49

elm-http-error-view

A generic view component
Elm
4
star
50

post-install-elm

Use NPM for managing elm deps
JavaScript
4
star
51

broken-clock

A broken clock used as examples for Elm
Elm
4
star
52

elm-macros

Python
3
star
53

elm-phoenixed

HTML
3
star
54

drawea

Elm
3
star
55

PyChat.js

A chat system built around graceful-websockets and tornado
JavaScript
3
star
56

elm-json-field-value

Elm
3
star
57

BigSister

Extendable IRC bot framework
Python
3
star
58

elm-test-runner

Python
3
star
59

elm-debounce-effects

An effect manager for dealing with debouncing
Elm
3
star
60

elm-query

JQuery for Elm
Elm
3
star
61

elm-diff

Elm
3
star
62

elm-stringify

DEPRECATED: no use
JavaScript
3
star
63

elm-game-of-games

Game of life in Elm
Elm
3
star
64

elm-shrink

Elm
2
star
65

elm-to-elixir

Elm
2
star
66

react-to-elm

Convert React components to Elm
2
star
67

elm-lint

Elm
2
star
68

fedora-matlab

Web challenge
2
star
69

PyGeo2

A project to continue maintaining PyGeo and bring it up to date
2
star
70

ts-core

Some core data structures and functions for typescript
TypeScript
2
star
71

code-golf

collection of code-golfs solutions from our code-golf club
Python
2
star
72

elm-lazy-list

Elm
2
star
73

talks

Various talks I've done
JavaScript
2
star
74

advent-of-code-2016

Idris
2
star
75

caching-layer-docker

Docker files for the elm caching layer
Shell
2
star
76

elm-default-dict

A dict implementation which allows you to provide a default value to use
Elm
1
star
77

SimpleGames

Simple games from Atari implemented in various languages.
Python
1
star
78

realrpg

Python
1
star
79

the-new-turing-omnibus

Elm
1
star
80

elm-uno

An Uno-to-x compiler written in Elm
Elm
1
star
81

mountain-path

Elm
1
star
82

ovas-2

C++
1
star
83

ts-list-zipper

TypeScript
1
star
84

relearn-yourself-a-java

A set of questions used to reteach Java to students.
Java
1
star
85

css-reactor

Elm
1
star
86

elm-safe-regex

JavaScript
1
star
87

Fuze

Elm
1
star
88

NetlogoSims

Simulations written using Netlogo
NetLogo
1
star
89

bach

TypeScript
1
star
90

newer-paper

Elm
1
star
91

ProcessingVisualizations

Python
1
star
92

elm-full-stack

1
star
93

baner

Flag parsing library in Typescript
TypeScript
1
star
94

fontbomber

A clone of https://github.com/code-curve/font-bomb, but written in Python
Python
1
star
95

SourceUploader

Simple uploader for files to gist
1
star
96

mainc

A simple, small benchmarking library for Node
TypeScript
1
star
97

LudumDare28

A game written for LudumDare28
Python
1
star
98

lets-write-elm

Elm
1
star
99

ArdLib

Arduino libraries and general programs with diagrams
1
star
100

elm-graphql-debug-errors

Elm
1
star