Skip to content

Commit

Permalink
fix stream_to_file
Browse files Browse the repository at this point in the history
  • Loading branch information
gasche committed Feb 15, 2022
1 parent 0fc0bb7 commit d6a4d63
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

- Avoid copying unnecessarily large amounts of strings when parsing (#85, #108,
@Leonidas-from-XIV)
- Fix `stream_to_file` (#133, @tcoopman and @gasche)

## 1.7.0

Expand Down
3 changes: 2 additions & 1 deletion lib/write.ml
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,8 @@ let stream_to_channel ?buf ?(len=2096) ?std oc st =
None -> Buffer.create len
| Some ob -> ob
in
stream_to_buffer ?std ob st
stream_to_buffer ?std ob st;
Buffer.output_buffer oc ob

let stream_to_file ?len ?std file st =
let oc = open_out file in
Expand Down
17 changes: 17 additions & 0 deletions test/test_write.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,24 @@ let to_file () =
test ~newline:true ();
test ~newline:false ()

let stream_to_file () =
let output_file = Filename.temp_file "test_yojson_stream_to_file" ".json" in
let data = [`String "foo"; `String "bar"] in
Yojson.Safe.stream_to_file output_file (Stream.of_list data);
let read_data =
let stream = Yojson.Safe.stream_from_file output_file in
let acc = ref [] in
Stream.iter (fun v -> acc := v :: !acc) stream;
List.rev !acc
in
Sys.remove output_file;
if data <> read_data then
(* TODO: it would be nice to use Alcotest.check,
but we don't have a 'testable' instance for JSON values. *)
Alcotest.fail "stream_{to,from}_file roundtrip failure"

let single_json = [
"to_string", `Quick, to_string;
"to_file", `Quick, to_file;
"stream_to_file", `Quick, stream_to_file;
]

0 comments on commit d6a4d63

Please sign in to comment.