brianmario-yajl-ruby 0.4.4 → 0.4.5

Sign up to get free protection for your applications and to get access to all the features.
data/ext/extconf.rb CHANGED
@@ -1,11 +1,13 @@
1
1
  # encoding: UTF-8
2
2
  require 'mkmf'
3
+ require 'rbconfig'
4
+
3
5
  dir_config('yajl')
4
6
  have_header('yajl/yajl_parse.h')
5
7
  have_header('yajl/yajl_gen.h')
6
8
 
7
9
  if have_library("yajl")
8
- create_makefile("yajl")
10
+ create_makefile("yajl_ext")
9
11
  else
10
12
  puts "Yajl not found, maybe try manually specifying --with-yajl-dir to find it?"
11
13
  end
data/ext/yajl.c CHANGED
@@ -44,6 +44,66 @@ void set_static_value(void * ctx, VALUE val) {
44
44
  }
45
45
  }
46
46
 
47
+ void encode_part(yajl_gen hand, VALUE obj, VALUE io) {
48
+ VALUE str, outBuff, otherObj;
49
+ int objLen;
50
+ int idx = 0;
51
+ const unsigned char * buffer;
52
+ unsigned int len;
53
+ yajl_gen_get_buf(hand, &buffer, &len);
54
+ outBuff = rb_str_new((const char *)buffer, len);
55
+ rb_io_write(io, outBuff);
56
+ yajl_gen_clear(hand);
57
+
58
+ switch (TYPE(obj)) {
59
+ case T_HASH:
60
+ yajl_gen_map_open(hand);
61
+
62
+ // TODO: itterate through keys in the hash
63
+ VALUE keys = rb_funcall(obj, intern_keys, 0);
64
+ VALUE entry;
65
+ for(idx=0; idx<RARRAY_LEN(keys); idx++) {
66
+ entry = rb_ary_entry(keys, idx);
67
+ // the key
68
+ encode_part(hand, entry, io);
69
+ // the value
70
+ encode_part(hand, rb_hash_aref(obj, entry), io);
71
+ }
72
+
73
+ yajl_gen_map_close(hand);
74
+ break;
75
+ case T_ARRAY:
76
+ yajl_gen_array_open(hand);
77
+ for(idx=0; idx<RARRAY_LEN(obj); idx++) {
78
+ otherObj = rb_ary_entry(obj, idx);
79
+ encode_part(hand, otherObj, io);
80
+ }
81
+ yajl_gen_array_close(hand);
82
+ break;
83
+ case T_NIL:
84
+ yajl_gen_null(hand);
85
+ break;
86
+ case T_TRUE:
87
+ yajl_gen_bool(hand, 1);
88
+ break;
89
+ case T_FALSE:
90
+ yajl_gen_bool(hand, 0);
91
+ break;
92
+ case T_FIXNUM:
93
+ case T_FLOAT:
94
+ case T_BIGNUM:
95
+ str = rb_funcall(obj, intern_to_s, 0);
96
+ objLen = RSTRING_LEN(str);
97
+ yajl_gen_number(hand, RSTRING_PTR(str), (unsigned int)objLen);
98
+ break;
99
+ default:
100
+ str = rb_funcall(obj, intern_to_s, 0);
101
+ objLen = RSTRING_LEN(str);
102
+ yajl_gen_string(hand, (const unsigned char *)RSTRING_PTR(str), (unsigned int)objLen);
103
+ break;
104
+ }
105
+ }
106
+
47
107
  static int found_null(void * ctx) {
48
108
  set_static_value(ctx, Qnil);
49
109
  check_and_fire_callback(ctx);
@@ -150,7 +210,7 @@ static VALUE t_parse(VALUE self, VALUE io) {
150
210
  // allocate our parser
151
211
  streamParser = yajl_alloc(&callbacks, &cfg, NULL, (void *)context);
152
212
 
153
- VALUE parsed = rb_str_new("", readBufferSize);
213
+ VALUE parsed = rb_str_new2("");
154
214
  VALUE rbufsize = INT2FIX(readBufferSize);
155
215
 
156
216
  // now parse from the IO
@@ -179,11 +239,33 @@ static VALUE t_parse(VALUE self, VALUE io) {
179
239
  return rb_ary_pop(context);
180
240
  }
181
241
 
182
- void Init_yajl() {
242
+ static VALUE t_encode(VALUE self, VALUE obj, VALUE io) {
243
+ yajl_gen_config conf = {0, " "};
244
+ yajl_gen hand;
245
+ yajl_status stat;
246
+ const unsigned char * buffer;
247
+ unsigned int len;
248
+ VALUE outBuff;
249
+
250
+ hand = yajl_gen_alloc(&conf, NULL);
251
+ encode_part(hand, obj, io);
252
+
253
+ // just make sure we output the remaining buffer
254
+ yajl_gen_get_buf(hand, &buffer, &len);
255
+ outBuff = rb_str_new((const char *)buffer, len);
256
+ rb_io_write(io, outBuff);
257
+
258
+ yajl_gen_clear(hand);
259
+ yajl_gen_free(hand);
260
+ return Qnil;
261
+ }
262
+
263
+ void Init_yajl_ext() {
183
264
  mYajl = rb_define_module("Yajl");
184
265
 
185
266
  mStream = rb_define_module_under(mYajl, "Stream");
186
267
  rb_define_module_function(mStream, "parse", t_parse, 1);
268
+ rb_define_module_function(mStream, "encode", t_encode, 2);
187
269
 
188
270
  mChunked = rb_define_module_under(mYajl, "Chunked");
189
271
  rb_define_module_function(mChunked, "parse_some", t_parseSome, 1);
@@ -197,4 +279,6 @@ void Init_yajl() {
197
279
  intern_eof = rb_intern("eof?");
198
280
  intern_respond_to = rb_intern("respond_to?");
199
281
  intern_call = rb_intern("call");
282
+ intern_keys = rb_intern("keys");
283
+ intern_to_s = rb_intern("to_s");
200
284
  }
data/ext/yajl.h CHANGED
@@ -5,7 +5,7 @@
5
5
  #define READ_BUFSIZE 4096
6
6
 
7
7
  static VALUE cParseError, mYajl, mStream, mChunked;
8
- static ID intern_io_read, intern_eof, intern_respond_to, intern_call;
8
+ static ID intern_io_read, intern_eof, intern_respond_to, intern_call, intern_keys, intern_to_s;
9
9
  static int readBufferSize = READ_BUFSIZE;
10
10
  static yajl_parser_config cfg = {1, 1};
11
11
 
@@ -43,3 +43,4 @@ static yajl_callbacks callbacks = {
43
43
  static VALUE t_setParseComplete(VALUE self, VALUE callback);
44
44
  static VALUE t_parseSome(VALUE self, VALUE string);
45
45
  static VALUE t_parse(VALUE self, VALUE io);
46
+ static VALUE t_encode(VALUE self, VALUE obj, VALUE io);
@@ -0,0 +1,11 @@
1
+ # encoding: UTF-8
2
+ module Yajl
3
+ module Bzip2
4
+ # === Yajl::Bzip2::StreamWriter
5
+ class StreamWriter < ::Bzip2::Writer
6
+ def self.encode(obj, io)
7
+ Yajl::Stream.encode(obj, new(io))
8
+ end
9
+ end
10
+ end
11
+ end
data/lib/yajl/bzip2.rb CHANGED
@@ -5,6 +5,7 @@ require 'yajl.rb' unless defined?(Yajl::Stream)
5
5
  begin
6
6
  require 'bzip2' unless defined?(Bzip2)
7
7
  require 'yajl/bzip2/stream_reader.rb'
8
+ require 'yajl/bzip2/stream_writer.rb'
8
9
  rescue LoadError => e
9
10
  raise "Unable to load the bzip2 library. Is the bzip2-ruby gem installed?"
10
11
  end
@@ -0,0 +1,16 @@
1
+ # encoding: UTF-8
2
+ module Yajl
3
+ module Deflate
4
+ # === Yajl::Deflate::StreamWriter
5
+ class StreamWriter < ::Zlib::Deflate
6
+ def write(str)
7
+ deflate(str)
8
+ str.size unless str.nil?
9
+ end
10
+
11
+ def self.encode(obj, io)
12
+ Yajl::Stream.encode(obj, new(io))
13
+ end
14
+ end
15
+ end
16
+ end
data/lib/yajl/deflate.rb CHANGED
@@ -2,4 +2,5 @@
2
2
 
3
3
  require 'yajl.rb' unless defined?(Yajl::Stream)
4
4
  require 'zlib' unless defined?(Zlib)
5
- require 'yajl/deflate/stream_reader.rb'
5
+ require 'yajl/deflate/stream_reader.rb'
6
+ require 'yajl/deflate/stream_writer.rb'
@@ -0,0 +1,11 @@
1
+ # encoding: UTF-8
2
+ module Yajl
3
+ module Gzip
4
+ # === Yajl::Gzip::StreamWriter
5
+ class StreamWriter < ::Zlib::GzipWriter
6
+ def self.encode(obj, io)
7
+ Yajl::Stream.encode(obj, new(io))
8
+ end
9
+ end
10
+ end
11
+ end
data/lib/yajl/gzip.rb CHANGED
@@ -2,4 +2,5 @@
2
2
 
3
3
  require 'yajl.rb' unless defined?(Yajl::Stream)
4
4
  require 'zlib' unless defined?(Zlib)
5
- require 'yajl/gzip/stream_reader.rb'
5
+ require 'yajl/gzip/stream_reader.rb'
6
+ require 'yajl/gzip/stream_writer.rb'
@@ -28,11 +28,12 @@ module Yajl
28
28
  user_agent = opts.has_key?(['User-Agent']) ? opts['User-Agent'] : "Yajl::HttpStream #{Yajl::VERSION}"
29
29
 
30
30
  socket = TCPSocket.new(uri.host, uri.port)
31
- request = "GET #{uri.path}#{uri.query ? "?"+uri.query : nil} HTTP/1.0\r\n"
31
+ request = "GET #{uri.path}#{uri.query ? "?"+uri.query : nil} HTTP/1.1\r\n"
32
32
  request << "Host: #{uri.host}\r\n"
33
33
  request << "Authorization: Basic #{[userinfo].pack('m')}\r\n" unless uri.userinfo.nil?
34
34
  request << "User-Agent: #{user_agent}\r\n"
35
35
  request << "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
36
+ request << "Connection: close\r\n"
36
37
  encodings = []
37
38
  encodings << "bzip2" if defined?(Yajl::Bzip2)
38
39
  encodings << "gzip" if defined?(Yajl::Gzip)
@@ -61,21 +62,25 @@ module Yajl
61
62
  end
62
63
  end
63
64
 
64
- content_type = response_head[:headers]["Content-Type"].split('; ')
65
- content_type = content_type.first
66
- if ALLOWED_MIME_TYPES.include?(content_type)
67
- case response_head[:headers]["Content-Encoding"]
68
- when "gzip"
69
- return Yajl::Gzip::StreamReader.parse(socket)
70
- when "deflate"
71
- return Yajl::Deflate::StreamReader.parse(socket, -Zlib::MAX_WBITS)
72
- when "bzip2"
73
- return Yajl::Bzip2::StreamReader.parse(socket)
65
+ if response_head[:headers]["Transfer-Encoding"] == 'chunked'
66
+ raise Exception, "Chunked responses not supported yet (I'm working on this)"
67
+ else
68
+ content_type = response_head[:headers]["Content-Type"].split('; ')
69
+ content_type = content_type.first
70
+ if ALLOWED_MIME_TYPES.include?(content_type)
71
+ case response_head[:headers]["Content-Encoding"]
72
+ when "gzip"
73
+ return Yajl::Gzip::StreamReader.parse(socket)
74
+ when "deflate"
75
+ return Yajl::Deflate::StreamReader.parse(socket, -Zlib::MAX_WBITS)
76
+ when "bzip2"
77
+ return Yajl::Bzip2::StreamReader.parse(socket)
78
+ else
79
+ return Yajl::Stream.parse(socket)
80
+ end
74
81
  else
75
- return Yajl::Stream.parse(socket)
82
+ raise InvalidContentType, "The response MIME type #{content_type}"
76
83
  end
77
- else
78
- raise InvalidContentType, "The response MIME type #{content_type}"
79
84
  end
80
85
  ensure
81
86
  socket.close
data/lib/yajl.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # encoding: UTF-8
2
- require 'yajl.bundle'
2
+ require 'yajl_ext'
3
3
 
4
4
  # = Extras
5
5
  # We're not going to load these auotmatically, because you might not need them ;)
@@ -13,7 +13,7 @@ require 'yajl.bundle'
13
13
  #
14
14
  # Ruby bindings to the excellent Yajl (Yet Another JSON Parser) ANSI C library.
15
15
  module Yajl
16
- VERSION = "0.4.4"
16
+ VERSION = "0.4.5"
17
17
 
18
18
  # == Yajl::Chunked
19
19
  #
data/yajl-ruby.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{yajl-ruby}
5
- s.version = "0.4.4"
5
+ s.version = "0.4.5"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Brian Lopez"]
9
- s.date = %q{2009-05-12}
9
+ s.date = %q{2009-05-17}
10
10
  s.email = %q{seniorlopez@gmail.com}
11
11
  s.extensions = ["ext/extconf.rb"]
12
12
  s.extra_rdoc_files = [
@@ -20,9 +20,15 @@ Gem::Specification.new do |s|
20
20
  "README.rdoc",
21
21
  "Rakefile",
22
22
  "VERSION.yml",
23
+ "benchmark/encode.rb",
24
+ "benchmark/encode_json_and_marshal.rb",
25
+ "benchmark/encode_json_and_yaml.rb",
23
26
  "benchmark/http.rb",
24
- "benchmark/stream.rb",
27
+ "benchmark/parse.rb",
28
+ "benchmark/parse_json_and_marshal.rb",
29
+ "benchmark/parse_json_and_yaml.rb",
25
30
  "benchmark/subjects/contacts.json",
31
+ "benchmark/subjects/contacts.yml",
26
32
  "benchmark/subjects/item.json",
27
33
  "benchmark/subjects/ohai.json",
28
34
  "benchmark/subjects/twitter_search.json",
@@ -34,10 +40,13 @@ Gem::Specification.new do |s|
34
40
  "lib/yajl.rb",
35
41
  "lib/yajl/bzip2.rb",
36
42
  "lib/yajl/bzip2/stream_reader.rb",
43
+ "lib/yajl/bzip2/stream_writer.rb",
37
44
  "lib/yajl/deflate.rb",
38
45
  "lib/yajl/deflate/stream_reader.rb",
46
+ "lib/yajl/deflate/stream_writer.rb",
39
47
  "lib/yajl/gzip.rb",
40
48
  "lib/yajl/gzip/stream_reader.rb",
49
+ "lib/yajl/gzip/stream_writer.rb",
41
50
  "lib/yajl/http_stream.rb",
42
51
  "spec/active_support_spec.rb",
43
52
  "spec/fixtures/fail.15.json",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brianmario-yajl-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.4
4
+ version: 0.4.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Lopez
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-12 00:00:00 -07:00
12
+ date: 2009-05-17 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -29,9 +29,15 @@ files:
29
29
  - README.rdoc
30
30
  - Rakefile
31
31
  - VERSION.yml
32
+ - benchmark/encode.rb
33
+ - benchmark/encode_json_and_marshal.rb
34
+ - benchmark/encode_json_and_yaml.rb
32
35
  - benchmark/http.rb
33
- - benchmark/stream.rb
36
+ - benchmark/parse.rb
37
+ - benchmark/parse_json_and_marshal.rb
38
+ - benchmark/parse_json_and_yaml.rb
34
39
  - benchmark/subjects/contacts.json
40
+ - benchmark/subjects/contacts.yml
35
41
  - benchmark/subjects/item.json
36
42
  - benchmark/subjects/ohai.json
37
43
  - benchmark/subjects/twitter_search.json
@@ -43,10 +49,13 @@ files:
43
49
  - lib/yajl.rb
44
50
  - lib/yajl/bzip2.rb
45
51
  - lib/yajl/bzip2/stream_reader.rb
52
+ - lib/yajl/bzip2/stream_writer.rb
46
53
  - lib/yajl/deflate.rb
47
54
  - lib/yajl/deflate/stream_reader.rb
55
+ - lib/yajl/deflate/stream_writer.rb
48
56
  - lib/yajl/gzip.rb
49
57
  - lib/yajl/gzip/stream_reader.rb
58
+ - lib/yajl/gzip/stream_writer.rb
50
59
  - lib/yajl/http_stream.rb
51
60
  - spec/active_support_spec.rb
52
61
  - spec/fixtures/fail.15.json