lib/json/streamer.rb in json-streamer-0.4.0 vs lib/json/streamer.rb in json-streamer-0.5.0
- old
+ new
@@ -22,26 +22,27 @@
@parser.start_array {start_array}
@parser.key {|k| key(k)}
end
- def get(nesting_level:-1, key:nil)
+ # Callbacks containing yield has be defined in the method called via block
+ def get(nesting_level:-1, key:nil, yield_values:true)
@yield_nesting_level = nesting_level
@wanted_key = key
+ @yield_values = yield_values
@parser.value do |v|
if @aggregator[@current_nesting_level].kind_of? Array
@aggregator[@current_nesting_level] << v
else
@aggregator[@current_nesting_level][@current_key] = v
- if yield_value?
+ if @yield_values and yield_value?
yield v
end
end
end
- # Callback containing yield has be defined in the method called via block
@parser.end_object do
if yield_object?
yield @aggregator[@current_nesting_level].clone
# TODO probably can be faster than reject!{true}
@aggregator[@current_nesting_level].reject!{true}
@@ -72,11 +73,11 @@
def yield_object?
@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?
- @wanted_key == @current_key
+ (@current_nesting_level + 1).eql? @yield_nesting_level or @wanted_key == @current_key
end
def start_object
@temp_aggregator_keys[@current_nesting_level] = @current_key
@current_nesting_level += 1
@@ -87,15 +88,11 @@
@temp_aggregator_keys[@current_nesting_level] = @current_key
@current_nesting_level += 1
@aggregator[@current_nesting_level] = []
end
- def key k
+ def key(k)
@current_key = k
- end
-
- def value v
-
end
def merge_up
return if @current_nesting_level == 0
previous_nesting_level = @current_nesting_level - 1