forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJs_json.resi
277 lines (220 loc) · 7.79 KB
/
Js_json.resi
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
/* Copyright (C) 2015-2016 Bloomberg Finance L.P.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/***
Efficient JSON encoding using JavaScript API
**see** [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON)
*/
/* ## Types */
@unboxed /** The JSON data structure */
type rec t =
| Boolean(bool)
| @as(null) Null
| String(string)
| Number(float)
| Object(dict<t>)
| Array(array<t>)
module Kind: {
type json = t
/** Underlying type of a JSON value */
type rec t<_> =
| String: t<Js_string.t>
| Number: t<float>
| Object: t<dict<json>>
| Array: t<array<json>>
| Boolean: t<bool>
| Null: t<Js_types.null_val>
}
type tagged_t =
| JSONFalse
| JSONTrue
| JSONNull
| JSONString(string)
| JSONNumber(float)
| JSONObject(dict<t>)
| JSONArray(array<t>)
/* ## Accessors */
let classify: t => tagged_t
/**
`test(v, kind)` returns `true` if `v` is of `kind`.
*/
let test: ('a, Kind.t<'b>) => bool
/**
`decodeString(json)` returns `Some(s)` if `json` is a `string`, `None` otherwise.
*/
let decodeString: t => option<Js_string.t>
/**
`decodeNumber(json)` returns `Some(n)` if `json` is a `number`, `None` otherwise.
*/
let decodeNumber: t => option<float>
/**
`decodeObject(json)` returns `Some(o)` if `json` is an `object`, `None` otherwise.
*/
let decodeObject: t => option<dict<t>>
/**
`decodeArray(json)` returns `Some(a)` if `json` is an `array`, `None` otherwise.
*/
let decodeArray: t => option<array<t>>
/**
`decodeBoolean(json)` returns `Some(b)` if `json` is a `boolean`, `None` otherwise.
*/
let decodeBoolean: t => option<bool>
/**
`decodeNull(json)` returns `Some(null)` if `json` is a `null`, `None` otherwise.
*/
let decodeNull: t => option<Js_null.t<'a>>
/* ## Constructors */
/*
Those functions allows the construction of an arbitrary complex
JSON values.
*/
@val /** `null` is the singleton null JSON value. */
external null: t = "null"
/** `string(s)` makes a JSON string of the `string` `s`. */
external string: string => t = "%identity"
/** `number(n)` makes a JSON number of the `float` `n`. */
external number: float => t = "%identity"
/** `boolean(b)` makes a JSON boolean of the `bool` `b`. */
external boolean: bool => t = "%identity"
/** `object_(dict)` makes a JSON object of the `dict`. */
external object_: dict<t> => t = "%identity"
/** `array_(a)` makes a JSON array of the `Js.Json.t` array `a`. */
external array: array<t> => t = "%identity"
/*
The functions below are specialized for specific array type which
happened to be already JSON object in the ReScript runtime. Therefore
they are more efficient (constant time rather than linear conversion).
*/
/** `stringArray(a)` makes a JSON array of the `string` array `a`. */
external stringArray: array<string> => t = "%identity"
/** `numberArray(a)` makes a JSON array of the `float` array `a`. */
external numberArray: array<float> => t = "%identity"
/** `booleanArray(a)` makes a JSON array of the `bool` array `a`. */
external booleanArray: array<bool> => t = "%identity"
/** `objectArray(a) makes a JSON array of the `JsDict.t` array `a`. */
external objectArray: array<dict<t>> => t = "%identity"
/* ## String conversion */
@val
@scope("JSON")
/**
`parseExn(s)` parses the `string` `s` into a JSON data structure.
Returns a JSON data structure.
Raises `SyntaxError` if the given string is not a valid JSON. Note: `SyntaxError` is a JavaScript exception.
See [`parse`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse) on MDN.
## Examples
```rescript
/* parse a simple JSON string */
let json = try Js.Json.parseExn(` "hello" `) catch {
| _ => failwith("Error parsing JSON string")
}
switch Js.Json.classify(json) {
| Js.Json.JSONString(value) => Js.log(value)
| _ => failwith("Expected a string")
}
```
```rescript
/* parse a complex JSON string */
let getIds = s => {
let json = try Js.Json.parseExn(s) catch {
| _ => failwith("Error parsing JSON string")
}
switch Js.Json.classify(json) {
| Js.Json.JSONObject(value) =>
/* In this branch, compiler infer value : Js.Json.t dict */
switch Js.Dict.get(value, "ids") {
| Some(ids) =>
switch Js.Json.classify(ids) {
| Js.Json.JSONArray(ids) => /* In this branch compiler infer ids : Js.Json.t array */
ids
| _ => failwith("Expected an array")
}
| None => failwith("Expected an `ids` property")
}
| _ => failwith("Expected an object")
}
}
/* prints `1, 2, 3` */
Js.log(getIds(` { "ids" : [1, 2, 3 ] } `))
```
*/
external parseExn: string => t = "parse"
@val
@scope("JSON")
/**
`stringify(json)` formats the JSON data structure as a `string`.
Returns the string representation of a given JSON data structure.
See [`stringify`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) on MDN.
## Examples
```rescript
/* Creates and stringifies a simple JS object */
let dict = Js.Dict.empty()
Js.Dict.set(dict, "name", Js.Json.string("John Doe"))
Js.Dict.set(dict, "age", Js.Json.number(30.0))
Js.Dict.set(dict, "likes", Js.Json.stringArray(["ReScript", "ocaml", "js"]))
Js.log(Js.Json.stringify(Js.Json.object_(dict)))
```
*/
external stringify: t => string = "stringify"
@val
@scope("JSON")
/**
`stringifyWithSpace(json)` formats the JSON data structure as a `string`.
Returns the string representation of a given JSON data structure with spacing.
See [`stringify`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) on MDN.
## Examples
```rescript
/* Creates and stringifies a simple JS object with spacing */
let dict = Js.Dict.empty()
Js.Dict.set(dict, "name", Js.Json.string("John Doe"))
Js.Dict.set(dict, "age", Js.Json.number(30.0))
Js.Dict.set(dict, "likes", Js.Json.stringArray(["ReScript", "ocaml", "js"]))
Js.log(Js.Json.stringifyWithSpace(Js.Json.object_(dict), 2))
```
*/
external stringifyWithSpace: (t, @as(json`null`) _, int) => string = "stringify"
@val
@scope("JSON")
/**
`stringifyAny(value)` formats any value into a JSON string.
## Examples
```rescript
/* prints `["hello", "world"]` */
Js.log(Js.Json.stringifyAny(["hello", "world"]))
```
*/
external stringifyAny: 'a => option<string> = "stringify"
/**
Best-effort serialization, it tries to seralize as
many objects as possible and deserialize it back
It is unsafe in two aspects
- It may throw during parsing
- when you cast it to a specific type, it may have a type mismatch
*/
let deserializeUnsafe: string => 'a
/**
It will raise in such situations:
- The object can not be serlialized to a JSON
- There are cycles
- Some JS engines can not stringify deeply nested json objects
*/
let serializeExn: 'a => string