Skip to content

Commit

Permalink
Merge pull request #103 from ocaml-community/remove-json-max-type
Browse files Browse the repository at this point in the history
Remove `json_max` type
  • Loading branch information
Leonidas-from-XIV authored Jan 26, 2022
2 parents b3b4eeb + 1864d32 commit c35769a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 14 deletions.
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 @@ -135,7 +135,6 @@ end
#define VARIANT
#include "type.ml"
#include "monomorphic.mli"
type json_max = t
#include "write.mli"
#include "write2.mli"
#undef INT
Expand Down

0 comments on commit c35769a

Please sign in to comment.