diff options
| author | Sébastien Dailly <sebastien@dailly.me> | 2026-03-27 08:48:05 +0100 |
|---|---|---|
| committer | Sébastien Dailly <sebastien@dailly.me> | 2026-03-27 08:48:05 +0100 |
| commit | 830e7c5d27d161b91402414e6ecd49f1ed818bfb (patch) | |
| tree | 448864635d29b1ea66424cbb9e0816fbcf1e1907 /lib/configuration/json5.ml | |
Initial commit
Diffstat (limited to 'lib/configuration/json5.ml')
| -rw-r--r-- | lib/configuration/json5.ml | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/lib/configuration/json5.ml b/lib/configuration/json5.ml new file mode 100644 index 0000000..f6e182c --- /dev/null +++ b/lib/configuration/json5.ml @@ -0,0 +1,54 @@ +module Json_decodeable : + Decoders.Decode.Decodeable with type value = Yojson_five.Basic.t = struct + type value = Yojson_five.Basic.t + + let pp fmt json = Yojson.Basic.pretty_print fmt json + + let of_string : string -> (value, string) result = + fun string -> Yojson_five.Basic.from_string string + + let of_file file = Yojson_five.Basic.from_file file + let get_string = function `String value -> Some value | _ -> None + let get_int = function `Int value -> Some value | _ -> None + + let get_float = function + | `Float value -> Some value + | `Int value -> Some (float_of_int value) + | _ -> None + + let get_bool = function `Bool value -> Some value | _ -> None + let get_null = function `Null -> Some () | _ -> None + + let get_list : value -> value list option = function + | `List l -> Some l + | _ -> None + + let get_key_value_pairs : value -> (value * value) list option = function + | `Assoc assoc -> + Some (List.map (fun (key, value) -> (`String key, value)) assoc) + | _ -> None + + let to_list values = `List values +end + +module Decode = Decoders.Decode.Make (Json_decodeable) + +module Json_encodeable = struct + type value = Yojson_five.Basic.t + + let to_string json = Yojson_five.Basic.to_string json + let of_string x = `String x + let of_int x = `Int x + let of_float x = `Float x + let of_bool x = `Bool x + let null = `Null + let of_list xs = `List xs + + let of_key_value_pairs xs = + `Assoc + (xs + |> Decoders.Util.My_list.filter_map (fun (k, v) -> + match k with `String k -> Some (k, v) | _ -> None)) +end + +module Encode = Decoders.Encode.Make (Json_encodeable) |
