Skip to content

Commit

Permalink
Merge pull request #124 from panglesd/terminating-json-files-with-new…
Browse files Browse the repository at this point in the history
…lines

Making `to_string` generate POSIX-compliant files
  • Loading branch information
Leonidas-from-XIV authored Jan 24, 2022
2 parents ddada01 + a27932d commit caa7cd8
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
- Add an opam package `yojson-bench` to deal with benchmarks dependency
(@tmcgilchrist, #117)

### Change

- The function `to_file` now adds a newline at the end of the generated file. An
optional argument allows to return to the original behaviour (#124, @panglesd)

## 1.7.0

*2019-02-14*
Expand Down
4 changes: 3 additions & 1 deletion lib/write.ml
Original file line number Diff line number Diff line change
Expand Up @@ -460,10 +460,12 @@ let to_output ?buf ?(len=4096) ?std out x =
out#output (Buffer.contents ob) 0 (Buffer.length ob);
()

let to_file ?len ?std file x =
let to_file ?len ?std ?(newline = true) file x =
let oc = open_out file in
try
to_channel ?len ?std oc x;
if newline then
output_string oc "\n";
close_out oc
with e ->
close_out_noerr oc;
Expand Down
5 changes: 4 additions & 1 deletion lib/write.mli
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ val to_output :
val to_file :
?len:int ->
?std:bool ->
?newline:bool ->
string -> t -> unit
(** Write a compact JSON value to a file.
See [to_string] for the role of the optional arguments. *)
See [to_string] for the role of the optional arguments.
@param newline whether to end the content of the file with a new line.
Optional parameter with value [true] by default. *)

val to_buffer :
?std:bool ->
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ let json_string =
^ {|"string":"string",|}
^ {|"list":[0,1,2]|}
^ "}"

let json_string_newline =
json_string
^ "\n"
3 changes: 3 additions & 0 deletions test/fixtures.mli
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ val json_value : Yojson.Safe.t

(** A JSON string that must parse to [json_value] *)
val json_string : string

(** The same JSON string terminated with a newline *)
val json_string_newline : string
35 changes: 25 additions & 10 deletions test/test_write.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,32 @@ let to_string () =
Alcotest.(check string) __LOC__ Fixtures.json_string (Yojson.Safe.to_string Fixtures.json_value)

let to_file () =
let output_file = Filename.temp_file "test_yojson_to_file" ".json" in
Yojson.Safe.to_file output_file Fixtures.json_value;
let file_content =
let ic = open_in output_file in
let length = in_channel_length ic in
let s = really_input_string ic length in
close_in ic;
s
let test ?newline () =
let output_file = Filename.temp_file "test_yojson_to_file" ".json" in
let correction = match newline with
| None ->
Yojson.Safe.to_file output_file Fixtures.json_value;
Fixtures.json_string_newline
| Some newline ->
Yojson.Safe.to_file ~newline output_file Fixtures.json_value;
if newline then
Fixtures.json_string_newline
else
Fixtures.json_string
in
let file_content =
let ic = open_in output_file in
let length = in_channel_length ic in
let s = really_input_string ic length in
close_in ic;
s
in
Alcotest.(check string) __LOC__ correction file_content;
Sys.remove output_file
in
Alcotest.(check string) __LOC__ Fixtures.json_string file_content;
Sys.remove output_file
test ();
test ~newline:true ();
test ~newline:false ()

let single_json = [
"to_string", `Quick, to_string;
Expand Down

0 comments on commit caa7cd8

Please sign in to comment.