Skip to content
Draft
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
2 changes: 1 addition & 1 deletion jsonpath.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
8 changes: 7 additions & 1 deletion lib/jsonpath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 21 additions & 1 deletion test/test_jsonpath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
require 'phocus'
require 'jsonpath'
require 'json'
require 'open3'
require 'rbconfig'

class TestJsonpath < MiniTest::Unit::TestCase
def setup
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down