-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
json.gleam
305 lines (276 loc) Β· 6.89 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import gleam/list
import gleam/result
import gleam/bit_array
import gleam/option.{type Option, None, Some}
import gleam/dynamic.{type Dynamic}
import gleam/string_builder.{type StringBuilder}
pub 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("[", dynamic.list(of: dynamic.int))
/// Error(UnexpectedEndOfInput)
/// ```
///
/// ```gleam
/// > decode("1", dynamic.string)
/// Error(UnexpectedFormat([dynamic.DecodeError("String", "Int", [])]))
/// ```
///
pub fn decode(
from json: String,
using decoder: dynamic.Decoder(t),
) -> Result(t, DecodeError) {
do_decode(from: json, using: decoder)
}
@target(erlang)
fn do_decode(
from json: String,
using decoder: dynamic.Decoder(t),
) -> Result(t, DecodeError) {
let bits = bit_array.from_string(json)
decode_bits(bits, decoder)
}
@target(javascript)
fn do_decode(
from json: String,
using decoder: dynamic.Decoder(t),
) -> Result(t, DecodeError) {
use dynamic_value <- result.then(decode_string(json))
decoder(dynamic_value)
|> result.map_error(UnexpectedFormat)
}
@target(javascript)
@external(javascript, "../gleam_json_ffi.mjs", "decode")
fn decode_string(a: String) -> Result(Dynamic, DecodeError)
/// 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(<<"[">>, dynamic.list(of: dynamic.int))
/// Error(UnexpectedEndOfInput)
/// ```
///
/// ```gleam
/// > decode_bits("<<1">>, dynamic.string)
/// Error(UnexpectedFormat([dynamic.DecodeError("String", "Int", [])]))
/// ```
///
pub fn decode_bits(
from json: BitArray,
using decoder: dynamic.Decoder(t),
) -> Result(t, DecodeError) {
use dynamic_value <- result.then(decode_to_dynamic(json))
decoder(dynamic_value)
|> result.map_error(UnexpectedFormat)
}
@target(erlang)
@external(erlang, "gleam_json_ffi", "decode")
fn decode_to_dynamic(a: BitArray) -> Result(Dynamic, DecodeError)
@target(javascript)
fn decode_to_dynamic(json: BitArray) -> Result(Dynamic, DecodeError) {
case bit_array.to_string(json) {
Ok(string) -> decode_string(string)
Error(Nil) -> Error(UnexpectedByte("", 0))
}
}
/// 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 fn to_string(json: Json) -> String {
do_to_string(json)
}
@external(erlang, "gleam_json_ffi", "json_to_string")
@external(javascript, "../gleam_json_ffi.mjs", "json_to_string")
fn do_to_string(a: Json) -> 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 fn to_string_builder(json: Json) -> StringBuilder {
do_to_string_builder(json)
}
@external(erlang, "gleam_json_ffi", "json_to_iodata")
@external(javascript, "../gleam_json_ffi.mjs", "json_to_string")
fn do_to_string_builder(a: Json) -> StringBuilder
/// Encode a string into JSON, using normal JSON escaping.
///
/// ## Examples
///
/// ```gleam
/// > to_string(string("Hello!"))
/// "\"Hello!\""
/// ```
///
pub fn string(input: String) -> Json {
do_string(input)
}
@external(erlang, "gleam_json_ffi", "string")
@external(javascript, "../gleam_json_ffi.mjs", "identity")
fn do_string(a: String) -> Json
/// Encode a bool into JSON.
///
/// ## Examples
///
/// ```gleam
/// > to_string(bool(False))
/// "false"
/// ```
///
pub fn bool(input: Bool) -> Json {
do_bool(input)
}
@external(erlang, "gleam_json_ffi", "bool")
@external(javascript, "../gleam_json_ffi.mjs", "identity")
fn do_bool(a: Bool) -> Json
/// Encode an int into JSON.
///
/// ## Examples
///
/// ```gleam
/// > to_string(int(50))
/// "50"
/// ```
///
pub fn int(input: Int) -> Json {
do_int(input)
}
@external(erlang, "gleam_json_ffi", "int")
@external(javascript, "../gleam_json_ffi.mjs", "identity")
fn do_int(a: Int) -> Json
/// Encode a float into JSON.
///
/// ## Examples
///
/// ```gleam
/// > to_string(float(4.7))
/// "4.7"
/// ```
///
pub fn float(input: Float) -> Json {
do_float(input)
}
@external(erlang, "gleam_json_ffi", "float")
@external(javascript, "../gleam_json_ffi.mjs", "identity")
fn do_float(input input: Float) -> Json
/// The JSON value null.
///
/// ## Examples
///
/// ```gleam
/// > to_string(null())
/// "null"
/// ```
///
pub fn null() -> Json {
do_null()
}
@external(erlang, "gleam_json_ffi", "null")
@external(javascript, "../gleam_json_ffi.mjs", "do_null")
fn do_null() -> Json
/// Encode an optional value into JSON, using null if it is 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 fn object(entries: List(#(String, Json))) -> Json {
do_object(entries)
}
@external(erlang, "gleam_json_ffi", "object")
@external(javascript, "../gleam_json_ffi.mjs", "object")
fn do_object(entries entries: List(#(String, Json))) -> Json
/// 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 fn preprocessed_array(from: List(Json)) -> Json {
do_preprocessed_array(from)
}
@external(erlang, "gleam_json_ffi", "array")
@external(javascript, "../gleam_json_ffi.mjs", "array")
fn do_preprocessed_array(from from: List(Json)) -> Json