Skip to content

Commit a2ba0e1

Browse files
Adds an example to README
1 parent d1d0c35 commit a2ba0e1

File tree

1 file changed

+158
-1
lines changed

1 file changed

+158
-1
lines changed

README.md

+158-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ This project requires that you already have [elixir](http://elixir-lang.org/)
99
and its build tool `mix` installed, this can be done with `brew install elixir`
1010
or similar.
1111

12-
- Clone this repository: `git clone [email protected]:dragonwasrobot/json-schema-to-elm.git`
12+
- Clone this repository: `git clone
13+
[email protected]:dragonwasrobot/json-schema-to-elm.git`
1314
- Build an executable: `MIX_ENV=prod mix build`
1415
- An executable, `js2e`, has now been created in your current working directory.
1516

@@ -22,3 +23,159 @@ works is still in progress, but feel free to take a look at the `examples`
2223
folder which contains an example of a pair of JSON schemas and their
2324
corresponding Elm output. Likewise, representations of each of the different
2425
JSON schema types are described in the `lib/types` folder.
26+
27+
## Example
28+
29+
If we supply `js2e` with the following JSON schema file, `definitions.json`:
30+
``` json
31+
{
32+
"$schema": "http://json-schema.org/draft-04/schema",
33+
"title": "Definitions",
34+
"id": "http://example.com/definitions.json",
35+
"description": "Schema for common types",
36+
"definitions": {
37+
"color": {
38+
"id": "#color",
39+
"type": "string",
40+
"enum": [ "red", "yellow", "green", "blue" ]
41+
},
42+
"point": {
43+
"id": "#point",
44+
"type": "object",
45+
"properties": {
46+
"x": {
47+
"type": "number"
48+
},
49+
"y": {
50+
"type": "number"
51+
}
52+
},
53+
"required": [ "x", "y" ]
54+
}
55+
}
56+
}
57+
```
58+
59+
it produces the following Elm file, `Domain/Definitions.elm`:
60+
61+
``` elm
62+
module Domain.Definitions exposing (..)
63+
64+
-- Schema for common types
65+
66+
import Json.Decode as Decode
67+
exposing
68+
( float
69+
, int
70+
, string
71+
, list
72+
, succeed
73+
, fail
74+
, map
75+
, maybe
76+
, field
77+
, at
78+
, andThen
79+
, oneOf
80+
, nullable
81+
, Decoder
82+
)
83+
import Json.Decode.Pipeline
84+
exposing
85+
( decode
86+
, required
87+
, optional
88+
, custom
89+
)
90+
import Json.Encode as Encode
91+
exposing
92+
( Value
93+
, float
94+
, int
95+
, string
96+
, list
97+
, object
98+
)
99+
100+
101+
type Color
102+
= Red
103+
| Yellow
104+
| Green
105+
| Blue
106+
107+
108+
type alias Point =
109+
{ x : Float
110+
, y : Float
111+
}
112+
113+
114+
colorDecoder : String -> Decoder Color
115+
colorDecoder color =
116+
case color of
117+
"red" ->
118+
succeed Red
119+
120+
"yellow" ->
121+
succeed Yellow
122+
123+
"green" ->
124+
succeed Green
125+
126+
"blue" ->
127+
succeed Blue
128+
129+
_ ->
130+
fail <| "Unknown color type: " ++ color
131+
132+
133+
pointDecoder : Decoder Point
134+
pointDecoder =
135+
decode Point
136+
|> required "x" Decode.float
137+
|> required "y" Decode.float
138+
139+
140+
encodeColor : Color -> Value
141+
encodeColor color =
142+
case color of
143+
Red ->
144+
Encode.string "red"
145+
146+
Yellow ->
147+
Encode.string "yellow"
148+
149+
Green ->
150+
Encode.string "green"
151+
152+
Blue ->
153+
Encode.string "blue"
154+
155+
156+
encodePoint : Point -> Value
157+
encodePoint point =
158+
let
159+
x =
160+
[ ( "x", Encode.float point.x ) ]
161+
162+
y =
163+
[ ( "y", Encode.float point.y ) ]
164+
in
165+
object <|
166+
x
167+
++ y
168+
```
169+
170+
which contains an Elm type for the `color` and `point` definitions along with
171+
their corresponding JSON decoders and encoders.
172+
173+
Furthermore, if we supply `js2e` with another JSON schema file that references
174+
any of the definitions in `definitions.json`, e.g.
175+
176+
``` json
177+
{ "$ref" : "http://example.com/definitions.json#point" }
178+
```
179+
180+
then the corresponding Elm file output will import the definitions `Point`,
181+
`pointDecoder` and `encodePoint` from `Domain.Definitions`.

0 commit comments

Comments
 (0)