summaryrefslogtreecommitdiff
path: root/ext/json
diff options
context:
space:
mode:
authorJean Boussier <jean.boussier@gmail.com>2025-07-27 14:31:40 +0200
committerHiroshi SHIBATA <hsbt@ruby-lang.org>2025-07-28 09:39:12 +0900
commitd0020d58f468b83e69b0494ffa83990db8f18d45 (patch)
tree8644b6f7437bee9691ab49763edcb941bdba8e2c /ext/json
parentb4ef5da70b26698b9da316daa36a5de3374b5270 (diff)
[ruby/json] Fix duplicated key warning location
Followup: https://github.com/ruby/json/pull/818 Now the warning should point at the `JSON.parse` caller, and not inside the json gem itself. https://github.com/ruby/json/commit/cd51557387
Diffstat (limited to 'ext/json')
-rw-r--r--ext/json/lib/json/common.rb35
-rw-r--r--ext/json/parser/parser.c3
2 files changed, 20 insertions, 18 deletions
diff --git a/ext/json/lib/json/common.rb b/ext/json/lib/json/common.rb
index 486ec62a58..69e436f190 100644
--- a/ext/json/lib/json/common.rb
+++ b/ext/json/lib/json/common.rb
@@ -48,7 +48,7 @@ module JSON
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 @@ module JSON
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/ext/json/parser/parser.c b/ext/json/parser/parser.c
index 8d72c18ace..ab9d6c205e 100644
--- a/ext/json/parser/parser.c
+++ b/ext/json/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