Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove json_max type #103

Merged
merged 3 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
37 changes: 31 additions & 6 deletions lib/pretty.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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>@[<hov>%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)
Expand All @@ -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
#elif defined STRINGLIT
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
#elif defined STRINGLIT
let representation = `Stringlit s in
#endif
format std out (`List [ representation; x ])
else
let op = json_string_of_string s in
Format.fprintf out "<@[<hv2>%s: %a@]>" op (format std) x
#endif

and format_field std out (name, x) =
Format.fprintf out "@[<hv2>%s: %a@]" (json_string_of_string name) (format std) x
Expand Down
12 changes: 6 additions & 6 deletions lib/write2.ml
Original file line number Diff line number Diff line change
@@ -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
13 changes: 12 additions & 1 deletion lib/yojson.cppo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#define TUPLE
#define VARIANT
#include "type.ml"
type json_max = t
#include "write.ml"
#include "monomorphic.ml"
module Pretty =
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand Down
1 change: 0 additions & 1 deletion lib/yojson.cppo.mli
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ end
#define VARIANT
#include "type.ml"
#include "monomorphic.mli"
type json_max = t
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that if we want to remove the json_max alias from the public API without making too much changes to the code, only removing this line is sufficient. It would stay defined in the implementation but hidden from the public. That would be enough for releasing 2.0, and we can decide about the cppo madness later.
Am I wrong?

Copy link
Member Author

@Leonidas-from-XIV Leonidas-from-XIV Jan 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would agree. Removing it from the public API should free us from compatibility constraints and then we can implement the rest in any other way going forward.

I've incorporated your suggestions and rebased it on current master so the changelog and CI should succeed now.

#include "write.mli"
#include "write2.mli"
#undef INT
Expand Down