yaji 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY.markdown CHANGED
@@ -1,3 +1,7 @@
1
+ === 0.0.3 / 2011-08-19
2
+
3
+ * Allow multiple filters to select sibling JSON nodes
4
+
1
5
  === 0.0.2 / 2011-08-19
2
6
 
3
7
  * Integration with curb gem
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 depend on [yajl][1]. So you need development headers installed
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
@@ -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
- #define YAJI_STR_START_WITH(str, head) \
265
- (RSTRING_LEN(head) <= RSTRING_LEN(str) && memcmp(RSTRING_PTR(head), RSTRING_PTR(str), RSTRING_LEN(head)) == 0)
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) || YAJI_STR_START_WITH(path, 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, "01&", &query, &proc);
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
@@ -18,7 +18,7 @@
18
18
  #
19
19
 
20
20
  require "yaji/version"
21
- require "yaji/parser_ext"
21
+ require "parser_ext"
22
22
 
23
23
  module YAJI
24
24
  end
data/lib/yaji/version.rb CHANGED
@@ -18,5 +18,5 @@
18
18
  #
19
19
 
20
20
  module YAJI
21
- VERSION = "0.0.2"
21
+ VERSION = "0.0.3"
22
22
  end
data/tasks/compile.rake CHANGED
@@ -7,7 +7,6 @@ end
7
7
 
8
8
  Rake::ExtensionTask.new("parser_ext", gemspec) do |ext|
9
9
  ext.ext_dir = File.join('ext', 'yaji')
10
- ext.lib_dir = File.join('lib', 'yaji')
11
10
 
12
11
  # clean compiled extension
13
12
  CLEAN.include "#{ext.lib_dir}/*.#{RbConfig::CONFIG['DLEXT']}"
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.2
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-19 00:00:00.000000000Z
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: &8959040 !ruby/object:Gem::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: *8959040
24
+ version_requirements: *20429520
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: minitest
27
- requirement: &8958360 !ruby/object:Gem::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: *8958360
35
+ version_requirements: *20428740
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: curb
38
- requirement: &8957820 !ruby/object:Gem::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: *8957820
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