Skip to content

Sync Belt / Js doc headers #5361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 69 commits into from
Jun 22, 2022
Merged
Changes from 1 commit
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
d901677
Sync doc headers for belt.ml
ryyppy Feb 23, 2022
4609b2e
Sync doc headers for belt_Array.mli
ryyppy Feb 23, 2022
abb4d81
Update belt_Float.mli doc headers
ryyppy Feb 23, 2022
1728a0c
Sync docs for belt_HashMap.mli
ryyppy Feb 23, 2022
beb4478
Sync docs for belt_HashMapInt.mli
ryyppy Jun 6, 2022
2e994cb
Sync docs for belt_HashMapString.mli
ryyppy Jun 8, 2022
dc95a16
Sync belt_HashSet.mli
ryyppy Jun 8, 2022
f835e37
Update belt_HashMapInt
ryyppy Jun 8, 2022
3bed2cc
Sync belt_HashSetString.mli
ryyppy Jun 8, 2022
3270426
Sync docs for belt_Int.mli
ryyppy Jun 8, 2022
becaf84
Sync docs for belt_List.mli
ryyppy Jun 8, 2022
646081f
Sync docs for belt_Option.mli
nkrkv Jun 10, 2022
362d3a3
Adapt docs in belt_Option.mli for parts missing in rescript-lang.org MDX
nkrkv Jun 10, 2022
8035b05
Sync docs for belt_Result.mli
nkrkv Jun 10, 2022
8397ef9
Sync docs for belt_Set.mli
whitchapman Jun 11, 2022
2c0885f
Sync docs for belt_SetInt.mli
whitchapman Jun 11, 2022
9663bdf
Sync docs for belt_SetString.mli
whitchapman Jun 11, 2022
e733a6d
Sync docs for belt_SetDict.mli
whitchapman Jun 11, 2022
da3071e
Sync docs for belt_MutableSetInt.mli
whitchapman Jun 11, 2022
b794d0d
Sync docs for belt_MutableSetString.mli
whitchapman Jun 11, 2022
9d1dd6c
Sync all the docstring texts from sort-array.mdx
jihchi Jun 12, 2022
d4efae0
Merge pull request #5424 from nkrkv/sync-belt-doc-headers-option
ryyppy Jun 13, 2022
6e3063f
Merge pull request #5427 from nkrkv/sync-belt-doc-headers-result
ryyppy Jun 13, 2022
d8515e8
Remove duplication of type definitions
jihchi Jun 13, 2022
0a97557
Sync all the docstring texts from sort-array-int.mdx
jihchi Jun 13, 2022
428d3db
Sync docs for belt_Map.mli
BartSchuurmans Jun 13, 2022
261692a
Sync docs for belt_MapInt.mli
BartSchuurmans Jun 13, 2022
7ca7043
Sync docs for belt_MapString.mli
BartSchuurmans Jun 13, 2022
ad94bc3
Sync docs for belt_MutableMap.mli
BartSchuurmans Jun 13, 2022
9a49a37
Sync docs for belt_MutableMapInt.mli
BartSchuurmans Jun 13, 2022
30dc28d
Sync docs for belt_MutableMapString.mli
BartSchuurmans Jun 13, 2022
ed2aa3a
Remove duplicate type signatures
whitchapman Jun 13, 2022
712a52f
Merge pull request #5428 from whitchapman/sync-belt-doc-headers-set
ryyppy Jun 13, 2022
45bfce0
Sync all the docstring texts from sort-array-string.mdx
jihchi Jun 13, 2022
a8cc5ff
Sync docs for belt_MutableSet.mli
whitchapman Jun 14, 2022
fbd0c82
Merge pull request #5434 from whitchapman/sync-belt-doc-headers-mutab…
ryyppy Jun 14, 2022
4178507
Merge pull request #5432 from jihchi/sync-belt-doc-headers_sortarrays…
ryyppy Jun 14, 2022
2800a93
Merge pull request #5429 from Minnozz/sync-belt-doc-headers-map
ryyppy Jun 14, 2022
ae46303
Merge pull request #5430 from jihchi/sync-belt-doc-headers_sortarray
ryyppy Jun 14, 2022
0b0538a
Merge pull request #5431 from jihchi/sync-belt-doc-headers_sortarrayint
ryyppy Jun 14, 2022
824cc08
Sync docs for belt_Range.mli
ryyppy Jun 14, 2022
62634c8
Sync docs for belt_MutableQueue.mli
ryyppy Jun 14, 2022
d43bf15
Sync docs for belt_MutableStack.mli
ryyppy Jun 14, 2022
0407303
Sync docs for Js.ml
ryyppy Jun 15, 2022
a658afc
Sync docs for js_array.ml
ryyppy Jun 16, 2022
eb294cc
Sync docs for js_array2.ml
ryyppy Jun 16, 2022
a1faf34
Sync docs for js_date.ml
ryyppy Jun 16, 2022
c214954
Sync docs for js_dict.mli
ryyppy Jun 16, 2022
cb2474d
Sync docs for js_exn.mli
ryyppy Jun 16, 2022
7e00a94
Sync docs for js_float.ml
ryyppy Jun 16, 2022
c7caccb
Sync docs for js_math.ml
nkrkv Jun 16, 2022
19d74fd
Sync docs for js_option.ml
nkrkv Jun 16, 2022
8d7850c
Merge pull request #5441 from nkrkv/sync-belt-doc-headers-js-math
ryyppy Jun 18, 2022
9a60245
Merge pull request #5442 from nkrkv/sync-belt-doc-headers-js-option
ryyppy Jun 18, 2022
cd57c84
Sync docs of js_global.ml
ryyppy Jun 18, 2022
68213d9
Sync docs for js_int.ml
ryyppy Jun 18, 2022
634b6b9
Sync docs for js_re.ml
nkrkv Jun 16, 2022
b75250e
Merge pull request #5443 from nkrkv/sync-belt-doc-headers-js-re
ryyppy Jun 19, 2022
6f5242c
Sync docs for js_json.mli
ryyppy Jun 20, 2022
38dab88
Sync docs for js_null.mli
ryyppy Jun 20, 2022
5ffa57d
Sync docs for js_null_undefined
ryyppy Jun 20, 2022
fde71c9
Sync docs for js_obj.ml
ryyppy Jun 20, 2022
e1cc0ca
Sync docs for js_promise.ml
ryyppy Jun 20, 2022
cff9c68
Sync docs for js_string.ml
ryyppy Jun 20, 2022
3b8d625
Fix misplaced doc in js_array2
ryyppy Jun 20, 2022
89cda0c
Sync docs for js_string2.ml
ryyppy Jun 21, 2022
c99f880
Update docs for js_types.mli
ryyppy Jun 21, 2022
8c88fe1
Sync docs for js_undefined.mli
ryyppy Jun 22, 2022
776cdac
Sync docs for js_vector.mli
ryyppy Jun 22, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Sync docs for js_json.mli
  • Loading branch information
