Skip to content

Commit f517de4

Browse files
committed
Use JSON.parse instead of JSON.load
1 parent 754bf30 commit f517de4

File tree

4 files changed

+18
-17
lines changed

4 files changed

+18
-17
lines changed

CHANGELOG.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
1.6.0 (2022-02-14)
1+
Unreleased Changes
22
------------------
33

4+
* Issue - Use `JSON.parse` instead of `JSON.load`.
5+
46
1.6.0 (2022-02-14)
57
------------------
68

7-
* Feature - Add support for string comparisions.
9+
* Feature - Add support for string comparisons.
810

911
1.5.0 (2022-01-10)
1012
------------------
@@ -230,4 +232,3 @@
230232
------------------
231233

232234
* Passing all of the JMESPath compliance tests.
233-

bin/jmespath.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
require 'json'
77

88
expression = ARGV[0]
9-
json = JSON.load(STDIN.read)
9+
json = JSON.parse(STDIN.read)
1010

1111
$stdout.puts(JSON.dump(JMESPath.search(expression, json)))

lib/jmespath.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ def search(expression, data, runtime_options = {})
2626
data = case data
2727
when Hash, Struct then data # check for most common case first
2828
when Pathname then load_json(data)
29-
when IO, StringIO then JSON.load(data.read)
29+
when IO, StringIO then JSON.parse(data.read)
3030
else data
3131
end
3232
Runtime.new(runtime_options).search(expression, data)
3333
end
3434

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

4040
end

lib/jmespath/lexer.rb

+11-11
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,12 @@ def inside(chars, delim, type)
298298
# Certain versions of Ruby and of the pure_json gem not support loading
299299
# scalar JSON values, such a numbers, booleans, strings, etc. These
300300
# simple values must be first wrapped inside a JSON object before calling
301-
# `JSON.load`.
301+
# `JSON.parse`.
302302
#
303303
# # works in most JSON versions, raises in some versions
304-
# JSON.load("true")
305-
# JSON.load("123")
306-
# JSON.load("\"abc\"")
304+
# JSON.parse("true")
305+
# JSON.parse("123")
306+
# JSON.parse("\"abc\"")
307307
#
308308
# This is an known issue for:
309309
#
@@ -317,12 +317,12 @@ def inside(chars, delim, type)
317317
# causes issues in environments that cannot compile the gem. We previously
318318
# had a direct dependency on `json_pure`, but this broke with the v2 update.
319319
#
320-
# This method allows us to detect how the `JSON.load` behaves so we know
320+
# This method allows us to detect how the `JSON.parse` behaves so we know
321321
# if we have to wrap scalar JSON values to parse them or not.
322322
# @api private
323323
def self.requires_wrapping?
324324
begin
325-
JSON.load('false')
325+
JSON.parse('false')
326326
rescue JSON::ParserError
327327
true
328328
end
@@ -332,12 +332,12 @@ def self.requires_wrapping?
332332
def parse_json(token, quoted = false)
333333
begin
334334
if quoted
335-
token.value = JSON.load("{\"value\":#{token.value}}")['value']
335+
token.value = JSON.parse("{\"value\":#{token.value}}")['value']
336336
else
337337
begin
338-
token.value = JSON.load("{\"value\":#{token.value}}")['value']
338+
token.value = JSON.parse("{\"value\":#{token.value}}")['value']
339339
rescue
340-
token.value = JSON.load(sprintf('{"value":"%s"}', token.value.lstrip))['value']
340+
token.value = JSON.parse(sprintf('{"value":"%s"}', token.value.lstrip))['value']
341341
end
342342
end
343343
rescue JSON::ParserError
@@ -349,9 +349,9 @@ def parse_json(token, quoted = false)
349349
def parse_json(token, quoted = false)
350350
begin
351351
if quoted
352-
token.value = JSON.load(token.value)
352+
token.value = JSON.parse(token.value)
353353
else
354-
token.value = JSON.load(token.value) rescue JSON.load(sprintf('"%s"', token.value.lstrip))
354+
token.value = JSON.parse(token.value) rescue JSON.parse(sprintf('"%s"', token.value.lstrip))
355355
end
356356
rescue JSON::ParserError
357357
token.type = T_UNKNOWN

0 commit comments

Comments
 (0)