this repo has no description
1(*--------------------------------------------------------------------------- 2 Copyright (c) 2025 Anil Madhavapeddy. All rights reserved. 3 SPDX-License-Identifier: ISC 4 ---------------------------------------------------------------------------*) 5 6(** JSON object-as-map codec utilities. 7 8 JMAP frequently uses JSON objects as maps with string or Id keys. 9 These codecs convert between JSON objects and OCaml association lists. *) 10 11module String_map = Map.Make(String) 12 13let of_string value_jsont = 14 let kind = "String map" in 15 Jsont.Object.map ~kind Fun.id 16 |> Jsont.Object.keep_unknown (Jsont.Object.Mems.string_map value_jsont) ~enc:Fun.id 17 |> Jsont.Object.finish 18 |> Jsont.map 19 ~dec:(fun m -> List.of_seq (String_map.to_seq m)) 20 ~enc:(fun l -> String_map.of_list l) 21 22let of_id value_jsont = 23 let kind = "Id map" in 24 (* Use string map internally, then convert keys to Ids *) 25 let string_codec = of_string value_jsont in 26 let dec pairs = 27 List.map (fun (k, v) -> 28 match Id.of_string k with 29 | Ok id -> (id, v) 30 | Error msg -> Jsont.Error.msgf Jsont.Meta.none "%s: invalid key %s - %s" kind k msg 31 ) pairs 32 in 33 let enc pairs = 34 List.map (fun (id, v) -> (Id.to_string id, v)) pairs 35 in 36 Jsont.map ~kind ~dec ~enc string_codec 37 38let id_to_bool = of_id Jsont.bool 39 40let string_to_bool = of_string Jsont.bool