diff --git a/ext/json/ext/parser/parser.c b/ext/json/ext/parser/parser.c index 8d72c18ac..ab9d6c205 100644 --- a/ext/json/ext/parser/parser.c +++ b/ext/json/ext/parser/parser.c @@ -422,7 +422,8 @@ static void emit_parse_warning(const char *message, JSON_ParserState *state) long line, column; cursor_position(state, &line, &column); - rb_warn("%s at line %ld column %ld", message, line, column); + VALUE warning = rb_sprintf("%s at line %ld column %ld", message, line, column); + rb_funcall(mJSON, rb_intern("deprecation_warning"), 1, warning); } #define PARSE_ERROR_FRAGMENT_LEN 32 diff --git a/lib/json/common.rb b/lib/json/common.rb index 486ec62a5..69e436f19 100644 --- a/lib/json/common.rb +++ b/lib/json/common.rb @@ -48,7 +48,7 @@ def array_class_proc(array_class, on_load) end end - # TODO: exctract :create_additions support to another gem for version 3.0 + # TODO: extract :create_additions support to another gem for version 3.0 def create_additions_proc(opts) if opts[:symbolize_names] raise ArgumentError, "options :symbolize_names and :create_additions cannot be used in conjunction" @@ -87,31 +87,32 @@ def create_additions_proc(opts) opts end - GEM_ROOT = File.expand_path("../../../", __FILE__) + "/" def create_additions_warning - message = "JSON.load implicit support for `create_additions: true` is deprecated " \ + JSON.deprecation_warning "JSON.load implicit support for `create_additions: true` is deprecated " \ "and will be removed in 3.0, use JSON.unsafe_load or explicitly " \ "pass `create_additions: true`" + end + end + end - uplevel = 4 - caller_locations(uplevel, 10).each do |frame| - if frame.path.nil? || frame.path.start_with?(GEM_ROOT) || frame.path.end_with?("/truffle/cext_ruby.rb", ".c") - uplevel += 1 - else - break - end - end - - if RUBY_VERSION >= "3.0" - warn(message, uplevel: uplevel - 1, category: :deprecated) + class << self + def deprecation_warning(message, uplevel = 4) # :nodoc: + gem_root = File.expand_path("../../../", __FILE__) + "/" + caller_locations(uplevel, 10).each do |frame| + if frame.path.nil? || frame.path.start_with?(gem_root) || frame.path.end_with?("/truffle/cext_ruby.rb", ".c") + uplevel += 1 else - warn(message, uplevel: uplevel - 1) + break end end + + if RUBY_VERSION >= "3.0" + warn(message, uplevel: uplevel - 1, category: :deprecated) + else + warn(message, uplevel: uplevel - 1) + end end - end - class << self # :call-seq: # JSON[object] -> new_array or new_string # diff --git a/test/json/json_parser_test.rb b/test/json/json_parser_test.rb index be13c5ed3..6455d2971 100644 --- a/test/json/json_parser_test.rb +++ b/test/json/json_parser_test.rb @@ -346,6 +346,12 @@ def test_parse_duplicate_key assert_equal expected_sym, parse('{"a": 1, "a": 2}', symbolize_names: true) end + if RUBY_ENGINE == 'RUBY_ENGINE' + assert_deprecated_warning(/#{File.basename(__FILE__)}\:#{__LINE__ + 1}/) do + assert_equal expected, parse('{"a": 1, "a": 2}') + end + end + unless RUBY_ENGINE == 'jruby' assert_raise(ParserError) do fake_key = Object.new