yajl-ruby 0.7.1 → 0.7.2

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.

Potentially problematic release.


This version of yajl-ruby might be problematic. Click here for more details.

Files changed (46) hide show
  1. data/CHANGELOG.md +8 -1
  2. data/MIT-LICENSE +3 -3
  3. data/README.rdoc +8 -8
  4. data/VERSION.yml +1 -1
  5. data/benchmark/subjects/ohai.yml +171 -171
  6. data/ext/api/yajl_common.h +9 -9
  7. data/ext/api/yajl_gen.h +10 -10
  8. data/ext/api/yajl_parse.h +15 -15
  9. data/ext/yajl.c +8 -8
  10. data/ext/yajl_alloc.c +6 -6
  11. data/ext/yajl_alloc.h +6 -6
  12. data/ext/yajl_buf.c +7 -7
  13. data/ext/yajl_buf.h +7 -7
  14. data/ext/yajl_bytestack.h +10 -10
  15. data/ext/yajl_encode.c +12 -12
  16. data/ext/yajl_encode.h +6 -6
  17. data/ext/yajl_ext.c +67 -80
  18. data/ext/yajl_ext.h +4 -4
  19. data/ext/yajl_gen.c +16 -16
  20. data/ext/yajl_lex.c +79 -76
  21. data/ext/yajl_lex.h +14 -14
  22. data/ext/yajl_parser.c +34 -34
  23. data/ext/yajl_parser.h +7 -7
  24. data/lib/yajl.rb +7 -7
  25. data/lib/yajl/bzip2/stream_reader.rb +1 -11
  26. data/lib/yajl/bzip2/stream_writer.rb +1 -1
  27. data/lib/yajl/deflate/stream_reader.rb +6 -7
  28. data/lib/yajl/deflate/stream_writer.rb +2 -2
  29. data/lib/yajl/gzip/stream_reader.rb +0 -10
  30. data/lib/yajl/http_stream.rb +12 -12
  31. data/lib/yajl/json_gem/encoding.rb +4 -4
  32. data/lib/yajl/json_gem/parsing.rb +3 -3
  33. data/spec/encoding/encoding_spec.rb +23 -23
  34. data/spec/global/global_spec.rb +8 -8
  35. data/spec/http/http_delete_spec.rb +13 -13
  36. data/spec/http/http_error_spec.rb +2 -2
  37. data/spec/http/http_get_spec.rb +15 -15
  38. data/spec/http/http_post_spec.rb +12 -12
  39. data/spec/http/http_put_spec.rb +14 -14
  40. data/spec/json_gem_compatibility/compatibility_spec.rb +21 -21
  41. data/spec/parsing/active_support_spec.rb +6 -6
  42. data/spec/parsing/chunked_spec.rb +12 -12
  43. data/spec/parsing/fixtures_spec.rb +4 -4
  44. data/spec/parsing/one_off_spec.rb +9 -9
  45. data/yajl-ruby.gemspec +3 -3
  46. metadata +12 -5
@@ -3,13 +3,13 @@ module Yajl
3
3
  module Deflate
4
4
  # A wrapper around the Zlib::Deflate class for easier JSON stream parsing
5
5
  class StreamWriter < ::Zlib::Deflate
6
-
6
+
7
7
  # A helper method to allow use similar to IO#write
8
8
  def write(str)
9
9
  deflate(str)
10
10
  str.size unless str.nil?
11
11
  end
12
-
12
+
13
13
  # A helper method for one-off encoding to a deflate-compressed stream
14
14
  #
15
15
  # Look up Yajl::Encoder#encode for parameter documentation
@@ -4,16 +4,6 @@ module Yajl
4
4
  # This is a wrapper around Zlib::GzipReader to allow it's #read method to adhere
5
5
  # to the IO spec, allowing for two parameters (length, and buffer)
6
6
  class StreamReader < ::Zlib::GzipReader
