From 570999f04c1d0202ccdd297b82914114b94e904a Mon Sep 17 00:00:00 2001 From: Peter Ohler Date: Sun, 3 Dec 2023 19:47:31 -0500 Subject: [PATCH 1/6] Add raise_on_empty option --- CHANGELOG.md | 2 ++ ext/oj/usual.c | 20 ++++++++++++++++++++ ext/oj/usual.h | 1 + lib/oj/version.rb | 2 +- test/test_parser_usual.rb | 10 ++++++++++ 5 files changed, 34 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bc31ab8a..294470b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ - 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) diff --git a/ext/oj/usual.c b/ext/oj/usual.c index 27a1d351..c3b10160 100644 --- a/ext/oj/usual.c +++ b/ext/oj/usual.c @@ -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; } @@ -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[] = { @@ -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}, }; @@ -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; diff --git a/ext/oj/usual.h b/ext/oj/usual.h index 826ca28b..7da13b3e 100644 --- a/ext/oj/usual.h +++ b/ext/oj/usual.h @@ -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 diff --git a/lib/oj/version.rb b/lib/oj/version.rb index 4846b6f4..39bbe0d5 100644 --- a/lib/oj/version.rb +++ b/lib/oj/version.rb @@ -1,4 +1,4 @@ module Oj # Current version of the module. - VERSION = '3.16.1' + VERSION = '3.16.2' end diff --git a/test/test_parser_usual.rb b/test/test_parser_usual.rb index 8ec1fbfa..f9b620c2 100755 --- a/test/test_parser_usual.rb +++ b/test/test_parser_usual.rb @@ -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 From e0da8b782410e91e67285c5262177eeaf240cc39 Mon Sep 17 00:00:00 2001 From: Peter Ohler Date: Sun, 3 Dec 2023 20:12:36 -0500 Subject: [PATCH 2/6] Remove :strict from json test --- test/json_gem/json_generator_test.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/test/json_gem/json_generator_test.rb b/test/json_gem/json_generator_test.rb index fe5d590b..d15c1908 100755 --- a/test/json_gem/json_generator_test.rb +++ b/test/json_gem/json_generator_test.rb @@ -162,6 +162,7 @@ def test_safe_state # seems to occur on travis but not locally. actual = state.to_h actual.delete(:escape_slash) + actual.delete(:strict) assert_equal({ :allow_nan => false, :array_nl => "", From 6439ba3f89d0df13212923632913fa5a59cffeeb Mon Sep 17 00:00:00 2001 From: Peter Ohler Date: Sun, 3 Dec 2023 20:25:48 -0500 Subject: [PATCH 3/6] Remove :strict from json test --- test/json_gem/json_generator_test.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/json_gem/json_generator_test.rb b/test/json_gem/json_generator_test.rb index d15c1908..75db3c12 100755 --- a/test/json_gem/json_generator_test.rb +++ b/test/json_gem/json_generator_test.rb @@ -142,6 +142,7 @@ def test_pretty_state # seems to occur on travis but not locally. actual = state.to_h actual.delete(:escape_slash) + actual.delete(:strict) assert_equal({ :allow_nan => false, :array_nl => "\n", @@ -183,6 +184,7 @@ def test_fast_state # seems to occur on travis but not locally. actual = state.to_h actual.delete(:escape_slash) + actual.delete(:strict) assert_equal({ :allow_nan => false, :array_nl => "", From e69410d1ef8a10e4d3bc802b32fa4a62f0d26cad Mon Sep 17 00:00:00 2001 From: Peter Ohler Date: Sun, 3 Dec 2023 20:28:30 -0500 Subject: [PATCH 4/6] Remove :script_safe from json test --- test/json_gem/json_generator_test.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/json_gem/json_generator_test.rb b/test/json_gem/json_generator_test.rb index 75db3c12..be20b17c 100755 --- a/test/json_gem/json_generator_test.rb +++ b/test/json_gem/json_generator_test.rb @@ -143,6 +143,7 @@ def test_pretty_state actual = state.to_h actual.delete(:escape_slash) actual.delete(:strict) + actual.delete(:script_safe) assert_equal({ :allow_nan => false, :array_nl => "\n", @@ -164,6 +165,7 @@ def test_safe_state actual = state.to_h actual.delete(:escape_slash) actual.delete(:strict) + actual.delete(:script_safe) assert_equal({ :allow_nan => false, :array_nl => "", @@ -185,6 +187,7 @@ def test_fast_state actual = state.to_h actual.delete(:escape_slash) actual.delete(:strict) + actual.delete(:script_safe) assert_equal({ :allow_nan => false, :array_nl => "", From 8f5e5fbf2c770f9d3a5c930fce509d1447c59faa Mon Sep 17 00:00:00 2001 From: Peter Ohler Date: Wed, 6 Dec 2023 17:38:02 -0500 Subject: [PATCH 5/6] Update release date --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 294470b5..7ab53db6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # CHANGELOG -## 3.16.2 - 2023-11-21 +## 3.16.2 - 2023-12-06 - Fixed documentation formatting. From 5ace26994142bdee01af381ae7f575af290ca423 Mon Sep 17 00:00:00 2001 From: Peter Ohler Date: Wed, 6 Dec 2023 17:54:10 -0500 Subject: [PATCH 6/6] clang-format --- ext/oj/usual.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/oj/usual.c b/ext/oj/usual.c index c3b10160..1a1d0d17 100644 --- a/ext/oj/usual.c +++ b/ext/oj/usual.c @@ -602,7 +602,7 @@ static VALUE result(ojParser p) { return *d->vhead; } if (d->raise_on_empty) { - rb_raise(oj_parse_error_class, "empty string"); + rb_raise(oj_parse_error_class, "empty string"); } return Qnil; }