From 08d9b5f820d079aac9f11d0604ef29fe428801a7 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 12 Dec 2017 15:27:28 -0800 Subject: [PATCH] Make projection parser behave the same as regular parser The projection parser should raise the same exceptions that the regular JSON parser raises when parsing a bad document --- ext/yajl/yajl_ext.c | 2 +- spec/projection/projection.rb | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/ext/yajl/yajl_ext.c b/ext/yajl/yajl_ext.c index 3bcb8ae5..ebc7969a 100644 --- a/ext/yajl/yajl_ext.c +++ b/ext/yajl/yajl_ext.c @@ -792,7 +792,7 @@ static void rb_yajl_projector_ignore_value(yajl_event_stream_t parser) { return; } - rb_raise(cStandardError, "unknown value type to ignore %s", yajl_tok_name(value_event.token)); + rb_raise(cParseError, "unknown value type to ignore %s", yajl_tok_name(value_event.token)); } /* diff --git a/spec/projection/projection.rb b/spec/projection/projection.rb index 0e15f750..9558507c 100644 --- a/spec/projection/projection.rb +++ b/spec/projection/projection.rb @@ -42,6 +42,39 @@ }.to raise_error(Yajl::ParseError) end + it "should behave the same way as the regular parser on bad tokens like comma" do + bad_json = '{"name": "keith", "age":, 27}' + stream = StringIO.new(bad_json) + projector = Yajl::Projector.new(stream) + expect { + projector.project({"name" => nil}) + }.to raise_error(capture_exception_for(bad_json).class) + end + + it "should behave the same way as the regular parser on bad tokens like colon" do + bad_json = '{"name": "keith", "age":: 27}' + stream = StringIO.new(bad_json) + projector = Yajl::Projector.new(stream) + expect { + projector.project({"name" => nil}) + }.to raise_error(capture_exception_for(bad_json).class) + end + + it "should behave the same way as the regular parser on not enough json" do + bad_json = '{"name": "keith", "age":' + stream = StringIO.new(bad_json) + projector = Yajl::Projector.new(stream) + expect { + projector.project({"name" => nil}) + }.to raise_error(capture_exception_for(bad_json).class) + end + + def capture_exception_for(bad_json) + Yajl::Parser.new.parse(bad_json) + rescue Exception => e + e + end + def project(schema, over: "", json: nil, stream: nil) if stream.nil? if json.nil?