Module Js_json

module Js_json: sig .. end
Efficient JSON encoding using JavaScript API
See also MDN

type t 
The JSON data structure
type '_ kind = 
| String:Js_string.t kind
| Number:float kind
| Object:t Js_dict.t kind
| Array:t array kind
| Boolean:Js.boolean kind
| Null:Js_types.null_val kind
Underlying type of a JSON value
val reifyType : t -> 'b kind * 'b
reifyType v returns both type and underlying value
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
val decodeNumber : t -> float option
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
val decodeArray : t -> t array option
decodeArray json returns Some a if json is an array, None otherwise
val decodeBoolean : t -> Js.boolean option
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
val null : t
null is the singleton null JSON value
val string : string -> t
string s makes a JSON string of the string s
val number : float -> t
number n makes a JSON number of the float n
val boolean : Js.boolean -> t
boolean b makes a JSON boolean of the Js.boolean b
val object_ : t Js_dict.t -> t
object_ dict makes a JSON objet of the Js.Dict.t dict
val array_ : t array -> t
array_ a makes a JSON array of the Js.Json.t array a
val stringArray : string array -> t
stringArray a makes a JSON array of the string array a
val numberArray : float array -> t
numberArray a makes a JSON array of the float array a
val booleanArray : Js.boolean array -> t
booleanArray makes a JSON array of the Js.boolean array a
val objectArray : t Js_dict.t array -> t
objectArray a makes a JSON array of the JsDict.t array a
val parse : string -> t
parse s parses the string s into a JSON data structure

Returns a JSON data structure

Raises SyntaxError if given string is not a valid JSON. Note SyntaxError is a JavaScript exception.
See also MDN
(* parse a simple JSON string *)

let json = 
  try
    Js_json.parse {| "foo" |} 
  with
  | _ -> failwith "Error parsing JSON string"
in
match Js.Json.reifyType json in
| (Js.Json.String, value) -> Js.log value
| _ -> failWith "Expected a string"
(* parse a complex JSON string *)

let getIds s =
  let json = 
    try
      Js.Json.parse s
    with
    | _ -> failwith "Error parsing JSON string"
  in 
  match Js.Json.reifyType json with
  | (Js.Json.Object, 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.reifyType ids with
      | (Js.Json.Array, 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 ] } |} 
val stringify : t -> string
stringify json formats the JSON data structure as a string

Returns the string representation of a given JSON data structure

See also MDN
(* 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.numberOfInt 30); 
Js.Dict.set dict "likes" 
  (Js.Json.stringArray [|"bucklescript";"ocaml";"js"|]);

Js.log @@ Js.Json.stringify (Js.Json.object_ dict) 
val stringifyAny : 'a -> string option
stringifyAny value formats any value into a JSON string
See also MDN
(* prints `["foo", "bar"]` *)
Js.log @@ Js.Json.stringify [| "foo"; "bar" |]
val reify_type : 'a -> 'b kind * 'b
Deprecated.Please use Js_json.reifyType instead