Skip to content

Commit

Permalink
Fix issue when simple array values (i.e. not hashes) were not returned
Browse files Browse the repository at this point in the history
  • Loading branch information
thisismydesign committed May 24, 2017
1 parent 8e4af0d commit 2913823
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
11 changes: 7 additions & 4 deletions lib/json/streamer/json_streamer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def initialize(file_io, chunk_size = 1000)
@parser.start_object {start_object}
@parser.start_array {start_array}
@parser.key {|k| key(k)}

end

# Callbacks containing `yield` have to be defined in the method called via block otherwise yield won't work
Expand All @@ -30,7 +29,11 @@ def get(nesting_level:-1, key:nil, yield_values:true)

@parser.value do |v|
if @aggregator[@current_nesting_level].kind_of? Array
@aggregator[@current_nesting_level] << v
if yield_values and yield_value?(yield_nesting_level)
yield v
else
@aggregator[@current_nesting_level] << v
end
else
@aggregator[@current_nesting_level][@current_key] = v
if yield_values and yield_value?(yield_nesting_level, wanted_key)
Expand Down Expand Up @@ -70,8 +73,8 @@ def yield_object?(yield_nesting_level, wanted_key)
@current_nesting_level.eql? yield_nesting_level or (not wanted_key.nil? and wanted_key == @temp_aggregator_keys[@current_nesting_level-1])
end

def yield_value?(yield_nesting_level, wanted_key)
(@current_nesting_level + 1).eql? yield_nesting_level or wanted_key == @current_key
def yield_value?(yield_nesting_level, wanted_key = nil)
(@current_nesting_level + 1).eql? yield_nesting_level or (not wanted_key.nil? and wanted_key == @current_key)
end

def start_object
Expand Down
19 changes: 18 additions & 1 deletion spec/json/streamer/json_streamer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,25 @@
end

expect(objects.length).to eq(1)
expect(objects[0]).to eq([@example_value, @example_value])
end
end

context '1th level of JSON array' do
it 'should yield array elements' do

hash = [@example_value, @example_value]
json_file_mock = StringIO.new(JSON.generate(hash))
streamer = Json::Streamer::JsonStreamer.new(json_file_mock, 10)

objects = []
streamer.get(nesting_level:1) do |object|
objects.push(object)
end

expect(objects.length).to eq(2)
objects.each do |element|
expect(element).to eq([@example_value, @example_value])
expect(element).to eq(@example_value)
end
end
end
Expand Down

0 comments on commit 2913823

Please sign in to comment.