Skip to content

Commit aa1a43f

Browse files
Always output JSON compliant floats (if possible)
This disables the `std` argument but keeps it and the functions to not create additional unnecessary API breakage.
1 parent d6dcc95 commit aa1a43f

File tree

3 files changed

+26
-75
lines changed

3 files changed

+26
-75
lines changed

CHANGES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
### Changed
66

7+
- Floats are now always output to JSON in a standard-conformant way or not at
8+
all (raising an exception). This makes the `std` variants of functions
9+
identical to the non-`std` variants and the `std` arguments have no effect.
10+
Users are encouraged to switch to the non-`std` affixed variants, the others
11+
will be deprecated in the future. (#184, @Leonidas-from-XIV)
12+
713
### Deprecated
814

915
### Fixed

lib/prettyprint.ml

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ let is_atom_list l =
6666
bar
6767
]
6868
*)
69-
let rec format ~inside_box std (out:Format.formatter) (x:t) : unit =
69+
let rec format ~inside_box (out : Format.formatter) (x : t) : unit =
7070
match x with
7171
| `Null -> Format.pp_print_string out "null"
7272
| `Bool x -> Format.pp_print_bool out x
@@ -75,11 +75,7 @@ let rec format ~inside_box std (out:Format.formatter) (x:t) : unit =
7575
#endif
7676
#ifdef FLOAT
7777
| `Float x ->
78-
let s =
79-
if std then std_json_string_of_float x
80-
else json_string_of_float x
81-
in
82-
Format.pp_print_string out s
78+
Format.pp_print_string out (json_string_of_float x)
8379
#endif
8480
#ifdef STRING
8581
| `String s -> Format.pp_print_string out (json_string_of_string s)
@@ -99,24 +95,25 @@ let rec format ~inside_box std (out:Format.formatter) (x:t) : unit =
9995
if is_atom_list l then
10096
(* use line wrapping like we would do for a paragraph of text *)
10197
Format.fprintf out "[@;<1 0>@[<hov>%a@]@;<1 -2>]"
102-
(pp_list "," (format ~inside_box:false std)) l
98+
(pp_list "," (format ~inside_box:false)) l
10399
else
104100
(* print the elements horizontally if they fit on the line,
105101
otherwise print them in a column *)
106102
Format.fprintf out "[@;<1 0>@[<hv>%a@]@;<1 -2>]"
107-
(pp_list "," (format ~inside_box:false std)) l;
103+
(pp_list "," (format ~inside_box:false)) l;
108104
if not inside_box then Format.fprintf out "@]";
109105
| `Assoc [] -> Format.pp_print_string out "{}"
110106
| `Assoc l ->
111107
if not inside_box then Format.fprintf out "@[<hv2>";
112-
Format.fprintf out "{@;<1 0>%a@;<1 -2>}" (pp_list "," (format_field std)) l;
108+
Format.fprintf out "{@;<1 0>%a@;<1 -2>}" (pp_list "," (format_field)) l;
113109
if not inside_box then Format.fprintf out "@]";
114110

115-
and format_field std out (name, x) =
116-
Format.fprintf out "@[<hv2>%s: %a@]" (json_string_of_string name) (format ~inside_box:true std) x
111+
and format_field out (name, x) =
112+
Format.fprintf out "@[<hv2>%s: %a@]" (json_string_of_string name) (format ~inside_box:true) x
117113

118-
let pp ?(std = false) out x =
119-
Format.fprintf out "@[<hv2>%a@]" (format ~inside_box:true std) (x :> t)
114+
(* [std] argument to be deprecated *)
115+
let pp ?(std = true) out x =
116+
Format.fprintf out "@[<hv2>%a@]" (format ~inside_box:true) (x :> t)
120117

121118
let to_string ?std x =
122119
Format.asprintf "%a" (pp ?std) x

lib/write.ml

