Skip to content

Commit 4f91c7f

Browse files
authored
parser_json: use JSON.parse instead of .load (#4817)
**Which issue(s) this PR fixes**: Fixes # **What this PR does / why we need it**: Ref. #4813 (comment) Fix wrong usage of `JSON.load` method. We should use `JSON.parse` instead. JSON.load performs unnecessary deserialisation. ``` irb(main):001> JSON.load('{ "json_class": "String", "raw": [72, 101, 108, 108, 111] }') => "Hello" irb(main):002> JSON.parse('{ "json_class": "String", "raw": [72, 101, 108, 108, 111] }') => {"json_class" => "String", "raw" => [72, 101, 108, 108, 111]} ``` **Docs Changes**: **Release Note**: Signed-off-by: Shizuo Fujita <fujita@clear-code.com>
1 parent 1d5b0de commit 4f91c7f

File tree

3 files changed

+4
-4
lines changed

3 files changed

+4
-4
lines changed

lib/fluent/plugin/parser_json.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def configure_json_parser(name)
5454

5555
log&.info "Oj is not installed, and failing back to JSON for json parser"
5656
configure_json_parser(:json)
57-
when :json then [JSON.method(:load), JSON::ParserError]
57+
when :json then [JSON.method(:parse), JSON::ParserError]
5858
when :yajl then [Yajl.method(:load), Yajl::ParseError]
5959
else
6060
raise "BUG: unknown json parser specified: #{name}"

test/log/test_console_adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def test_args(level)
7373
def test_options(level)
7474
@console_logger.send(level, "subject", kwarg1: "opt1", kwarg2: "opt2")
7575
lines = @logdev.logs[0].split("\n")
76-
args = JSON.load(lines[1..].collect { |str| str.sub(/\s+\|/, "") }.join("\n"));
76+
args = JSON.parse(lines[1..].collect { |str| str.sub(/\s+\|/, "") }.join("\n"));
7777
assert_equal([
7878
1,
7979
"#{@timestamp_str} [#{level}]: 0.0s: subject",

test/plugin/test_parser_json.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def setup
1010

1111
sub_test_case "configure_json_parser" do
1212
data("oj", [:oj, [Oj.method(:load), Oj::ParseError]])
13-
data("json", [:json, [JSON.method(:load), JSON::ParserError]])
13+
data("json", [:json, [JSON.method(:parse), JSON::ParserError]])
1414
data("yajl", [:yajl, [Yajl.method(:load), Yajl::ParseError]])
1515
def test_return_each_loader((input, expected_return))
1616
result = @parser.instance.configure_json_parser(input)
@@ -28,7 +28,7 @@ def test_fall_back_oj_to_yajl_if_oj_not_available
2828

2929
result = @parser.instance.configure_json_parser(:oj)
3030

31-
assert_equal [JSON.method(:load), JSON::ParserError], result
31+
assert_equal [JSON.method(:parse), JSON::ParserError], result
3232
logs = @parser.logs.collect do |log|
3333
log.gsub(/\A\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} [-+]\d{4} /, "")
3434
end

0 commit comments

Comments
 (0)