Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix streaming to file descriptor #945

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions ext/oj/stream_writer.c
Original file line number Diff line number Diff line change
@@ -42,7 +42,8 @@ static void stream_writer_write(StreamWriter sw) {

switch (sw->type) {
case STRING_IO:
case STREAM_IO: {
case STREAM_IO:
case FILE_IO: {
volatile VALUE rs = rb_str_new(sw->sw.out.buf, size);

// Oddly enough, when pushing ASCII characters with UTF-8 encoding or
@@ -53,11 +54,6 @@ static void stream_writer_write(StreamWriter sw) {
rb_funcall(sw->stream, oj_write_id, 1, rs);
break;
}
case FILE_IO:
if (size != write(sw->fd, sw->sw.out.buf, size)) {
rb_raise(rb_eIOError, "Write failed. [_%d_:%s]\n", errno, strerror(errno));
}
break;
default: rb_raise(rb_eArgError, "expected an IO Object.");
}
stream_writer_reset_buf(sw);
16 changes: 16 additions & 0 deletions test/test_writer.rb
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
$LOAD_PATH << __dir__

require 'helper'
require 'open3'

class OjWriter < Minitest::Test

@@ -377,4 +378,19 @@ def test_stream_writer_push_null_value_with_key
w.pop()
assert_equal(%|{"nothing":null}\n|, output.string())
end

def test_stream_writer_subprocess
skip if RbConfig::CONFIG['host_os'] =~ /(mingw|mswin)/

Open3.popen3("/bin/bash", "-c", "cat > /dev/null") do |stdin, _stdout, _stderr, _wait_thr|
w = Oj::StreamWriter.new(stdin, :indent => 0)
w.push_array()
chunk = "{\"foo\":\"#{"bar"*1000}\"}"
1000.times do |_|
w.push_json(chunk)
end
w.pop()
stdin.close
end
end
end # OjWriter