/
json.gleam
230 lines (214 loc) Β· 4.97 KB
/
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import gleam/map
import gleam/list
import gleam/result
import gleam/bit_string
import gleam/option.{None, Option, Some}
import gleam/dynamic.{Dynamic}
import gleam/string_builder.{StringBuilder}
pub external type Json
pub type DecodeError {
UnexpectedEndOfInput
UnexpectedByte(byte: String, position: Int)
UnexpectedSequence(byte: String, position: Int)
UnexpectedFormat(List(dynamic.DecodeError))
}
/// Decode a JSON string into dynamically typed data which can be decoded into
/// typed data with the `gleam/dynamic` module.
///
/// ## Examples
///
/// ```gleam
/// > decode("[1,2,3]", dynamic.list(of: dynamic.int))
/// Ok([1, 2, 3])
/// ```
///
/// ```gleam
/// > decode("[", into: dynamic.list(of: dynamic.int))
/// Error(UnexpectedEndOfInput)
/// ```
///
/// ```gleam
/// > decode("1", into: dynamic.string)
/// Error(UnexpectedFormat([dynamic.DecodeError("String", "Int", [])]))
/// ```
///
pub fn decode(
from json: String,
using decoder: dynamic.Decoder(t),
) -> Result(t, DecodeError) {
let bits = bit_string.from_string(json)
decode_bits(bits, decoder)
}
/// Decode a JSON bit string into dynamically typed data which can be decoded
/// into typed data with the `gleam/dynamic` module.
///
/// ## Examples
///
/// ```gleam
/// > decode_bits(<<"[1,2,3]">>, dynamic.list(of: dynamic.int))
/// Ok([1, 2, 3])
/// ```
///
/// ```gleam
/// > decode_bits(<<"[">>, into: dynamic.list(of: dynamic.int))
/// Error(UnexpectedEndOfInput)
/// ```
///
/// ```gleam
/// > decode_bits("<<1">>, into: dynamic.string)
/// Error(UnexpectedFormat([dynamic.DecodeError("String", "Int", [])]))
/// ```
///
pub fn decode_bits(
from json: BitString,
using decoder: dynamic.Decoder(t),
) -> Result(t, DecodeError) {
try dynamic_value = decode_to_dynamic(json)
decoder(dynamic_value)
|> result.map_error(UnexpectedFormat)
}
external fn decode_to_dynamic(BitString) -> Result(Dynamic, DecodeError) =
"gleam_json_ffi" "decode"
/// Convert a JSON value into a string.
///
/// Where possible prefer the `to_string_builder` function as it is faster than
/// this function, and BEAM VM IO is optimised for sending `StringBuilder` data.
///
/// ## Examples
///
/// ```gleam
/// > to_string(array([1, 2, 3], of: int))
/// "[1,2,3]"
/// ```
///
pub external fn to_string(Json) -> String =
"gleam_json_ffi" "json_to_string"
/// Convert a JSON value into a string builder.
///
/// Where possible prefer this function to the `to_string` function as it is
/// slower than this function, and BEAM VM IO is optimised for sending
/// `StringBuilder` data.
///
/// ## Examples
///
/// ```gleam
/// > to_string_builder(array([1, 2, 3], of: int))
/// string_builder.from_string("[1,2,3]")
/// ```
///
pub external fn to_string_builder(Json) -> StringBuilder =
"gleam_json_ffi" "json_to_iodata"
/// Encode a string into JSON, using normal JSON escaping.
///
/// ## Examples
///
/// ```gleam
/// > to_string("Hello!")
/// "\"Hello!\""
/// ```
///
pub external fn string(input: String) -> Json =
"gleam_json_ffi" "string"
/// Encode a bool into JSON.
///
/// ## Examples
///
/// ```gleam
/// > to_string(bool(False))
/// "false"
/// ```
///
pub external fn bool(input: Bool) -> Json =
"gleam_json_ffi" "bool"
/// Encode an int into JSON.
///
/// ## Examples
///
/// ```gleam
/// > to_string(int(50))
/// "50"
/// ```
///
pub external fn int(input: Int) -> Json =
"gleam_json_ffi" "int"
/// Encode an float into JSON.
///
/// ## Examples
///
/// ```gleam
/// > to_string(float(4.7))
/// "4.7"
/// ```
///
pub external fn float(input: Float) -> Json =
"gleam_json_ffi" "float"
/// The JSON value null.
///
/// ## Examples
///
/// ```gleam
/// > to_string(null())
/// "null"
/// ```
///
pub external fn null() -> Json =
"gleam_json_ffi" "null"
/// Encode an optional value into JSON, using null if it the `None` variant.
///
/// ## Examples
///
/// ```gleam
/// > to_string(nullable(Some(50), of: int))
/// "50"
/// ```
///
/// ```gleam
/// > to_string(nullable(None, of: int))
/// "null"
/// ```
///
pub fn nullable(from input: Option(a), of inner_type: fn(a) -> Json) -> Json {
case input {
Some(value) -> inner_type(value)
None -> null()
}
}
/// Encode a list of key-value pairs into a JSON object.
///
/// ## Examples
///
/// ```gleam
/// > to_string(object([
/// #("game", string("Pac-Man")),
/// #("score", int(3333360)),
/// ]))
/// "{\"game\":\"Pac-Mac\",\"score\":3333360}"
/// ```
///
pub external fn object(entries: List(#(String, Json))) -> Json =
"gleam_json_ffi" "object"
/// Encode a list into a JSON array.
///
/// ## Examples
///
/// ```gleam
/// > to_string(array([1, 2, 3], of: int))
/// "[1, 2, 3]"
/// ```
///
pub fn array(from entries: List(a), of inner_type: fn(a) -> Json) -> Json {
entries
|> list.map(inner_type)
|> preprocessed_array
}
/// Encode a list of JSON values into a JSON array.
///
/// ## Examples
///
/// ```gleam
/// > to_string(preprocessed_array([int(1), float(2.0), string("3")]))
/// "[1, 2.0, \"3\"]"
/// ```
///
pub external fn preprocessed_array(from: List(Json)) -> Json =
"gleam_json_ffi" "array"