Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ext/json/ext/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 18 additions & 17 deletions lib/json/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
#
Expand Down
6 changes: 6 additions & 0 deletions test/json/json_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading