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