Skip to content

Commit cd51557

Browse files
committed
Fix duplicated key warning location
Followup: #818 Now the warning should point at the `JSON.parse` caller, and not inside the json gem itself.
1 parent bea97e0 commit cd51557

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

ext/json/ext/parser/parser.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,8 @@ static void emit_parse_warning(const char *message, JSON_ParserState *state)
422422
long line, column;
423423
cursor_position(state, &line, &column);
424424

425-
rb_warn("%s at line %ld column %ld", message, line, column);
425+
VALUE warning = rb_sprintf("%s at line %ld column %ld", message, line, column);
426+
rb_funcall(mJSON, rb_intern("deprecation_warning"), 1, warning);
426427
}
427428

428429
#define PARSE_ERROR_FRAGMENT_LEN 32

lib/json/common.rb

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def array_class_proc(array_class, on_load)
4848
end
4949
end
5050

51-
# TODO: exctract :create_additions support to another gem for version 3.0
51+
# TODO: extract :create_additions support to another gem for version 3.0
5252
def create_additions_proc(opts)
5353
if opts[:symbolize_names]
5454
raise ArgumentError, "options :symbolize_names and :create_additions cannot be used in conjunction"
@@ -87,31 +87,32 @@ def create_additions_proc(opts)
8787
opts
8888
end
8989

90-
GEM_ROOT = File.expand_path("../../../", __FILE__) + "/"
9190
def create_additions_warning
92-
message = "JSON.load implicit support for `create_additions: true` is deprecated " \
91+
JSON.deprecation_warning "JSON.load implicit support for `create_additions: true` is deprecated " \
9392
"and will be removed in 3.0, use JSON.unsafe_load or explicitly " \
9493
"pass `create_additions: true`"
94+
end
95+
end
96+
end
9597

96-
uplevel = 4
97-
caller_locations(uplevel, 10).each do |frame|
98-
if frame.path.nil? || frame.path.start_with?(GEM_ROOT) || frame.path.end_with?("/truffle/cext_ruby.rb", ".c")
99-
uplevel += 1
100-
else
101-
break
102-
end
103-
end
104-
105-
if RUBY_VERSION >= "3.0"
106-
warn(message, uplevel: uplevel - 1, category: :deprecated)
98+
class << self
99+
def deprecation_warning(message, uplevel = 4) # :nodoc:
100+
gem_root = File.expand_path("../../../", __FILE__) + "/"
101+
caller_locations(uplevel, 10).each do |frame|
102+
if frame.path.nil? || frame.path.start_with?(gem_root) || frame.path.end_with?("/truffle/cext_ruby.rb", ".c")
103+
uplevel += 1
107104
else
108-
warn(message, uplevel: uplevel - 1)
105+
break
109106
end
110107
end
108+
109+
if RUBY_VERSION >= "3.0"
110+
warn(message, uplevel: uplevel - 1, category: :deprecated)
111+
else
112+
warn(message, uplevel: uplevel - 1)
113+
end
111114
end
112-
end
113115

114-
class << self
115116
# :call-seq:
116117
# JSON[object] -> new_array or new_string
117118
#

test/json/json_parser_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,12 @@ def test_parse_duplicate_key
346346
assert_equal expected_sym, parse('{"a": 1, "a": 2}', symbolize_names: true)
347347
end
348348

349+
if RUBY_ENGINE == 'RUBY_ENGINE'
350+
assert_deprecated_warning(/#{File.basename(__FILE__)}\:#{__LINE__ + 1}/) do
351+
assert_equal expected, parse('{"a": 1, "a": 2}')
352+
end
353+
end
354+
349355
unless RUBY_ENGINE == 'jruby'
350356
assert_raise(ParserError) do
351357
fake_key = Object.new

0 commit comments

Comments
 (0)