Js_json
Efficient JSON encoding using JavaScript API
type kind(_) =
| String : kind(Js_string.t)
| Number : kind(float)
| Object : kind(Js_dict.t(t))
| Array : kind(array(t))
| Boolean : kind(bool)
| Null : kind(Js_types.null_val)
;
Underlying type of a JSON value
let test: 'a => kind('b) => bool;
test v kind
returns true if v
is of kind
let decodeString: t => option(Js_string.t);
decodeString json
returns Some s
if json
is a string, None
otherwise
let decodeNumber: t => option(float);
decodeNumber json
returns Some n
if json
is a number, None
otherwise
decodeObject json
returns Some o
if json
is an object, None
otherwise
decodeArray json
returns Some a
if json
is an array, None
otherwise
let decodeBoolean: t => option(bool);
decodeBoolean json
returns Some b
if json
is a boolean, None
otherwise
decodeNull json
returns Some null
if json
is a null, None
otherwise
Those functions allows the construction of an arbitrary complex JSON values.
let null: t;
null
is the singleton null JSON value
let string: string => t;
string s
makes a JSON string of the string
s
let number: float => t;
number n
makes a JSON number of the float
n
let boolean: bool => t;
boolean b
makes a JSON boolean of the bool
b
The functions below are specialized for specific array type which happened to be already JSON object in the Melange runtime. Therefore they are more efficient (constant time rather than linear conversion).
let stringArray: array(string) => t;
stringArray a
makes a JSON array of the string array
a
let numberArray: array(float) => t;
numberArray a
makes a JSON array of the float array
a
let booleanArray: array(bool) => t;
booleanArray
makes a JSON array of the bool array
a
let parseExn: string => t;
parseExn s
parses the string s
into a JSON data structure
Returns a JSON data structure
let stringify: t => string;
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 *)
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"|]);
Js.log (Js.Json.stringify (Js.Json.object_ dict))
let stringifyWithSpace: t => int => string;
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 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"|]);
Js.log (Js.Json.stringifyWithSpace (Js.Json.object_ dict) 2)
stringifyAny value
formats any value
into a JSON string
(* prints ``"foo", "bar"`` *)
Js.log (Js.Json.stringifyAny [| "foo"; "bar" |])
Best-effort serialization, it tries to seralize as many objects as possible and deserialize it back
It is unsafe in two aspects