<commit_before># Json::Streamer

Utility to support JSON streaming allowing you to get objects based on various criteria.
Useful for e.g. streaming objects from a JSON array.

This gem will basically spare you the need to define you own callbacks when parsing JSON stream.
Streaming is useful for
- big files that not fit in the memory (or you'd rather avoid the risk)
- files read in chunks (e.g. arriving over network)
- cases where you expect some issue with the file (e.g. losing connection to source, invalid data at some point) but would like to get as much data as possible anyway

Regarding performance:
The gem uses JSON::Stream's events in the background. It was chosen because it's a pure Ruby parser.
A similar implementation can be done using the ~10 times faster Yajl::FFI gem that is dependent on the native YAJL library.
I did not measure the performance of my implementation on top of these libraries.

I do not recommend this or any of the gems mentioned above if you don't need streaming.

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'json-streamer'
```

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install json-streamer

## Usage

```ruby
require 'json/streamer'

# Get a JsonStreamer object that will parse file_stream by chunks of 500
# Default chunk size in 1000
streamer = Json::Streamer::JsonStreamer.new(file_stream, 500)
```

```ruby
# Get objects based on nesting level
# First level will give you the full JSON, second level will give you objects within full JSON object, etc.
streamer.get_objects_from_level(2)
```

Getting second level objects on the JSON below will yield you 2 empty objects

```json
{<commit_msg>Update usage example<commit_after># Json::Streamer

Utility to support JSON streaming allowing you to get objects based on various criteria.
Useful for e.g. streaming objects from a JSON array.

This gem will basically spare you the need to define you own callbacks when parsing JSON stream.
Streaming is useful for
- big files that not fit in the memory (or you'd rather avoid the risk)
- files read in chunks (e.g. arriving over network)
- cases where you expect some issue with the file (e.g. losing connection to source, invalid data at some point) but would like to get as much data as possible anyway

Regarding performance:
The gem uses JSON::Stream's events in the background. It was chosen because it's a pure Ruby parser.
A similar implementation can be done using the ~10 times faster Yajl::FFI gem that is dependent on the native YAJL library.
I did not measure the performance of my implementation on top of these libraries.

I do not recommend this or any of the gems mentioned above if you don't need streaming.

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'json-streamer'
```

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install json-streamer

## Usage

```ruby
require 'json/streamer'

# Get a JsonStreamer object that will parse file_stream by chunks of 500
# Default chunk size in 1000
streamer = Json::Streamer::JsonStreamer.new(file_stream, 500)
```

```ruby
# Get objects based on nesting level
# First level will give you the full JSON, second level will give you objects within full JSON object, etc.
streamer.get_objects_from_level(2).each do |object|
    p object
end
```

Getting second level objects on the JSON below will yield you 2 empty objects

```json
{
{'fraction_non_alphanumeric': 0.05769788344447666, 'fraction_numerical': 0.008118295158016816, 'mean_word_length': 4.267175572519084, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6128992', 'n_tokens_mistral': 962, 'n_tokens_neox': 891, 'n_words': 523}