From 00a97db334ee626bdfd9d74cb84ba21bb9c3f36f Mon Sep 17 00:00:00 2001 From: Marek Kubica Date: Tue, 21 Jul 2020 08:36:45 +0200 Subject: [PATCH 1/3] Update Changelog with incompatible change --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 3667d313..40d3e8f3 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,6 +12,7 @@ - Removed `yojson-biniou` library - Removed deprecated `json` type aliasing type `t` which has been available since 1.6.0 (@Leonidas-from-XIV, #100). +- Removed `json_max` type (@Leonidas-from-XIV, #103) - Removed constraint that the "root" value being rendered (via either `pretty_print` or `to_string`) must be an object or array. (@cemerick, #121) From 456de66b1e9693e45f4364e91fccff9f42760083 Mon Sep 17 00:00:00 2001 From: Marek Kubica Date: Tue, 21 Jul 2020 08:40:02 +0200 Subject: [PATCH 2/3] Remove `json_max` type The `json_max` type is the type which includes all polymorphic variants. This is done so the `Pretty` module can be reused through all the variants, but it is somewhat obscure how that works since it requires upcasting. This commit changes it to generate different `Pretty` modules, each specialized to the exact selected variant. --- lib/pretty.ml | 37 +++++++++++++++++++++++++++++++------ lib/write2.ml | 12 ++++++------ lib/yojson.cppo.ml | 13 ++++++++++++- lib/yojson.cppo.mli | 1 - 4 files changed, 49 insertions(+), 14 deletions(-) diff --git a/lib/pretty.ml b/lib/pretty.ml index d3d4f405..208035ad 100644 --- a/lib/pretty.ml +++ b/lib/pretty.ml @@ -3,26 +3,39 @@ let pp_list sep ppx out l = let pp_sep out () = Format.fprintf out "%s@ " sep in Format.pp_print_list ~pp_sep ppx out l -let rec format std (out:Format.formatter) (x : t) : unit = +let rec format std (out:Format.formatter) (x:t) : unit = match x with | `Null -> Format.pp_print_string out "null" | `Bool x -> Format.pp_print_bool out x +#ifdef INT | `Int x -> Format.pp_print_string out (json_string_of_int x) +#endif +#ifdef FLOAT | `Float x -> let s = if std then std_json_string_of_float x else json_string_of_float x in Format.pp_print_string out s +#endif +#ifdef STRING | `String s -> Format.pp_print_string out (json_string_of_string s) - | `Intlit s - | `Floatlit s +#endif +#ifdef INTLIT + | `Intlit s -> Format.pp_print_string out s +#endif +#ifdef FLOATLIT + | `Floatlit s -> Format.pp_print_string out s +#endif +#ifdef STRINGLIT | `Stringlit s -> Format.pp_print_string out s +#endif | `List [] -> Format.pp_print_string out "[]" | `List l -> Format.fprintf out "[@;<1 0>@[%a@]@;<1 -2>]" (pp_list "," (format std)) l | `Assoc [] -> Format.pp_print_string out "{}" | `Assoc l -> Format.fprintf out "{@;<1 0>%a@;<1 -2>}" (pp_list "," (format_field std)) l +#ifdef TUPLE | `Tuple l -> if std then format std out (`List l) @@ -31,19 +44,31 @@ let rec format std (out:Format.formatter) (x : t) : unit = Format.pp_print_string out "()" else Format.fprintf out "(@,%a@;<0 -2>)" (pp_list "," (format std)) l - +#endif +#ifdef VARIANT | `Variant (s, None) -> if std then - format std out (`String s) +#ifdef STRING + let representation = `String s in +#else + let representation = `Stringlit s in +#endif + format std out representation else Format.fprintf out "<%s>" (json_string_of_string s) | `Variant (s, Some x) -> if std then - format std out (`List [ `String s; x ]) +#ifdef STRING + let representation = `String s in +#else + let representation = `Stringlit s in +#endif + format std out (`List [ representation; x ]) else let op = json_string_of_string s in Format.fprintf out "<@[%s: %a@]>" op (format std) x +#endif and format_field std out (name, x) = Format.fprintf out "@[%s: %a@]" (json_string_of_string name) (format std) x diff --git a/lib/write2.ml b/lib/write2.ml index 3fda5644..09062112 100644 --- a/lib/write2.ml +++ b/lib/write2.ml @@ -1,9 +1,9 @@ -let pretty_print ?std out (x : t) = - Pretty.pp ?std out (x :> json_max) +let pretty_print ?std out x = + Pretty.pp ?std out x -let pretty_to_string ?std (x : t) = - Pretty.to_string ?std (x :> json_max) +let pretty_to_string ?std x = + Pretty.to_string ?std x -let pretty_to_channel ?std oc (x : t) = - Pretty.to_channel ?std oc (x :> json_max) +let pretty_to_channel ?std oc x = + Pretty.to_channel ?std oc x diff --git a/lib/yojson.cppo.ml b/lib/yojson.cppo.ml index 218940b7..3805b3ba 100644 --- a/lib/yojson.cppo.ml +++ b/lib/yojson.cppo.ml @@ -9,7 +9,6 @@ #define TUPLE #define VARIANT #include "type.ml" -type json_max = t #include "write.ml" #include "monomorphic.ml" module Pretty = @@ -33,6 +32,10 @@ struct #define STRING #include "type.ml" #include "write.ml" +module Pretty = +struct +#include "pretty.ml" +end #include "monomorphic.ml" #include "write2.ml" #include "read.ml" @@ -56,6 +59,10 @@ struct #include "type.ml" #include "safe.ml" #include "write.ml" +module Pretty = +struct +#include "pretty.ml" +end #include "monomorphic.ml" #include "write2.ml" #include "read.ml" @@ -80,6 +87,10 @@ struct #define VARIANT #include "type.ml" #include "write.ml" +module Pretty = +struct +#include "pretty.ml" +end #include "monomorphic.ml" #include "write2.ml" #include "read.ml" diff --git a/lib/yojson.cppo.mli b/lib/yojson.cppo.mli index 2b5c44fa..f3452294 100644 --- a/lib/yojson.cppo.mli +++ b/lib/yojson.cppo.mli @@ -132,7 +132,6 @@ end #define VARIANT #include "type.ml" #include "monomorphic.mli" -type json_max = t #include "write.mli" #include "write2.mli" #undef INT From 1864d325cb80f3c4faf91c3a2a3617ea6495b7db Mon Sep 17 00:00:00 2001 From: Marek Kubica Date: Wed, 26 Jan 2022 13:24:50 +0100 Subject: [PATCH 3/3] Make sure either STRING or STRINGLIT are defined Co-authored-by: panglesd <34110029+panglesd@users.noreply.github.com> --- lib/pretty.ml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pretty.ml b/lib/pretty.ml index 208035ad..3c4019fc 100644 --- a/lib/pretty.ml +++ b/lib/pretty.ml @@ -50,7 +50,7 @@ let rec format std (out:Format.formatter) (x:t) : unit = if std then #ifdef STRING let representation = `String s in -#else +#elif defined STRINGLIT let representation = `Stringlit s in #endif format std out representation @@ -61,7 +61,7 @@ let rec format std (out:Format.formatter) (x:t) : unit = if std then #ifdef STRING let representation = `String s in -#else +#elif defined STRINGLIT let representation = `Stringlit s in #endif format std out (`List [ representation; x ])