7
-
8
- # Wrapper method to allow use similar to IO#read
9
- def read(len=nil, buffer=nil)
10
- unless buffer.nil?
11
- buffer.replace super(len)
12
- return buffer
13
- end
14
- super(len)
15
- end
16
-
17
7
  # Helper method for one-off parsing from a gzip-compressed stream
18
8
  #
19
9
  # See Yajl::Parser#parse for parameter documentation
@@ -7,24 +7,24 @@ module Yajl
7
7
  # This module is for making HTTP requests to which the response bodies (and possibly requests in the near future)
8
8
  # are streamed directly into Yajl.
9
9
  class HttpStream
10
-
10
+
11
11
  # This Exception is thrown when an HTTP response isn't in ALLOWED_MIME_TYPES
12
12
  # and therefore cannot be parsed.
13
13
  class InvalidContentType < Exception; end
14
14
  class HttpError < StandardError
15
-
15
+
16
16
  attr_reader :message, :headers
17
-
17
+
18
18
  def initialize(message, headers)
19
19
  @message = message
20
20
  @headers = headers
21
21
  end
22
22
  end
23
-
23
+
24
24
  # The mime-type we expect the response to be. If it's anything else, we can't parse it
25
25
  # and an InvalidContentType is raised.
26
26
  ALLOWED_MIME_TYPES = ["application/json", "text/plain"]
27
-
27
+
28
28
  # Makes a basic HTTP GET request to the URI provided
29
29
  def self.get(uri, opts = {}, &block)
30
30
  request("GET", uri, opts, &block)
@@ -42,7 +42,7 @@ module Yajl
42
42
  def self.post(uri, body, opts = {}, &block)
43
43
  request("POST", uri, opts.merge({:body => body}), &block)
44
44
  end
45
-
45
+
46
46
  # Makes a basic HTTP POST request to the URI provided allowing the user to terminate the connection
47
47
  def post(uri, body, opts = {}, &block)
48
48
  initialize_socket(uri, opts)
@@ -55,7 +55,7 @@ module Yajl
55
55
  def self.put(uri, body, opts = {}, &block)
56
56
  request("PUT", uri, opts.merge({:body => body}), &block)
57
57
  end
58
-
58
+
59
59
  # Makes a basic HTTP PUT request to the URI provided allowing the user to terminate the connection
60
60
  def put(uri, body, opts = {}, &block)
61
61
  initialize_socket(uri, opts)
@@ -82,13 +82,13 @@ module Yajl
82
82
  @intentional_termination = true
83
83
  @socket.close
84
84
  end
85
-
85
+
86
86
  protected
87
87
  def self.request(method, uri, opts = {}, &block)
88
88
  if uri.is_a?(String)
89
89
  uri = URI.parse(uri)
90
90
  end
91
-
91
+
92
92
  user_agent = opts.has_key?('User-Agent') ? opts.delete(['User-Agent']) : "Yajl::HttpStream #{Yajl::VERSION}"
93
93
  if method == "POST" || method == "PUT"
94
94
  content_type = opts.has_key?('Content-Type') ? opts.delete(['Content-Type']) : "application/x-www-form-urlencoded"
@@ -140,11 +140,11 @@ module Yajl
140
140
  end
141
141
  end
142
142
  end
143
-
143
+
144
144
  if (response_head[:code] != 200)
145
- raise HttpError.new("Code 200 expected got #{response_head[:code]}", response_head[:headers])
145
+ raise HttpError.new("Code 200 expected got #{response_head[:code]}", response_head[:headers])
146
146
  end
147
-
147
+
148
148
  parser = Yajl::Parser.new(opts)
149
149
  parser.on_parse_complete = block if block_given?
150
150
  if response_head[:headers]["Transfer-Encoding"] == 'chunked'
@@ -8,14 +8,14 @@ Yajl::Encoder.enable_json_gem_compatability
8
8
  # Our fallback to_json definition
9
9
  class Object
