json-streamer 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +26 -6
- data/lib/json/streamer.rb +6 -9
- data/lib/json/streamer/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 228fd4a492c65890dea98aeb1b2d8f5e0346704a
|
4
|
+
data.tar.gz: ea930d766ae7c7c5f8ef3d1ea15cc4f0af23d012
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
#
|
48
|
-
streamer.get(nesting_level:
|
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"
|
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
|
-
|
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
|
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
|