From c19c0463722154f6fd54aa852bfc50fbaf8556d1 Mon Sep 17 00:00:00 2001 From: Alberto Diaz Date: Thu, 15 Oct 2026 13:38:00 +0200 Subject: [PATCH] Avoid deprecated multi_json APIs while retaining legacy compatibility --- jsonpath.gemspec | 2 +- lib/jsonpath.rb | 8 +++++++- test/test_jsonpath.rb | 22 +++++++++++++++++++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/jsonpath.gemspec b/jsonpath.gemspec index 9ecd57a..f9e8782 100644 --- a/jsonpath.gemspec +++ b/jsonpath.gemspec @@ -18,7 +18,7 @@ Gem::Specification.new do |s| s.licenses = ['MIT'] # dependencies - s.add_runtime_dependency 'multi_json' + s.add_runtime_dependency 'multi_json', '>= 1.3' s.add_development_dependency 'bundler' s.add_development_dependency 'code_stats' s.add_development_dependency 'minitest', '~> 2.2.0' diff --git a/lib/jsonpath.rb b/lib/jsonpath.rb index 0388f62..68a4cb2 100644 --- a/lib/jsonpath.rb +++ b/lib/jsonpath.rb @@ -142,7 +142,13 @@ def self.for(obj_or_str) private def self.process_object(obj_or_str, opts = {}) - obj_or_str.is_a?(String) ? MultiJson.decode(obj_or_str, max_nesting: opts[:max_nesting]) : obj_or_str + return obj_or_str unless obj_or_str.is_a?(String) + + if defined?(MultiJSON) + MultiJSON.parse(obj_or_str, max_nesting: opts[:max_nesting]) + else + MultiJson.load(obj_or_str, max_nesting: opts[:max_nesting]) + end end def deep_clone diff --git a/test/test_jsonpath.rb b/test/test_jsonpath.rb index 80fa0bb..bd2bb19 100644 --- a/test/test_jsonpath.rb +++ b/test/test_jsonpath.rb @@ -4,6 +4,8 @@ require 'phocus' require 'jsonpath' require 'json' +require 'open3' +require 'rbconfig' class TestJsonpath < MiniTest::Unit::TestCase def setup @@ -17,6 +19,23 @@ def test_bracket_matching assert_equal [9], JsonPath.new('$.store.book[0].price').on(@object) end + def test_parse_string_without_deprecation_warnings + # Use a fresh process because newer multi_json releases warn only once per API. + code = <<~RUBY + gem 'multi_json', '#{Gem.loaded_specs['multi_json'].version}' + require 'jsonpath' + puts JsonPath.new('$.credential.access_token').first( + '{"credential":{"access_token":"example-token"}}' + ) + RUBY + stdout, stderr, status = Open3.capture3( + RbConfig.ruby, '-W2', '-I', File.expand_path('../lib', __dir__), '-e', code + ) + assert status.success?, stderr + assert_equal "example-token\n", stdout + assert_equal '', stderr + end + def test_lookup_direct_path assert_equal 7, JsonPath.new('$.store.*').on(@object).first['book'].size end @@ -1189,7 +1208,8 @@ def test_raise_max_nesting_error } }.to_json - assert_raises(MultiJson::ParseError) { JsonPath.new('$.a', max_nesting: 1).on(json) } + json_parser = defined?(MultiJSON) ? MultiJSON : MultiJson + assert_raises(json_parser::ParseError) { JsonPath.new('$.a', max_nesting: 1).on(json) } end def test_linefeed_in_path_error