10
10
  def to_json(*args, &block)
11
- "\"#{to_s}\""
11
+ to_s.dump
12
12
  end
13
13
  end
14
14
 
15
15
  module JSON
16
16
  class JSONError < StandardError; end unless defined?(JSON::JSONError)
17
17
  class GeneratorError < JSONError; end unless defined?(JSON::GeneratorError)
18
-
18
+
19
19
  def self.generate(obj, opts={})
20
20
  begin
21
21
  options_map = {}
@@ -28,7 +28,7 @@ module JSON
28
28
  raise JSON::GeneratorError, e.message
29
29
  end
30
30
  end
31
-
31
+
32
32
  def self.pretty_generate(obj, opts={})
33
33
  begin
34
34
  options_map = {}
@@ -39,7 +39,7 @@ module JSON
39
39
  raise JSON::GeneratorError, e.message
40
40
  end
41
41
  end
42
-
42
+
43
43
  def self.dump(obj, io=nil, *args)
44
44
  begin
45
45
  Yajl::Encoder.encode(obj, io)
@@ -4,11 +4,11 @@ require 'yajl' unless defined?(Yajl::Parser)
4
4
  module JSON
5
5
  class JSONError < StandardError; end unless defined?(JSON::JSONError)
6
6
  class ParserError < JSONError; end unless defined?(JSON::ParserError)
7
-
7
+
8
8
  def self.default_options
9
9
  @default_options ||= {:symbolize_keys => false}
10
10
  end
11
-
11
+
12
12
  def self.parse(str, opts=JSON.default_options)
13
13
  begin
14
14
  Yajl::Parser.parse(str, opts)
@@ -16,7 +16,7 @@ module JSON
16
16
  raise JSON::ParserError, e.message
17
17
  end
18
18
  end
19
-
19
+
20
20
  def self.load(input, *args)
21
21
  begin
22
22
  Yajl::Parser.parse(input, default_options)
@@ -3,13 +3,13 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')
3
3
 
4
4
  class Dummy2
5
5
  def to_json
6
- "hawtness"
6
+ "hawtness".dump
7
7
  end
8
8
  end
9
9
 
10
10
  describe "Yajl JSON encoder" do
11
11
  FILES = Dir[File.dirname(__FILE__)+'/../../benchmark/subjects/*.json']
12
-
12
+
13
13
  FILES.each do |file|
14
14
  it "should encode #{File.basename(file)} to an IO" do
15
15
  # we don't care about testing the stream subject as it has multiple JSON strings in it
@@ -27,7 +27,7 @@ describe "Yajl JSON encoder" do
27
27
  end
28
28
  end
29
29
  end
30
-
30
+
31
31
  FILES.each do |file|
32
32
  it "should encode #{File.basename(file)} and return a String" do
33
33
  # we don't care about testing the stream subject as it has multiple JSON strings in it
@@ -42,7 +42,7 @@ describe "Yajl JSON encoder" do
42
42
  end
43
43
  end
44
44
  end
45
-
45
+
46
46
  FILES.each do |file|
47
47
  it "should encode #{File.basename(file)} call the passed block, passing it a String" do
48
48
  # we don't care about testing the stream subject as it has multiple JSON strings in it
@@ -60,7 +60,7 @@ describe "Yajl JSON encoder" do
60
60
  end
61
61
  end
62
62
  end
63
-
63
+
64
64
  it "should encode with :pretty turned on and a single space indent, to an IO" do
65
65
  output = "{\n \"foo\": 1234\n}"
66
66
  obj = {:foo => 1234}
@@ -70,7 +70,7 @@ describe "Yajl JSON encoder" do
70
70
  io.rewind
71
71
  io.read.should == output
72
72
  end
73
-
73
+
74
74
  it "should encode with :pretty turned on and a single space indent, and return a String" do
75
75
  output = "{\n \"foo\": 1234\n}"
76
76
  obj = {:foo => 1234}
