Skip to content

Commit

Permalink
Raise on empty (#906)
Browse files Browse the repository at this point in the history
* Add raise_on_empty option

* Remove :strict from json test

* Remove :strict from json test

* Remove :script_safe from json test

* Update release date

* clang-format
  • Loading branch information
ohler55 committed Dec 6, 2023
1 parent 67c20f5 commit b76175c
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
@@ -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
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
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
@@ -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
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
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

0 comments on commit b76175c

Please sign in to comment.