(* Properties of the JSONB codec (jsonb.html) and the json1 functions over it: no input escapes as anything but the engine's own error, text that parses renders to RFC 8259 text that parses back to the same JSONB and renders the same, json() answers that text, minified RFC 8259 text renders back byte for byte, and a document the codec reads writes back as the value it read. *) open Alcobar module J = Sql.Jsonb module O = Sql.Private.Json1 module V = Catalog.Value (* The engine's refusal is an answer; any other exception escapes and fails the sample. *) let total f x = match f x with v -> Some v | exception Catalog.Error _ -> None (* jsonb(text), and json() of JSONB *) let of_text s = match O.json ~blob:true (V.Text s) with | V.Blob b, _ -> Some b | _ -> None | exception Catalog.Error _ -> None let render b = match O.json ~blob:false (V.Blob b) with | V.Text t, _ -> t | _ -> failf "json() of JSONB is not text" let seek path c = match J.Cursor.seek path c with _ -> () (* Arbitrary bytes, as text, as a BLOB, through the codec and the cursor. *) let test_crash s = ignore (of_text s : string option); ignore (total J.sniff s : bool option); ignore (total (O.json ~blob:false) (V.Blob s) : O.answer option); ignore (total (O.json ~blob:true) (V.Blob s) : O.answer option); ignore (total (fun v -> O.pretty v None) (V.Blob s) : O.answer option); ignore (total O.error_position (V.Text s) : O.answer option); ignore (total O.error_position (V.Blob s) : O.answer option); (* A valid document and a codec refusal are both answers for arbitrary bytes. *) (match J.of_string J.Codec.Value.t s with | Ok _ | Error _ -> ()); let c = J.Cursor.of_string s in ignore (total (seek "$.a[#-1]") c : unit option); ignore (total (J.Cursor.set "$.b[0]" J.Codec.int "1") c : (unit, _) result option) (* json() answers the rendering [t] of [s], tagged as JSON. *) let check_json s t = if s <> "" then match O.json ~blob:false (V.Text s) with | V.Text t', true -> check_eq ~pp:Fmt.string t t' | _ -> failf "json(%S) is not tagged text" s (* Text that parses: its rendering parses back to JSONB that renders the same text, the canonical form being a fixed point, and json() answers it. Nothing parses deeper than the renderer goes, so the rendering never fails. *) let test_roundtrip s = match of_text s with | None -> reach "refused" | Some b -> ( reach "parsed"; let t = render b in match of_text t with | None -> failf "the rendering %S of %S does not parse" t s | Some b' -> check_eq ~pp:Fmt.string t (render b'); check_json s t) (* sqlite3's text over nox-json's values: what reading then writing gives reads and writes back unchanged. *) let test_json s = let t = Json.Codec.Value.t in match Sql.Json.of_string t s with | Error _ -> reach "refused" | Ok v -> ( reach "read"; let s' = Sql.Json.to_string t v in match Sql.Json.of_string t s' with | Error e -> failf "%S does not read back: %a" s' Json.Error.pp e | Ok v' -> check_eq ~pp:Fmt.string s' (Sql.Json.to_string t v')) (* JSONB the codec reads writes back as the same value: the value is the format's own, so no element changes type or text. *) let test_value s = match of_text s with | None -> () | Some b -> ( match J.of_string J.Codec.Value.t b with | Error _ -> failf "jsonb(%S) is not valid JSONB" s | Ok v -> let b' = J.to_string J.Codec.Value.t v in check_eq ~pp:J.Value.pp ~eq:J.Value.equal v (J.of_string_exn J.Codec.Value.t b')) (* Minified RFC 8259 text, built from generated values. *) let hex = "0123456789abcdef" let json_text = let str = map [ bytes ] (fun s -> let b = Buffer.create (String.length s + 2) in Buffer.add_char b '"'; String.iter (fun c -> match c with | '"' -> Buffer.add_string b "\\\"" | '\\' -> Buffer.add_string b "\\\\" | '\n' -> Buffer.add_string b "\\n" | c when Char.code c < 0x20 || Char.code c >= 0x80 -> Buffer.add_string b "\\u00"; Buffer.add_char b hex.[Char.code c lsr 4]; Buffer.add_char b hex.[Char.code c land 0xf] | c -> Buffer.add_char b c) s; Buffer.add_char b '"'; Buffer.contents b) in let scalar = choose [ const "null"; const "true"; const "false"; map [ int64 ] Int64.to_string; map [ int; uint8 ] (fun i f -> Fmt.str "%d.%de%d" i f (f mod 7)); str; ] in fix (fun json -> choose [ scalar; map [ list json ] (fun xs -> "[" ^ String.concat "," xs ^ "]"); map [ list (pair str json) ] (fun kvs -> "{" ^ String.concat "," (List.map (fun (k, v) -> k ^ ":" ^ v) kvs) ^ "}"); ]) let test_canonical s = match of_text s with | None -> failf "%S does not parse" s | Some b -> check_eq ~pp:Fmt.string s (render b) let suite = ( "jsonb", [ test_case "no exception escapes" [ bytes ] test_crash; test_case ~outcomes:[ "parsed"; "refused" ] "text round trips" [ choose [ bytes; json_text ] ] test_roundtrip; test_case "canonical text renders back" [ json_text ] test_canonical; test_case "values write back" [ choose [ bytes; json_text ] ] test_value; test_case ~outcomes:[ "read"; "refused" ] "nox-json values round trip" [ choose [ bytes; json_text ] ] test_json; ] )