From ee0de3fd4e02f95f42fd3fe9cb18bcfe3e7e2bf1 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Tue, 5 Nov 2024 19:57:46 +0100 Subject: [ruby/json] JSON.dump: write directly into the provided IO Ref: https://github.com/ruby/json/issues/524 Rather than to buffer everything in memory. Unfortunately Ruby doesn't provide an API to write into and IO without first allocating a string, which is a bit wasteful. https://github.com/ruby/json/commit/f017af6c0a --- test/json/json_common_interface_test.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'test') diff --git a/test/json/json_common_interface_test.rb b/test/json/json_common_interface_test.rb index a5d62337e1..1f157da026 100644 --- a/test/json/json_common_interface_test.rb +++ b/test/json/json_common_interface_test.rb @@ -162,6 +162,17 @@ class JSONCommonInterfaceTest < Test::Unit::TestCase assert_equal too_deep, dump(obj, strict: false) end + def test_dump_in_io + io = StringIO.new + assert_same io, JSON.dump([1], io) + assert_equal "[1]", io.string + + big_object = ["a" * 10, "b" * 40, { foo: 1.23 }] * 5000 + io.rewind + assert_same io, JSON.dump(big_object, io) + assert_equal JSON.dump(big_object), io.string + end + def test_dump_should_modify_defaults max_nesting = JSON.dump_default_options[:max_nesting] dump([], StringIO.new, 10) -- cgit v1.2.3