@@ -78,7 +78,7 @@ describe "Yajl JSON encoder" do
78
78
  output = encoder.encode(obj)
79
79
  output.should == output
80
80
  end
81
-
81
+
82
82
  it "should encode with :pretty turned on and a tab character indent, to an IO" do
83
83
  output = "{\n\t\"foo\": 1234\n}"
84
84
  obj = {:foo => 1234}
@@ -88,7 +88,7 @@ describe "Yajl JSON encoder" do
88
88
  io.rewind
89
89
  io.read.should == output
90
90
  end
91
-
91
+
92
92
  it "should encode with :pretty turned on and a tab character indent, and return a String" do
93
93
  output = "{\n\t\"foo\": 1234\n}"
94
94
  obj = {:foo => 1234}
@@ -96,7 +96,7 @@ describe "Yajl JSON encoder" do
96
96
  output = encoder.encode(obj)
97
97
  output.should == output
98
98
  end
99
-
99
+
100
100
  it "should encode with it's class method with :pretty and a tab character indent options set, to an IO" do
101
101
  output = "{\n\t\"foo\": 1234\n}"
102
102
  obj = {:foo => 1234}
@@ -105,14 +105,14 @@ describe "Yajl JSON encoder" do
105
105
  io.rewind
106
106
  io.read.should == output
107
107
  end
108
-
108
+
109
109
  it "should encode with it's class method with :pretty and a tab character indent options set, and return a String" do
110
110
  output = "{\n\t\"foo\": 1234\n}"
111
111
  obj = {:foo => 1234}
112
112
  output = Yajl::Encoder.encode(obj, :pretty => true, :indent => "\t")
113
113
  output.should == output
114
114
  end
115
-
115
+
116
116
  it "should encode with it's class method with :pretty and a tab character indent options set, to a block" do
117
117
  output = "{\n\t\"foo\": 1234\n}"
118
118
  obj = {:foo => 1234}
@@ -122,7 +122,7 @@ describe "Yajl JSON encoder" do
122
122
  end
123
123
  output.should == output
124
124
  end
125
-
125
+
126
126
  it "should encode multiple objects into a single stream, to an IO" do
127
127
  io = StringIO.new
128
128
  obj = {:foo => 1234}
@@ -134,7 +134,7 @@ describe "Yajl JSON encoder" do
134
134
  output = "{\"foo\":1234}{\"foo\":1234}{\"foo\":1234}{\"foo\":1234}{\"foo\":1234}"
135
135
  io.read.should == output
136
136
  end
137
-
137
+
138
138
  it "should encode multiple objects into a single stream, and return a String" do
139
139
  obj = {:foo => 1234}
140
140
  encoder = Yajl::Encoder.new
@@ -145,37 +145,37 @@ describe "Yajl JSON encoder" do
145
145
  output = "{\"foo\":1234}{\"foo\":1234}{\"foo\":1234}{\"foo\":1234}{\"foo\":1234}"
146
146
  json_output.should == output
147
147
  end
148
-
148
+
149
149
  it "should encode all map keys as strings" do
150
150
  Yajl::Encoder.encode({1=>1}).should eql("{\"1\":1}")
151
151
  end
152
-
152
+
153
153
  it "should check for and call #to_json if it exists on custom objects" do
154
154
  d = Dummy2.new
155
155
  Yajl::Encoder.encode({:foo => d}).should eql('{"foo":"hawtness"}')
156
156
  end
157
-
157
+
158
158
  it "should encode a hash where the key and value can be symbols" do
159
159
  Yajl::Encoder.encode({:foo => :bar}).should eql('{"foo":"bar"}')
160
160
  end
161
-
161
+
162
162
  it "should encode using a newline or nil terminator" do
163
163
  Yajl::Encoder.new(:terminator => "\n").encode({:foo => :bar}).should eql("{\"foo\":\"bar\"}\n")
