Skip to content

Commit 9e15849

Browse files
committed
fix: parse
1 parent 4ab2065 commit 9e15849

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

ext/json_scanner/json_scanner.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ VALUE scan(int argc, VALUE *argv, VALUE self)
753753
yajl_status stat;
754754
scan_ctx *ctx;
755755
int free_ctx = true;
756-
VALUE err_msg = Qnil, bytes_consumed, result, roots_info_result = Qundef;
756+
VALUE err_msg = Qnil, bytes_consumed = Qnil, result, roots_info_result = Qundef;
757757
// Turned out callbacks can't raise exceptions
758758
// VALUE callback_err;
759759
#if RUBY_API_VERSION_MAJOR > 2 || (RUBY_API_VERSION_MAJOR == 2 && RUBY_API_VERSION_MINOR >= 7)

lib/json_scanner.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class Error < StandardError; end
1313
ALLOWED_OPTS = %i[verbose_error allow_comments dont_validate_strings allow_multiple_values
1414
allow_trailing_garbage allow_partial_values symbolize_path_keys].freeze
1515
private_constant :ALLOWED_OPTS
16+
STUB = :stub
17+
private_constant :STUB
1618

1719
def self.parse(json_str, config_or_path_ary, **opts)
1820
# with_path and with_roots_info is set here
@@ -22,22 +24,29 @@ def self.parse(json_str, config_or_path_ary, **opts)
2224

2325
results, roots = scan(json_str, config_or_path_ary, **opts, with_path: true, with_roots_info: true)
2426

27+
res = process_results(json_str, results, roots, opts[:symbolize_path_keys])
28+
29+
opts[:allow_multiple_values] ? res : res.first
30+
end
31+
32+
def self.process_results(json_str, results, roots, symbolize_names)
2533
# stubs are symbols, so they can be distinguished from real values
2634
res = roots.map(&:first)
2735
# results for different path matchers can overlap, in that case we will simply parse more than one time,
2836
# but there shouln't be any surprises in the behavior
2937
results.each do |result|
30-
process_result(res, result, roots, json_str, opts[:symbolize_path_keys])
38+
process_result(res, result, roots, json_str, symbolize_names)
3139
end
32-
33-
opts[:allow_multiple_values] ? res : res.first
40+
res
3441
end
3542

43+
private_class_method :process_results
44+
3645
def self.process_result(res, result, roots, json_str, symbolize_names)
3746
current_root_index = 0
3847
next_root = roots[1]
3948
result.each do |path, (begin_pos, end_pos, _type)|
40-
if next_root && begin_pos >= next_root[1]
49+
while next_root && begin_pos >= next_root[1]
4150
current_root_index += 1
4251
next_root = roots[current_root_index + 1]
4352
end

spec/json_scanner_spec.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -320,17 +320,17 @@
320320
end
321321
end
322322

323-
# it "extracts values" do
324-
# json_str = 'null false {"a": 1, "b": [0,1], "c": 42} 4.2 {"b": [1,2,3]} {} 1 []'
325-
# json_selector = [["a"], ["b", 2]]
326-
# expect(
327-
# described_class.parse(json_str, json_selector, allow_multiple_values: true),
328-
# ).to eq(
329-
# [
330-
# :null, :boolean, { "a" => 1 }, :number, { "b" => [nil, nil, 3] }, :object, :number, :array,
331-
# ]
332-
# )
333-
# end
323+
it "extracts values" do
324+
json_str = 'null false {"a": 1, "b": [0,1], "c": 42} 4.2 {"b": [1,2,3]} {} 1 []'
325+
json_selector = [["a"], ["b", 2]]
326+
expect(
327+
described_class.parse(json_str, json_selector, allow_multiple_values: true),
328+
).to eq(
329+
[
330+
:null, :boolean, { "a" => 1 }, :number, { "b" => [nil, nil, 3] }, :object, :number, :array,
331+
],
332+
)
333+
end
334334

335335
describe described_class::Config do
336336
it "saves state" do

0 commit comments

Comments
 (0)