Skip to content
This repository
Newer
Older
100644 62 lines (52 sloc) 1.696 kb
fccc6851 »
2011-06-21 Initial open-source release
1 (*
2 Copyright © 2011 MLstate
3
4 This file is part of OPA.
5
6 OPA is free software: you can redistribute it and/or modify it under the
7 terms of the GNU Affero General Public License, version 3, as published by
8 the Free Software Foundation.
9
10 OPA is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
13 more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with OPA. If not, see <http://www.gnu.org/licenses/>.
17 *)
18 (** Just define the json type in Ocaml *)
19
20 (** Json type in OCaml *)
21 type json =
22 | Int of int
23 | Float of float
24 | String of string
25 | Bool of bool
26 | Void
27 | Array of json list
28 | Record of (string * json) list
29
30 (** {6 Passing Json to OPA} *)
31
32 (**
33 In opa, you can use magic_unserialize to unserialize
34 this structure, and build opa values.
35
36 <!> For optimization purpose, for passing such values to opa,
37 the fields in [Record] should be in decreasing order.
38
39 This function checks that property
40 *)
41 let assert_for_opa_magic_unserialize json =
42 let check_record list =
43 let rec aux last = function
44 | [] -> ()
45 | (hd, _) :: tl ->
46 if not (String.compare last hd > 0)
47 then assert false
48 else aux hd tl
49 in
50 match list with
51 | [] | [_] -> ()
52 | (hd, _) :: tl -> aux hd tl
53 in
54 let rec aux = function
55 | Array json -> List.iter aux json
56 | Record fields ->
57 check_record fields ;
58 List.iter (fun (_, json) -> aux json) fields
59 | _ -> ()
60 in
61 aux json
Something went wrong with that request. Please try again.