164
164
  Yajl::Encoder.new(:terminator => nil).encode({:foo => :bar}).should eql("{\"foo\":\"bar\"}")
165
165
  end
166
-
166
+
167
167
  it "should encode using a newline or nil terminator, to an IO" do
168
168
  s = StringIO.new
169
169
  Yajl::Encoder.new(:terminator => "\n").encode({:foo => :bar}, s)
170
170
  s.rewind
171
171
  s.read.should eql("{\"foo\":\"bar\"}\n")
172
-
172
+
173
173
  s = StringIO.new
174
174
  Yajl::Encoder.new(:terminator => nil).encode({:foo => :bar}, s)
175
175
  s.rewind
176
176
  s.read.should eql("{\"foo\":\"bar\"}")
177
177
  end
178
-
178
+
179
179
  it "should encode using a newline or nil terminator, using a block" do
180
180
  s = StringIO.new
181
181
  Yajl::Encoder.new(:terminator => "\n").encode({:foo => :bar}) do |chunk|
@@ -183,7 +183,7 @@ describe "Yajl JSON encoder" do
183
183
  end
184
184
  s.rewind
185
185
  s.read.should eql("{\"foo\":\"bar\"}\n")
186
-
186
+
187
187
  s = StringIO.new
188
188
  nilpassed = false
189
189
  Yajl::Encoder.new(:terminator => nil).encode({:foo => :bar}) do |chunk|
@@ -194,13 +194,13 @@ describe "Yajl JSON encoder" do
194
194
  s.rewind
195
195
  s.read.should eql("{\"foo\":\"bar\"}")
196
196
  end
197
-
197
+
198
198
  it "should not encode NaN" do
199
199
  lambda {
200
200
  Yajl::Encoder.encode(0.0/0.0)
201
201
  }.should raise_error(Yajl::EncodeError)
202
202
  end
203
-
203
+
204
204
  it "should not encode Infinity or -Infinity" do
