(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*) (* sqlite3's JSON text over nox-json's values: the text is read into JSONB by json.c's parser and written from JSONB by its renderer, and {!Jsonb_codec.json} crosses between JSONB and [Json.t]. *) (* where the parser stopped, as a location in [z] *) let malformed z err = let line = ref 1 and start = ref 0 in for k = 0 to min err (String.length z) - 1 do if z.[k] = '\n' then begin incr line; start := k + 1 end done; let loc = Loc.v ~file:Loc.file_none ~first_byte:err ~last_byte:err ~first_line_num:!line ~first_line_byte:!start ~last_line_num:!line ~last_line_byte:!start in Json.Error.msgf (Json.Meta.v loc) "malformed JSON" let of_string_exn c z = match Json_text.of_string z with | Error err -> raise (Json.Error (malformed z err)) | Ok t -> Json.decode_exn c (Jsonb_codec.dec_at Jsonb_codec.json t 0) let of_string c z = match of_string_exn c z with v -> Ok v | exception Json.Error e -> Error e let to_string ?indent c v = let t = Jsonb_core.empty 64 in Jsonb_codec.enc_into Jsonb_codec.json (Json.encode c v) t; match indent with | None -> Json_text.text_at t 0 | Some n -> Json_text.pretty t ~indent:(String.make (max n 0) ' ')