ryyppy committed Jun 20, 2022
commit 6f5242c5b5c59bc01c1b1349c445ee1bb9fa6557
246 changes: 113 additions & 133 deletions jscomp/others/js_json.mli
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
**see** [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON)
*)

(** {2 Types} *)
(** ## Types *)

(** The JSON data structure *)
type t
(** The JSON data structure *)

(** Underlying type of a JSON value *)
type _ kind =
Expand All @@ -51,214 +51,194 @@ type tagged_t =
| JSONObject of t Js_dict.t
| JSONArray of t array


(** {2 Accessor} *)
(** ## Accessor *)

val classify : t -> tagged_t


val test : 'a -> 'b kind -> bool
(** `test v kind` returns true if `v` is of `kind` *)
val test : 'a -> 'b kind -> bool
(** `test(v, kind)` returns `true` if `v` is of `kind`. *)

val decodeString : t -> Js_string.t option
(** `decodeString json` returns `Some s` if `json` is a string, `None`
otherwise *)
(** `decodeString(json)` returns `Some(s)` if `json` is a `string`, `None` otherwise. *)

val decodeNumber : t -> float option
(** `decodeNumber json` returns `Some n` if `json` is a number, `None`
otherwise *)
(** `decodeNumber(json)` returns `Some(n)` if `json` is a `number`, `None` otherwise. *)

val decodeObject : t -> t Js_dict.t option
(** `decodeObject json` returns `Some o` if `json` is an object, `None`
otherwise *)
(** `decodeObject(json)` returns `Some(o)` if `json` is an `object`, `None` otherwise. *)

val decodeArray : t -> t array option
(** `decodeArray json` returns `Some a` if `json` is an array, `None`
otherwise *)
(** `decodeArray(json)` returns `Some(a)` if `json` is an `array`, `None` otherwise. *)

val decodeBoolean : t -> bool option
(** `decodeBoolean json` returns `Some b` if `json` is a boolean, `None`
otherwise *)
(** `decodeBoolean(json)` returns `Some(b)` if `json` is a `boolean`, `None` otherwise. *)

val decodeNull : t -> 'a Js_null.t option
(** `decodeNull json` returns `Some null` if `json` is a null, `None`
otherwise *)
(** `decodeNull(json)` returns `Some(null)` if `json` is a `null`, `None` otherwise. *)

(** {2 Construtors} *)
(** ## Construtors *)

(** Those functions allows the construction of an arbitrary complex
JSON values.
(*
Those functions allows the construction of an arbitrary complex
JSON values.
*)

external null : t = "null" [@@bs.val]
(** `null` is the singleton null JSON value *)
external null : t = "null"
[@@bs.val]
(** `null` is the singleton null JSON value. *)

external string : string -> t = "%identity"
(** `string s` makes a JSON string of the `string` `s` *)
(** `string(s)` makes a JSON string of the `string` `s`. *)

external number : float -> t = "%identity"
(** `number n` makes a JSON number of the `float` `n` *)
(** `number(n)` makes a JSON number of the `float` `n`. *)

external boolean : bool -> t = "%identity"
(** `boolean b` makes a JSON boolean of the `bool` `b` *)
(** `boolean(b)` makes a JSON boolean of the `bool` `b`. *)

external object_ : t Js_dict.t -> t = "%identity"
(** `object_ dict` makes a JSON object of the `Js.Dict.t` `dict` *)

(** `object_(dict)` makes a JSON object of the `Js.Dict.t` `dict`. *)

external array : t array -> t = "%identity"
(** `array_ a` makes a JSON array of the `Js.Json.t array` `a` *)
(** `array_(a)` makes a JSON array of the `Js.Json.t` array `a`. *)

(**
(*
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).
*)

external stringArray : string array -> t = "%identity"
(** `stringArray a` makes a JSON array of the `string array` `a` *)
(** `stringArray(a)` makes a JSON array of the `string` array `a`. *)

external numberArray : float array -> t = "%identity"
(** `numberArray a` makes a JSON array of the `float array` `a` *)
(** `numberArray(a)` makes a JSON array of the `float` array `a`. *)

external booleanArray : bool array -> t = "%identity"
(** `booleanArray` makes a JSON array of the `bool array` `a` *)
(** `booleanArray(a)` makes a JSON array of the `bool` array `a`. *)

external objectArray : t Js_dict.t array -> t = "%identity"
(** `objectArray a` makes a JSON array of the `JsDict.t array` `a` *)
(** `objectArray(a) makes a JSON array of the `JsDict.t` array `a`. *)

(** {2 String conversion} *)
(** ## String conversion *)

external parseExn : string -> t = "parse" [@@bs.val] [@@bs.scope "JSON"]
external parseExn : string -> t = "parse"
[@@bs.val] [@@bs.scope "JSON"]
(**
`parseExn s` parses the string `s` into a JSON data structure

**return** a JSON data structure

**raise** SyntaxError if given string is not a valid JSON. Note `SyntaxError` is a JavaScript exception.

```
(* parse a simple JSON string *)

let json =
try
Js.Json.parseExn {| "foo" |}
with
| _ -> failwith "Error parsing JSON string"
in
match Js.Json.classify json with
| Js.Json.JSONString value -> Js.log value
| _ -> failwith "Expected a string"
```

```
(* parse a complex JSON string *)

let getIds s =
let json =
try
Js.Json.parseExn s
with
| _ -> failwith "Error parsing JSON string"
in
match Js.Json.classify json with
| Js.Json.JSONObject value ->
(* In this branch, compiler infer value : Js.Json.t Js.Dict.t *)
begin match Js.Dict.get value "ids" with
| Some ids ->
begin match Js.Json.classify ids with
| Js.Json.JSONArray ids ->
(* In this branch compiler infer ids : Js.Json.t array *)
ids
| _ -> failwith "Expected an array"
end
| None -> failwith "Expected an `ids` property"
end
| _ -> failwith "Expected an object"

(* prints `1, 2, 3` *)
let _ =
Js.log (getIds {| { "ids" : [1, 2, 3] } |})
```

**see** [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse)
`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** [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse)

```res example
/* 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")
}
```

```res example
/* 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 Js.Dict.t */
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 stringify: t -> string = "stringify"
[@@bs.val] [@@bs.scope "JSON"]
external stringify : t -> string = "stringify"
[@@bs.val] [@@bs.scope "JSON"]
(**
`stringify json` formats the JSON data structure as a string

**return** the string representation of a given JSON data structure
`stringify(json)` formats the JSON data structure as a `string`.
Returns the string representation of a given JSON data structure.

```
(* Creates and stringifies a simple JS object *)
**See** [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify)

let dict = Js.Dict.empty () in
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 [|"bucklescript";"ocaml";"js"|]);
```res example
/* Creates and stringifies a simple JS object */

Js.log (Js.Json.stringify (Js.Json.object_ dict))
```
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(["bucklescript", "ocaml", "js"]))

**see** [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify)
Js.log(Js.Json.stringify(Js.Json.object_(dict)))
```
*)

external stringifyWithSpace: t -> (_ [@bs.as {json|null|json}]) -> int -> string = "stringify"
[@@bs.val] [@@bs.scope "JSON"]
external stringifyWithSpace : t -> (_[@bs.as {json|null|json}]) -> int -> string
= "stringify"
[@@bs.val] [@@bs.scope "JSON"]
(**
`stringify json` formats the JSON data structure as a string
`stringifyWithSpace(json)` formats the JSON data structure as a `string`.
Returns the string representation of a given JSON data structure with spacing.

**return** the string representation of a given JSON data structure
**See** [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify)

```
(* Creates and stringifies a simple JS object with spacing *)
```res example
/* Creates and stringifies a simple JS object with spacing */

let dict = Js.Dict.empty () in
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 [|"bucklescript";"ocaml";"js"|]);
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(["bucklescript", "ocaml", "js"]))

Js.log (Js.Json.stringifyWithSpace (Js.Json.object_ dict) 2)
```

**see** [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify)
Js.log(Js.Json.stringifyWithSpace(Js.Json.object_(dict), 2))
```
*)


external stringifyAny : 'a -> string option = "stringify"
[@@bs.val] [@@bs.scope "JSON"]
[@@bs.val] [@@bs.scope "JSON"]
(**
`stringifyAny value` formats any `value` into a JSON string
`stringifyAny(value)` formats any value into a JSON string.

```
(* prints ``"foo", "bar"`` *)
Js.log (Js.Json.stringifyAny [| "foo"; "bar" |])
```

**see** [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify)
```res example
/* prints `["hello", "world"]` */
Js.log(Js.Json.stringifyAny(["hello", "world"]))
```
*)


val deserializeUnsafe : string -> 'a
(**
Best-effort serialization, it tries to seralize as
many objects as possible and deserialize it back *)
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
*)
val deserializeUnsafe : string -> 'a


val serializeExn : 'a -> string
(**
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
*)
val serializeExn : 'a -> string