Source file ezjsonm_interface.ml

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
(**************************************************************************)
(*                                                                        *)
(*                 Copyright 2018-2022 OCamlPro                           *)
(*                                                                        *)
(*  All rights reserved. This file is distributed under the terms of the  *)
(*  GNU Lesser General Public License version 2.1, with the special       *)
(*  exception on linking described in the file LICENSE.                   *)
(*                                                                        *)
(**************************************************************************)

open Js_of_ocaml

exception Parse_error of Json_repr.ezjsonm * string

let from_string s =
  try
    Js._JSON##parse (Js.string s) |> Js_json.json_of_js
  with (Js_error.Exn e) ->
    let e2 = Js_error.to_error e in
    if Js.to_string e2##.name = "SyntaxError" then
      raise @@ Parse_error (`Null, Printf.sprintf "Ezjsonm.from_string %s" (Js.to_string e2##.message))
    else Js_error.raise_ e

let to_string ?(minify=true) j =
  if minify then
    Js._JSON##stringify (Js_json.js_of_json j) |> Js.to_string
  else
    Js.Unsafe.fun_call (Js.Unsafe.pure_js_expr "JSON.stringify")
      [| Js_json.js_of_json j; Js.Unsafe.inject (Js.null); Js.Unsafe.inject 2 |]
    |> Js.to_string