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

Parser multi load #932

Merged
merged 5 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 3.16.5 - 2024-07-07

- Fixed Oj::Parser so that block procedures work correctly.

## 3.16.4 - 2024-06-08

- Fixed Oj::Parse EOF issue on larger stream input.
Expand Down
18 changes: 17 additions & 1 deletion ext/oj/usual.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,14 @@ static void close_object(ojParser p) {
}
rb_hash_bulk_insert(d->vtail - head, head, obj);
d->ktail = d->khead + c->ki;

d->vtail = head;
head--;
*head = obj;
if (1 == d->vtail - d->vhead && rb_block_given_p()) {
d->vtail = d->vhead;
rb_yield(obj);
}
}

static void close_object_class(ojParser p) {
Expand Down Expand Up @@ -572,7 +577,18 @@ static VALUE result(ojParser p) {
Usual d = (Usual)p->ctx;

if (d->vhead < d->vtail) {
return *d->vhead;
long cnt = d->vtail - d->vhead;
volatile VALUE ary;
volatile VALUE *vp;

if (1 == cnt) {
return *d->vhead;
}
ary = rb_ary_new();
for (vp = d->vhead; vp < d->vtail; vp++) {
rb_ary_push(ary, *vp);
}
return ary;
}
if (d->raise_on_empty) {
rb_raise(oj_parse_error_class, "empty string");
Expand Down
2 changes: 1 addition & 1 deletion lib/oj/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Oj
# Current version of the module.
VERSION = '3.16.4'
VERSION = '3.16.5'
end
24 changes: 24 additions & 0 deletions test/test_parser_usual.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,30 @@ def test_decimal
assert_equal(Float, doc.class)
end

def test_multi_parse
p = Oj::Parser.new(:usual)
out = []
p.parse('{"a":1}{"b":{"x":2}} {"c":3}') { |j| out.push(j) }
assert_equal([{'a'=>1}, {'b'=>{'x'=>2}},{'c'=>3}], out)
end

def test_multi_load
p = Oj::Parser.new(:usual)
out = []
r, w = IO.pipe
thread = Thread.new do
['{"a":1}', '{"b":{"x"', ':2}}{"c":', '3}'].each { |seg|
w.write(seg)
sleep(0.1)
}
w.close
end
p.load(r) { |j| out.push(j) }
r.close
thread.join
assert_equal([{'a'=>1}, {'b'=>{'x'=>2}},{'c'=>3}], out)
end

def test_omit_null
p = Oj::Parser.new(:usual)
p.omit_null = true
Expand Down