json-streamer 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 14eacb500abba80f385b5f334b44801820cadc61
4
- data.tar.gz: 5e6c6fd650b13ca12a8f541d4a80f34bc73a7e1d
3
+ metadata.gz: 228fd4a492c65890dea98aeb1b2d8f5e0346704a
4
+ data.tar.gz: ea930d766ae7c7c5f8ef3d1ea15cc4f0af23d012
5
5
  SHA512:
6
- metadata.gz: c868d1324dd546e77eeb798df4716dc8cecea27f200643de75fe8009f4599c49c59c0a9744dbfb6f9c8e1ada67d52c46df3cc081eacb5faa8f2ddb32db0daa17
7
- data.tar.gz: c762bbb47ed0f1bd0a6c8a3ea0a3b2265db28ab8f0676714a643a645e35804423a9358949994fb4c6973e7ae1a0acd33ac002f0c2bf75267854d0bb4c8527cd2
6
+ metadata.gz: 558ba471b5a0ad18eccca5250bcd15c7e2e48d58040ac83cd88f032db9c25a2b18b55f458507d9b762d6f129b87fd57daff7a67e5e91e55c2a27b97ade324e18
7
+ data.tar.gz: 6b286e416488996dec24d18ef3976d99f41585ee62ac23172c39de21834eb2ed06fe3b977dd0f0e92d9de0e42d28670bebaee7423a359c34f1bf0c9c67c6ab24
data/README.md CHANGED
@@ -44,20 +44,20 @@ streamer = Json::Streamer::JsonStreamer.new(file_stream, 500)
44
44
 
45
45
  ```ruby
46
46
  # Get objects based on nesting level
47
- # First level will give you the full JSON, second level will give you objects within full JSON object, etc.
48
- streamer.get(nesting_level:2).each do |object|
47
+ # Level zero will give you the full JSON, first level will give you data within full JSON object, etc.
48
+ streamer.get(nesting_level:1).each do |object|
49
49
  p object
50
50
  end
51
51
  ```
52
52
 
53
- Getting second level objects on the JSON below will yield you 2 empty objects
54
-
55
53
  ```json
56
54
  {
57
55
  "object1": {},
58
56
  "object2": {}
59
57
  }
58
+
60
59
  =>
60
+
61
61
  {}
62
62
  {}
63
63
  ```
@@ -77,15 +77,35 @@ end
77
77
  "key" : "value",
78
78
  "obj2" : {
79
79
  "key" : {
80
- "key" : value"
80
+ "key" : "value"
81
81
  }
82
82
  }
83
83
  }
84
+
84
85
  =>
86
+
85
87
  "value"
86
88
  "value"
87
89
  "value"
88
- {"key"=>"value"}
90
+ {"key" : "value"}
91
+ ```
92
+
93
+ ```ruby
94
+ # You can also skip values if you'd only like to get objects and arrays
95
+ streamer.get(nesting_level:1, yield_values:false).each do |object|
96
+ p object
97
+ end
98
+ ```
99
+
100
+ ```json
101
+ {
102
+ "obj1" : {}
103
+ "key" : "value"
104
+ }
105
+
106
+ =>
107
+
108
+ {}
89
109
  ```
90
110
 
91
111
  Check the unit tests for more examples.
data/lib/json/streamer.rb CHANGED
@@ -24,22 +24,23 @@ module Json
24
24
 
25
25
  end
26
26
 
27
- def get(nesting_level:-1, key:nil)
27
+ # Callbacks containing yield has be defined in the method called via block
28
+ def get(nesting_level:-1, key:nil, yield_values:true)
28
29
  @yield_nesting_level = nesting_level
29
30
  @wanted_key = key
31
+ @yield_values = yield_values
30
32
 
31
33
  @parser.value do |v|
32
34
  if @aggregator[@current_nesting_level].kind_of? Array
33
35
  @aggregator[@current_nesting_level] << v
34
36
  else
35
37
  @aggregator[@current_nesting_level][@current_key] = v
36
- if yield_value?
38
+ if @yield_values and yield_value?
37
39
  yield v
38
40
  end
39
41
  end
40
42
  end
41
43
 
42
- # Callback containing yield has be defined in the method called via block
43
44
  @parser.end_object do
44
45
  if yield_object?
45
46
  yield @aggregator[@current_nesting_level].clone
@@ -74,7 +75,7 @@ module Json
74
75
  end
75
76
 
76
77
  def yield_value?
77
- @wanted_key == @current_key
78
+ (@current_nesting_level + 1).eql? @yield_nesting_level or @wanted_key == @current_key
78
79
  end
79
80
 
80
81
  def start_object
@@ -89,14 +90,10 @@ module Json
89
90
  @aggregator[@current_nesting_level] = []
90
91
  end
91
92
 
92
- def key k
93
+ def key(k)
93
94
  @current_key = k
94
95
  end
95
96
 
96
- def value v
97
-
98
- end
99
-
100
97
  def merge_up
101
98
  return if @current_nesting_level == 0
102
99
  previous_nesting_level = @current_nesting_level - 1
@@ -1,5 +1,5 @@
1
1
  module Json
2
2
  module Streamer
3
- VERSION = "0.4.0"
3
+ VERSION = "0.5.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-streamer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Csaba Apagyi