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

Raise on empty #906

Merged
merged 6 commits into from
Dec 6, 2023
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: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# CHANGELOG

## 3.16.2 - 2023-11-21
## 3.16.2 - 2023-12-06

- Fixed documentation formatting.

- Added option to the "usual" parser to raise an error on an empty input string.

## 3.16.1 - 2023-09-01

- Fixed exception type on number parsing. (thank you @jasonpenny)
Expand Down
20 changes: 20 additions & 0 deletions ext/oj/usual.c
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,9 @@ static VALUE result(ojParser p) {
if (d->vhead < d->vtail) {
return *d->vhead;
}
if (d->raise_on_empty) {
rb_raise(oj_parse_error_class, "empty string");
}
return Qnil;
}

Expand Down Expand Up @@ -1066,6 +1069,20 @@ static VALUE opt_symbol_keys_set(ojParser p, VALUE value) {
return (NULL != d->sym_cache) ? Qtrue : Qfalse;
}

static VALUE opt_raise_on_empty(ojParser p, VALUE value) {
Usual d = (Usual)p->ctx;

return d->raise_on_empty ? Qtrue : Qfalse;
}

static VALUE opt_raise_on_empty_set(ojParser p, VALUE value) {
Usual d = (Usual)p->ctx;

d->raise_on_empty = (Qtrue == value);

return d->raise_on_empty ? Qtrue : Qfalse;
}

static VALUE option(ojParser p, const char *key, VALUE value) {
struct opt *op;
struct opt opts[] = {
Expand Down Expand Up @@ -1095,6 +1112,8 @@ static VALUE option(ojParser p, const char *key, VALUE value) {
{.name = "omit_null=", .func = opt_omit_null_set},
{.name = "symbol_keys", .func = opt_symbol_keys},
{.name = "symbol_keys=", .func = opt_symbol_keys_set},
{.name = "raise_on_empty", .func = opt_raise_on_empty},
{.name = "raise_on_empty=", .func = opt_raise_on_empty_set},
{.name = NULL},
};

Expand Down Expand Up @@ -1129,6 +1148,7 @@ void oj_init_usual(ojParser p, Usual d) {
d->get_key = cache_key;
d->cache_keys = true;
d->ignore_json_create = false;
d->raise_on_empty = false;
d->cache_str = 6;
d->array_class = Qnil;
d->hash_class = Qnil;
Expand Down
1 change: 1 addition & 0 deletions ext/oj/usual.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ typedef struct _usual {
uint8_t miss_class;
bool cache_keys;
bool ignore_json_create;
bool raise_on_empty;
} *Usual;

// Initialize the parser with the usual delegate. If the usual delegate is
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.1'
VERSION = '3.16.2'
end
6 changes: 6 additions & 0 deletions test/json_gem/json_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ def test_pretty_state
# seems to occur on travis but not locally.
actual = state.to_h
actual.delete(:escape_slash)
actual.delete(:strict)
actual.delete(:script_safe)
assert_equal({
:allow_nan => false,
:array_nl => "\n",
Expand All @@ -162,6 +164,8 @@ def test_safe_state
# seems to occur on travis but not locally.
actual = state.to_h
actual.delete(:escape_slash)
actual.delete(:strict)
actual.delete(:script_safe)
assert_equal({
:allow_nan => false,
:array_nl => "",
Expand All @@ -182,6 +186,8 @@ def test_fast_state
# seems to occur on travis but not locally.
actual = state.to_h
actual.delete(:escape_slash)
actual.delete(:strict)
actual.delete(:script_safe)
assert_equal({
:allow_nan => false,
:array_nl => "",
Expand Down
10 changes: 10 additions & 0 deletions test/test_parser_usual.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ def test_hash_class
assert_equal(MyHash, doc.class)
end

def test_empty
p = Oj::Parser.new(:usual)
p.raise_on_empty = false
doc = p.parse(' ')
assert_nil(doc)

p.raise_on_empty = true
assert_raises(Oj::ParseError) { p.parse(' ') }
end

class MyClass
attr_accessor :a
attr_accessor :b
Expand Down