Something went wrong. Try again.
Declarative JSON data manipulation
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123open Codec_model
(* Canonical form *)
(* The codec a [Rec] forces to. Unboxed, so [==] on it is physical equality of that codec. *)type rec_ref = Rec_ref : 'a t -> rec_ref [@@unboxed]
let rec rec_depth r n = function | [] -> None | r' :: rs -> if r == r' then Some n else rec_depth r (n + 1) rs
let add_list b f xs = List.iteri (fun i x -> if i > 0 then Buffer.add_char b ','; f x) xs
(* [stack] holds the codecs the [Rec]s being written force to, the innermost first. *)let rec shape_codec : type a. Buffer.t -> rec_ref list -> a t -> unit = fun b stack -> function | Null _ -> Buffer.add_string b "null" | Bool _ -> Buffer.add_string b "bool" | Number _ -> Buffer.add_string b "number" | String _ -> Buffer.add_string b "string" | Ignore -> Buffer.add_string b "ignore" | Array map -> Buffer.add_char b '['; shape_codec b stack map.elt; Buffer.add_char b ']' | Object map -> shape_object b stack map | Any map -> shape_any b stack map | Map map -> shape_codec b stack map.dom | Rec l -> shape_rec b stack (Lazy.force l)
and shape_rec : type a. Buffer.t -> rec_ref list -> a t -> unit = fun b stack forced -> match rec_depth (Rec_ref forced) 0 stack with | Some n -> Buffer.add_char b '^'; Buffer.add_string b (Int.to_string n) | None -> Buffer.add_string b "rec("; shape_codec b (Rec_ref forced :: stack) forced; Buffer.add_char b ')'
and shape_any : type a. Buffer.t -> rec_ref list -> a any_map -> unit = fun b stack map -> let decs = [ map.dec_null; map.dec_bool; map.dec_number; map.dec_string; map.dec_array; map.dec_object; ] in Buffer.add_char b '('; List.iteri (fun i c -> if i > 0 then Buffer.add_char b '|'; shape_codec b stack c) (List.filter_map Fun.id decs); Buffer.add_char b ')'
and shape_object : type o dec. Buffer.t -> rec_ref list -> (o, dec) object_map -> unit = fun b stack map -> Buffer.add_char b '{'; if map.error_duplicates then Buffer.add_string b "unique;"; add_list b (fun (_, Mem_dec m) -> shape_member b stack m) (String_map.bindings (object_mem_decs map)); Buffer.add_char b ';'; begin match map.shape with | Basic u -> shape_unknown b stack u | Cases (u, cases) -> Option.iter (shape_unknown b stack) u; Buffer.add_char b ';'; shape_member b stack cases.tag; Buffer.add_char b '='; shape_cases b stack cases end; Buffer.add_char b '}'
and shape_member : type o a. Buffer.t -> rec_ref list -> (o, a) mem_map -> unit = fun b stack m -> Buffer.add_char b '"'; Buffer.add_string b (String.escaped m.name); Buffer.add_char b '"'; if Option.is_some m.dec_absent then Buffer.add_char b '?'; Buffer.add_char b ':'; shape_codec b stack m.type'
and shape_unknown : type o mems builder. Buffer.t -> rec_ref list -> (o, mems, builder) unknown_mems -> unit = fun b stack -> function | Skip -> Buffer.add_string b "skip" | Error -> Buffer.add_string b "error" | Keep (mems, _) -> Buffer.add_string b "keep "; shape_codec b stack mems.mems_type
and shape_cases : type o cases tag. Buffer.t -> rec_ref list -> (o, cases, tag) object_cases -> unit = fun b stack cases -> let compare (Case c0) (Case c1) = cases.tag_compare c0.tag c1.tag in add_list b (fun (Case c) -> Buffer.add_string b (Codec_stream.to_string cases.tag.type' c.tag); Buffer.add_char b '>'; shape_object b stack c.object_map) (List.sort compare cases.cases)
let shape c = let b = Buffer.create 256 in shape_codec b [] c; Buffer.contents b