Lines changed: 10 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -144,23 +144,12 @@ let write_normal_float_prec significant_figures ob x =
144144
if float_needs_period s then
145145
Buffer.add_string ob ".0"
146146

147-
(* used by atdgen *)
148-
let write_float_prec significant_figures ob x =
149-
match classify_float x with
150-
FP_nan ->
151-
Buffer.add_string ob "NaN"
152-
| FP_infinite ->
153-
Buffer.add_string ob (if x > 0. then "Infinity" else "-Infinity")
154-
| _ ->
155-
write_normal_float_prec significant_figures ob x
156-
157147
let json_string_of_float x =
158148
let ob = Buffer.create 20 in
159149
write_float ob x;
160150
Buffer.contents ob
161151

162-
163-
let write_std_float ob x =
152+
let write_float ob x =
164153
match classify_float x with
165154
FP_nan ->
166155
Common.json_error "NaN value not allowed in standard JSON"
@@ -180,8 +169,11 @@ let write_std_float ob x =
180169
if float_needs_period s then
181170
Buffer.add_string ob ".0"
182171

172+
(* to be deprecated in a future release *)
173+
let write_std_float = write_float
174+
183175
(* used by atdgen *)
184-
let write_std_float_prec significant_figures ob x =
176+
let write_float_prec significant_figures ob x =
185177
match classify_float x with
186178
FP_nan ->
187179
Common.json_error "NaN value not allowed in standard JSON"
@@ -194,11 +186,7 @@ let write_std_float_prec significant_figures ob x =
194186
| _ ->
195187
write_normal_float_prec significant_figures ob x
196188

197-
let std_json_string_of_float x =
198-
let ob = Buffer.create 20 in
199-
write_std_float ob x;
200-
Buffer.contents ob
201-
189+
let write_std_float_prec = write_float_prec
202190

203191
let write_intlit = Buffer.add_string
204192
let write_floatlit = Buffer.add_string
@@ -262,51 +250,11 @@ and write_list ob l =
262250

263251
let write_t = write_json
264252

265-
let rec write_std_json ob (x : t) =
266-
match x with
267-
`Null -> write_null ob ()
268-
| `Bool b -> write_bool ob b
269-
#ifdef INT
270-
| `Int i -> write_int ob i
271-
#endif
272-
#ifdef INTLIT
273-
| `Intlit s -> Buffer.add_string ob s
274-
#endif
275-
#ifdef FLOAT
276-
| `Float f -> write_std_float ob f
277-
#endif
278-
#ifdef FLOATLIT
279-
| `Floatlit s -> Buffer.add_string ob s
280-
#endif
281-
#ifdef STRING
282-
| `String s -> write_string ob s
283-
#endif
284-
#ifdef STRINGLIT
285-
| `Stringlit s -> Buffer.add_string ob s
286-
#endif
287-
| `Assoc l -> write_std_assoc ob l
288-
| `List l -> write_std_list ob l
253+
let write_std_json = write_json
289254

290-
and write_std_assoc ob l =
291-
let f_elt ob (s, x) =
292-
write_string ob s;
293-
Buffer.add_char ob ':';
294-
write_std_json ob x
295-
in
296-
Buffer.add_char ob '{';
297-
iter2 f_elt f_sep ob l;
298-
Buffer.add_char ob '}';
299-
300-
and write_std_list ob l =
301-
Buffer.add_char ob '[';
302-
iter2 write_std_json f_sep ob l;
303-
Buffer.add_char ob ']'
304-
305-
let to_buffer ?(suf = "") ?(std = false) ob x =
306-
if std then
307-
write_std_json ob x
308-
else
309-
write_json ob x;
255+
(* std argument is going to be deprecated *)
256+
let to_buffer ?(suf = "") ?(std = true) ob x =
257+
write_json ob x;
310258
Buffer.add_string ob suf
311259

312260
let to_string ?buf ?(len = 256) ?(suf = "") ?std x =

0 commit comments

Comments
 (0)