README.md in json-streamer-1.3.0 vs README.md in json-streamer-2.0.0

- old
+ new

@@ -78,11 +78,11 @@ "object2": {} } ``` Output: -```json +```ruby "first_level_value" {} ``` #### Get data based on key @@ -107,15 +107,15 @@ } } ``` Output: -```json +```ruby "value1" "value2" "value3" -{"desired_key" : "value3"} +{"desired_key" => "value3"} ``` #### Skip values ```ruby @@ -153,11 +153,11 @@ "obj1" : {"key" : "value"} } ``` Output: -```json +```ruby {:obj1=>{:key=>"value"}} ``` #### Passing IO later (EventMachine-style) @@ -175,9 +175,74 @@ ```ruby def receive_data(data) streamer << data end +``` + +#### Custom yield conditions + +[v2.0.0](https://github.com/thisismydesign/json-streamer/releases/tag/v2.0.0) introduces custom conditions which provide ultimate control over what to yield. + +The Conditions API exposes 3 callbacks: +- `yield_value` +- `yield_array` +- `yield_object` + +Each of them may be redefined. They are called once the corresponding data (value, array or object) is available. They should return whether the data should be yielded for the outside. They receive the data and the `aggregator` as parameters. + +The `aggregator` exposes data about the current state of the partly parsed JSON such as: +- `level` - current level +- `key` - current key +- `value` - current value +- `key_for_level(level)` - key for custom level +- `value_for_level(level)` - value for custom level +- `get` - the raw data (in a custom format) + +Example usage (inspired by [this issue](https://github.com/thisismydesign/json-streamer/issues/7#issuecomment-330232484)): + +```ruby +conditions = Json::Streamer::Conditions.new +conditions.yield_value = ->(aggregator:, value:) { false } +conditions.yield_array = ->(aggregator:, array:) { false } +conditions.yield_object = lambda do |aggregator:, object:| + aggregator.level.eql?(2) && aggregator.key_for_level(1).eql?('items1') +end + +streamer.get_with_conditions(conditions) do |object| + p object +end +``` + +Input: + +```ruby +{ + "other": "stuff", + "items1": [ + { + "key1": "value" + }, + { + "key2": "value" + } + ], + "items2": [ + { + "key3": "value" + }, + { + "key4": "value" + } + ] +} +``` + +Output: + +```ruby +{"key1"=>"value"} +{"key2"=>"value"} ``` ### Legacy API (pre-v1.2) This functionality is deprecated but kept for compatibility reasons.