From 90c497ca7504ba3487738363ae8c483c322ee117 Mon Sep 17 00:00:00 2001 From: Tim McGilchrist Date: Thu, 20 Jan 2022 20:02:44 +1100 Subject: [PATCH] Add Yojson.Raw.Util module Provides combinators for extracting fields from Yojson.Raw values. Matches the API for Yojson.Basic.Util and Yojson.Safe.Util. --- CHANGES.md | 3 ++ lib/util.ml | 85 +++++++++++++++++++++++++++++++++++++++------ lib/yojson.cppo.ml | 4 +++ lib/yojson.cppo.mli | 5 +++ 4 files changed, 86 insertions(+), 11 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index e0c3b058..f1745112 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,9 @@ ### Added +- Add Yojson.Raw.Util module to provide combinators for extracting fields + from Yojson.Raw values. (@tmcgilchrist, #163) + ### Removed ### Changed diff --git a/lib/util.ml b/lib/util.ml index 8f787ea5..695ffb0d 100644 --- a/lib/util.ml +++ b/lib/util.ml @@ -4,11 +4,17 @@ let typeof = function | `Assoc _ -> "object" | `Bool _ -> "bool" | `Float _ -> "float" +#ifdef INT | `Int _ -> "int" +#endif | `List _ -> "array" | `Null -> "null" | `String _ -> "string" | `Intlit _ -> "intlit" + | `Floatlit _ -> "floatlit" +#ifdef STRINGLIT + | `Stringlit _ -> "stringlit" +#endif | `Tuple _ -> "tuple" | `Variant _ -> "variant" @@ -49,38 +55,62 @@ let to_bool_option = function | js -> typerr "Expected bool or null, got " js let to_number = function +#ifdef INT | `Int i -> float i +#endif +#ifdef FLOAT | `Float f -> f +#endif | js -> typerr "Expected number, got " js let to_number_option = function +#ifdef INT | `Int i -> Some (float i) +#endif +#ifdef FLOAT | `Float f -> Some f +#endif | `Null -> None | js -> typerr "Expected number or null, got " js -let to_float = function `Float f -> f | js -> typerr "Expected float, got " js +let to_float = function +#ifdef FLOAT + | `Float f -> f +#endif + | js -> typerr "Expected float, got " js let to_float_option = function +#ifdef FLOAT | `Float f -> Some f +#endif | `Null -> None | js -> typerr "Expected float or null, got " js -let to_int = function `Int i -> i | js -> typerr "Expected int, got " js +let to_int = function +#ifdef INT + | `Int i -> i +#endif + | js -> typerr "Expected int, got " js let to_int_option = function +#ifdef INT | `Int i -> Some i +#endif | `Null -> None | js -> typerr "Expected int or null, got " js let to_list = function `List l -> l | js -> typerr "Expected array, got " js let to_string = function +#ifdef STRING | `String s -> s +#endif | js -> typerr "Expected string, got " js let to_string_option = function +#ifdef STRING | `String s -> Some s +#endif | `Null -> None | js -> typerr "Expected string or null, got " js @@ -124,17 +154,50 @@ let filter_member k l = let filter_assoc l = filter_map (function `Assoc l -> Some l | _ -> None) l let filter_bool l = filter_map (function `Bool x -> Some x | _ -> None) l -let filter_int l = filter_map (function `Int x -> Some x | _ -> None) l -let filter_float l = filter_map (function `Float x -> Some x | _ -> None) l +let filter_int l = + filter_map ( + function + #ifdef INT + | `Int x -> Some x + #endif + | _ -> None + ) l + +let filter_float l = + filter_map ( + function +#ifdef FLOAT + `Float x -> Some x +#endif + | _ -> None + ) l let filter_number l = - filter_map - (function `Int x -> Some (float x) | `Float x -> Some x | _ -> None) - l - -let filter_string l = filter_map (function `String x -> Some x | _ -> None) l -let keys o = to_assoc o |> List.map (fun (key, _) -> key) -let values o = to_assoc o |> List.map (fun (_, value) -> value) + filter_map ( + function +#ifdef INT + `Int x -> Some (float x) +#endif +#ifdef FLOAT + | `Float x -> Some x +#endif + | _ -> None + ) l + +let filter_string l = + filter_map ( + function +#ifdef STRING + `String x -> Some x +#endif + | _ -> None + ) l + +let keys o = + to_assoc o |> List.map (fun (key, _) -> key) + +let values o = + to_assoc o |> List.map (fun (_, value) -> value) let combine (first : t) (second : t) = match (first, second) with diff --git a/lib/yojson.cppo.ml b/lib/yojson.cppo.ml index 3805b3ba..3c4df1bc 100644 --- a/lib/yojson.cppo.ml +++ b/lib/yojson.cppo.ml @@ -94,6 +94,10 @@ end #include "monomorphic.ml" #include "write2.ml" #include "read.ml" +module Util = +struct + #include "util.ml" +end #undef INTLIT #undef FLOATLIT #undef STRINGLIT diff --git a/lib/yojson.cppo.mli b/lib/yojson.cppo.mli index ba62d73b..9185ed30 100644 --- a/lib/yojson.cppo.mli +++ b/lib/yojson.cppo.mli @@ -105,6 +105,11 @@ sig #include "write.mli" #include "write2.mli" #include "read.mli" +(** This module provides combinators for extracting fields from JSON values. *) +module Util : +sig + #include "util.mli" +end #undef INTLIT #undef FLOATLIT #undef STRINGLIT