yaji 0.0.2 → 0.0.3
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.
- data/HISTORY.markdown +4 -0
- data/README.markdown +15 -2
- data/ext/yaji/parser_ext.c +24 -4
- data/lib/yaji.rb +1 -1
- data/lib/yaji/version.rb +1 -1
- data/tasks/compile.rake +0 -1
- data/test/test_parser.rb +20 -0
- metadata +8 -8
data/HISTORY.markdown
CHANGED
data/README.markdown
CHANGED
@@ -6,7 +6,7 @@ YAJI is a ruby wrapper to YAJL providing iterator interface to streaming JSON pa
|
|
6
6
|
INSTALL
|
7
7
|
-------
|
8
8
|
|
9
|
-
This gem
|
9
|
+
This gem depends on [yajl][1]. So you need development headers installed
|
10
10
|
on your system to build this gem. For Debian GNU/Linux family it will be something like:
|
11
11
|
|
12
12
|
sudo apt-get install libyajl-dev
|
@@ -87,7 +87,20 @@ code above will print two lines:
|
|
87
87
|
{"id"=>2}
|
88
88
|
|
89
89
|
You can use this iterator when the data is huge and you'd like to allow
|
90
|
-
GC to collect yielded object before parser finish its job.
|
90
|
+
GC to collect yielded object before parser finish its job. You can also
|
91
|
+
specify additional selector if you need to fetch some sibling nodes,
|
92
|
+
e.g. `"size"` from previous example:
|
93
|
+
|
94
|
+
parser = YAJI::Parser.new('{"size":2,"items":[{"id":1}, {"id":2}]}')
|
95
|
+
parser.each("size", "items/") do |obj|
|
96
|
+
puts obj.inspect
|
97
|
+
end
|
98
|
+
|
99
|
+
it yields
|
100
|
+
|
101
|
+
2
|
102
|
+
{"id"=>1}
|
103
|
+
{"id"=>2}
|
91
104
|
|
92
105
|
|
93
106
|
LICENSE
|
data/ext/yaji/parser_ext.c
CHANGED
@@ -261,8 +261,28 @@ static VALUE rb_yaji_parser_parse(int argc, VALUE* argv, VALUE self)
|
|
261
261
|
return Qnil;
|
262
262
|
}
|
263
263
|
|
264
|
-
|
265
|
-
|
264
|
+
static int rb_yaji_str_start_with(VALUE str, VALUE query)
|
265
|
+
{
|
266
|
+
int i;
|
267
|
+
const char *ptr = RSTRING_PTR(str);
|
268
|
+
int len = RSTRING_LEN(str);
|
269
|
+
VALUE entry;
|
270
|
+
|
271
|
+
switch(TYPE(query)) {
|
272
|
+
case T_STRING:
|
273
|
+
return RSTRING_LEN(query) <= len && memcmp(RSTRING_PTR(query), ptr, RSTRING_LEN(query)) == 0;
|
274
|
+
break;
|
275
|
+
case T_ARRAY:
|
276
|
+
for (i=0; i<RARRAY_LEN(query); i++) {
|
277
|
+
entry = RARRAY_PTR(query)[i];
|
278
|
+
if (RSTRING_LEN(entry) <= len && memcmp(RSTRING_PTR(entry), ptr, RSTRING_LEN(entry)) == 0) {
|
279
|
+
return 1;
|
280
|
+
}
|
281
|
+
}
|
282
|
+
break;
|
283
|
+
}
|
284
|
+
return 0;
|
285
|
+
}
|
266
286
|
|
267
287
|
static VALUE rb_yaji_each_iter(VALUE chunk, VALUE* params_p)
|
268
288
|
{
|
@@ -275,7 +295,7 @@ static VALUE rb_yaji_each_iter(VALUE chunk, VALUE* params_p)
|
|
275
295
|
VALUE query = params[2];
|
276
296
|
VALUE last_entry, object, container, key, hash;
|
277
297
|
|
278
|
-
if (NIL_P(query) ||
|
298
|
+
if (NIL_P(query) || rb_yaji_str_start_with(path, query)) {
|
279
299
|
if (event == sym_hash_key) {
|
280
300
|
rb_ary_push(stack, value);
|
281
301
|
} else if (event == sym_start_hash || event == sym_start_array) {
|
@@ -319,7 +339,7 @@ static VALUE rb_yaji_each_iter(VALUE chunk, VALUE* params_p)
|
|
319
339
|
static VALUE rb_yaji_parser_each(int argc, VALUE* argv, VALUE self)
|
320
340
|
{
|
321
341
|
VALUE query, proc, params[3];
|
322
|
-
rb_scan_args(argc, argv, "
|
342
|
+
rb_scan_args(argc, argv, "0*&", &query, &proc);
|
323
343
|
RETURN_ENUMERATOR(self, argc, argv);
|
324
344
|
params[0] = proc; // callback
|
325
345
|
params[1] = rb_ary_new(); // stack
|
data/lib/yaji.rb
CHANGED
data/lib/yaji/version.rb
CHANGED
data/tasks/compile.rake
CHANGED
data/test/test_parser.rb
CHANGED
@@ -145,6 +145,26 @@ class TestParser < MiniTest::Unit::TestCase
|
|
145
145
|
assert expected, object
|
146
146
|
end
|
147
147
|
|
148
|
+
def test_it_allow_several_selectors
|
149
|
+
parser = YAJI::Parser.new(toys_json_str)
|
150
|
+
objects = []
|
151
|
+
parser.each("total_rows", "rows/") do |o|
|
152
|
+
objects << o
|
153
|
+
end
|
154
|
+
expected = [2,
|
155
|
+
{
|
156
|
+
"id" => "buzz",
|
157
|
+
"props" => { "humanoid"=> true, "armed"=> true },
|
158
|
+
"movies" => [1,2,3]
|
159
|
+
},
|
160
|
+
{
|
161
|
+
"id" => "barbie",
|
162
|
+
"props" => { "humanoid"=> true, "armed"=> false },
|
163
|
+
"movies" => [2,3]
|
164
|
+
}]
|
165
|
+
assert expected, objects
|
166
|
+
end
|
167
|
+
|
148
168
|
protected
|
149
169
|
|
150
170
|
def toys_json_str
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yaji
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-08-
|
12
|
+
date: 2011-08-22 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake-compiler
|
16
|
-
requirement: &
|
16
|
+
requirement: &20429520 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *20429520
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: minitest
|
27
|
-
requirement: &
|
27
|
+
requirement: &20428740 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *20428740
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: curb
|
38
|
-
requirement: &
|
38
|
+
requirement: &20427980 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *20427980
|
47
47
|
description: YAJI is a ruby wrapper to YAJL providing iterator interface to streaming
|
48
48
|
JSON parser
|
49
49
|
email: info@couchbase.com
|