Skip to content

Commit 5858cf9

Browse files
committed
Rendering JSON
1 parent 685c564 commit 5858cf9

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ Examples showing how to do many things in Gleam!
1616

1717
## Formats
1818

19+
- [Rendering JSON](./universal/test/formats/rendering_json.gleam)
1920
- [Rendering XML](./universal/test/formats/rendering_xml.gleam)

universal/gleam.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ version = "1.0.0"
1616
gleam_stdlib = ">= 0.34.0 and < 2.0.0"
1717
simplifile = ">= 2.1.0 and < 3.0.0"
1818
xmb = ">= 1.0.0 and < 2.0.0"
19+
gleam_json = ">= 1.0.1 and < 2.0.0"
1920

2021
[dev-dependencies]
2122
gleeunit = ">= 1.0.0 and < 2.0.0"

universal/manifest.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33

44
packages = [
55
{ name = "filepath", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "EFB6FF65C98B2A16378ABC3EE2B14124168C0CE5201553DE652E2644DCFDB594" },
6+
{ name = "gleam_json", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "9063D14D25406326C0255BDA0021541E797D8A7A12573D849462CAFED459F6EB" },
67
{ name = "gleam_stdlib", version = "0.40.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "86606B75A600BBD05E539EB59FABC6E307EEEA7B1E5865AFB6D980A93BCB2181" },
78
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
89
{ name = "simplifile", version = "2.1.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "BDD04F5D31D6D34E2EDFAEF0B68A6297AEC939888C3BFCE61133DE13857F6DA2" },
10+
{ name = "thoas", version = "1.2.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "E38697EDFFD6E91BD12CEA41B155115282630075C2A727E7A6B2947F5408B86A" },
911
{ name = "xmb", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "xmb", source = "hex", outer_checksum = "99425CD67BA2AF3E4A38515FB4BFCC121007B69444C81DE6EB837421290F14B4" },
1012
]
1113

1214
[requirements]
15+
gleam_json = { version = ">= 1.0.1 and < 2.0.0" }
1316
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
1417
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
1518
simplifile = { version = ">= 2.1.0 and < 3.0.0" }
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
//// # Generate JSON
2+
////
3+
//// The gleam_json package can be used to generate JSON. It works on any
4+
//// target.
5+
////
6+
//// ## Dependencies
7+
////
8+
//// - https://hex.pm/packages/gleam_json
9+
10+
import gleam/json
11+
import gleam/string
12+
import gleeunit/should
13+
14+
pub fn main_test() {
15+
// The `gleam/json` module has a function for each different type of data in
16+
// a JSON document. e.g. `bool`, `string`, `int`, etc.
17+
//
18+
// The `preprocessed_array` function is used for arrays made from list
19+
// that already contain JSON data.
20+
//
21+
let document =
22+
json.object([
23+
#(
24+
"bookstore",
25+
json.preprocessed_array([
26+
json.object([
27+
#("genre", json.string("Technology")),
28+
#("title", json.string("Introduction to JSON")),
29+
#("author", json.string("Kwame Nkrumah")),
30+
]),
31+
json.object([
32+
#("genre", json.string("Programming")),
33+
#("title", json.string("Learning Gleam")),
34+
#("author", json.string("Mei Wong")),
35+
]),
36+
]),
37+
),
38+
])
39+
40+
// The document can be converted to a string
41+
document
42+
|> json.to_string
43+
|> should.equal(
44+
"
45+
{
46+
\"bookstore\":[
47+
{
48+
\"genre\":\"Technology\",
49+
\"title\":\"Introduction to JSON\",
50+
\"author\":\"Kwame Nkrumah\"
51+
},
52+
{
53+
\"genre\":\"Programming\",
54+
\"title\":\"Learning Gleam\",
55+
\"author\":\"Mei Wong\"
56+
}
57+
]
58+
}
59+
"
60+
|> string.replace(" ", "")
61+
|> string.replace("\n", ""),
62+
)
63+
64+
// If you have a collection of items and you wish to map over each of them to
65+
// make JSON then you can use the `array` function rather than the
66+
// `preprocessed_array` function.
67+
let directions = ["North", "East", "South", "West"]
68+
json.object([
69+
#(
70+
"directions",
71+
json.array(directions, fn(direction) {
72+
json.object([#("name", json.string(direction))])
73+
}),
74+
),
75+
])
76+
|> json.to_string
77+
|> should.equal(
78+
"
79+
{
80+
\"directions\":[
81+
{\"name\":\"North\"},
82+
{\"name\":\"East\"},
83+
{\"name\":\"South\"},
84+
{\"name\":\"West\"}
85+
]
86+
}
87+
"
88+
|> string.replace(" ", "")
89+
|> string.replace("\n", ""),
90+
)
91+
}

0 commit comments

Comments
 (0)