205
205
  lambda {
206
206
  Yajl::Encoder.encode(1.0/0.0)
@@ -10,41 +10,41 @@ describe "Yajl" do
10
10
  it "should be able to encode to a string" do
11
11
  Yajl.dump({:a => 1234}).should eql('{"a":1234}')
12
12
  end
13
-
13
+
14
14
  it "should be able to encode to an IO" do
15
15
  io = StringIO.new
16
16
  Yajl.dump({:a => 1234}, io)
17
17
  io.rewind
18
- io.read.should eql('{"a":1234}')
18
+ io.read.should eql('{"a":1234}')
19
19
  end
20
-
20
+
21
21
  it "should be able to encode with a block supplied" do
22
22
  Yajl.dump({:a => 1234}) do |chunk|
23
23
  chunk.should eql('{"a":1234}')
24
24
  end
25
25
  end
26
26
  end
27
-
27
+
28
28
  context "load" do
29
29
  it "should exist as a class-method" do
30
30
  Yajl.should respond_to(:load)
31
31
  end
32
-
32
+
33
33
  it "should be able to parse from a string" do
34
34
  Yajl.load('{"a":1234}').should eql({"a" => 1234})
35
35
  end
36
-
36
+
37
37
  it "should be able to parse from an IO" do
38
38
  io = StringIO.new('{"a":1234}')
39
39
  Yajl.load(io).should eql({"a" => 1234})
40
40
  end
41
-
41
+
42
42
  it "should be able to parse from a string with a block supplied" do
43
43
  Yajl.load('{"a":1234}') do |h|
44
44
  h.should eql({"a" => 1234})
45
45
  end
46
46
  end
47
-
47
+
48
48
  it "should be able to parse from an IO with a block supplied" do
49
49
  io = StringIO.new('{"a":1234}')
50
50
  Yajl.load(io) do |h|
@@ -22,31 +22,31 @@ describe "Yajl HTTP DELETE request" do
22
22
  raw = File.new(File.expand_path(File.dirname(__FILE__) + '/fixtures/http.raw.dump'), 'r')
23
23
  parse_off_headers(raw)
24
24
  @template_hash = Yajl::Parser.parse(raw)
25
-
25
+
26
26
  raw.rewind
27
27
  parse_off_headers(raw)
28
28
  @template_hash_symbolized = Yajl::Parser.parse(raw, :symbolize_keys => true)
29
-
29
+
30
30
  @deflate = File.new(File.expand_path(File.dirname(__FILE__) + '/fixtures/http.deflate.dump'), 'r')
31
31
  @gzip = File.new(File.expand_path(File.dirname(__FILE__) + '/fixtures/http.gzip.dump'), 'r')
32
32
  end
33
-
33
+
34
34
  after(:each) do
35
35
  @file_path = nil
36
36
  end
37
-
37
+
38
38
  def prepare_mock_request_dump(format=:raw)
39
39
  @request = File.new(File.expand_path(File.dirname(__FILE__) + "/fixtures/http.#{format}.dump"), 'r')
40
40
  @uri = 'file://'+File.expand_path(File.dirname(__FILE__) + "/fixtures/http/http.#{format}.dump")
41
41
  TCPSocket.should_receive(:new).and_return(@request)
42
42
  @request.should_receive(:write)
43
43
  end
44
-
44
+
45
45
  it "should parse a raw response" do
46
46
  prepare_mock_request_dump :raw
47
47
  @template_hash.should == Yajl::HttpStream.delete(@uri)
48
48
  end
49
-
49
+
50
50
  it "should parse a raw response using instance method" do
51
51
  prepare_mock_request_dump :raw
52
52
  @uri.should_receive(:host)
@@ -54,12 +54,12 @@ describe "Yajl HTTP DELETE request" do
54
54
  stream = Yajl::HttpStream.new
55
55
  @template_hash.should == stream.delete(@uri)
56
56
  end
57
-
57
+
58
58
  it "should parse a raw response and symbolize keys" do
59
59
  prepare_mock_request_dump :raw
60
60
  @template_hash_symbolized.should == Yajl::HttpStream.delete(@uri, :symbolize_keys => true)
61
61
  end
62
-
62
+
63
63
  if defined?(Yajl::Bzip2::StreamReader)
64
64
  it "should parse a bzip2 compressed response" do
65
65
  prepare_mock_request_dump :bzip2
@@ -71,27 +71,27 @@ describe "Yajl HTTP DELETE request" do
71
71
  @template_hash_symbolized.should == Yajl::HttpStream.delete(@uri, :symbolize_keys => true)
72
72
  end
73
73
  end
74
-
74
+
75
75
  it "should parse a deflate compressed response" do
76
76
  prepare_mock_request_dump :deflate
77
77
  @template_hash.should == Yajl::HttpStream.delete(@uri)
78
78
  end
79
-
79
+
80
80
  it "should parse a deflate compressed response and symbolize keys" do
81
81
  prepare_mock_request_dump :deflate
82
82
  @template_hash_symbolized.should == Yajl::HttpStream.delete(@uri, :symbolize_keys => true)
83
83
  end
84
-
84
+
85
85
  it "should parse a gzip compressed response" do
86
86
  prepare_mock_request_dump :gzip
87
87
  @template_hash.should == Yajl::HttpStream.delete(@uri)
88
88
  end
89
-
89
+
90
90
  it "should parse a gzip compressed response and symbolize keys" do
91
91
  prepare_mock_request_dump :gzip
92
92
  @template_hash_symbolized.should == Yajl::HttpStream.delete(@uri, :symbolize_keys => true)
93
93
  end
94
-
94
+
95
95
  it "should raise when an HTTP code that isn't 200 is returned" do
96
96
  prepare_mock_request_dump :error
97
97
  lambda { Yajl::HttpStream.delete(@uri) }.should raise_exception(Yajl::HttpStream::HttpError)