Skip to content

Commit

Permalink
Use JSON.parse instead of JSON.load
Browse files Browse the repository at this point in the history
  • Loading branch information
mullermp committed Mar 7, 2022
1 parent 754bf30 commit f517de4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
1.6.0 (2022-02-14)
Unreleased Changes
------------------

* Issue - Use `JSON.parse` instead of `JSON.load`.

1.6.0 (2022-02-14)
------------------

* Feature - Add support for string comparisions.
* Feature - Add support for string comparisons.

1.5.0 (2022-01-10)
------------------
Expand Down Expand Up @@ -230,4 +232,3 @@
------------------

* Passing all of the JMESPath compliance tests.

2 changes: 1 addition & 1 deletion bin/jmespath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
require 'json'

expression = ARGV[0]
json = JSON.load(STDIN.read)
json = JSON.parse(STDIN.read)

$stdout.puts(JSON.dump(JMESPath.search(expression, json)))
4 changes: 2 additions & 2 deletions lib/jmespath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ def search(expression, data, runtime_options = {})
data = case data
when Hash, Struct then data # check for most common case first
when Pathname then load_json(data)
when IO, StringIO then JSON.load(data.read)
when IO, StringIO then JSON.parse(data.read)
else data
end
Runtime.new(runtime_options).search(expression, data)
end

# @api private
def load_json(path)
JSON.load(File.open(path, 'r', encoding: 'UTF-8') { |f| f.read })
JSON.parse(File.open(path, 'r', encoding: 'UTF-8') { |f| f.read })
end

end
Expand Down
22 changes: 11 additions & 11 deletions lib/jmespath/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,12 @@ def inside(chars, delim, type)
# Certain versions of Ruby and of the pure_json gem not support loading
# scalar JSON values, such a numbers, booleans, strings, etc. These
# simple values must be first wrapped inside a JSON object before calling
# `JSON.load`.
# `JSON.parse`.
#
# # works in most JSON versions, raises in some versions
# JSON.load("true")
# JSON.load("123")
# JSON.load("\"abc\"")
# JSON.parse("true")
# JSON.parse("123")
# JSON.parse("\"abc\"")
#
# This is an known issue for:
#
Expand All @@ -317,12 +317,12 @@ def inside(chars, delim, type)
# causes issues in environments that cannot compile the gem. We previously
# had a direct dependency on `json_pure`, but this broke with the v2 update.
#
# This method allows us to detect how the `JSON.load` behaves so we know
# This method allows us to detect how the `JSON.parse` behaves so we know
# if we have to wrap scalar JSON values to parse them or not.
# @api private
def self.requires_wrapping?
begin
JSON.load('false')
JSON.parse('false')
rescue JSON::ParserError
true
end
Expand All @@ -332,12 +332,12 @@ def self.requires_wrapping?
def parse_json(token, quoted = false)
begin
if quoted
token.value = JSON.load("{\"value\":#{token.value}}")['value']
token.value = JSON.parse("{\"value\":#{token.value}}")['value']
else
begin
token.value = JSON.load("{\"value\":#{token.value}}")['value']
token.value = JSON.parse("{\"value\":#{token.value}}")['value']
rescue
token.value = JSON.load(sprintf('{"value":"%s"}', token.value.lstrip))['value']
token.value = JSON.parse(sprintf('{"value":"%s"}', token.value.lstrip))['value']
end
end
rescue JSON::ParserError
Expand All @@ -349,9 +349,9 @@ def parse_json(token, quoted = false)
def parse_json(token, quoted = false)
begin
if quoted
token.value = JSON.load(token.value)
token.value = JSON.parse(token.value)
else
token.value = JSON.load(token.value) rescue JSON.load(sprintf('"%s"', token.value.lstrip))
token.value = JSON.parse(token.value) rescue JSON.parse(sprintf('"%s"', token.value.lstrip))
end
rescue JSON::ParserError
token.type = T_UNKNOWN
Expand Down

0 comments on commit f517de4

Please sign in to comment.