yajl-ruby 0.8.3 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of yajl-ruby might be problematic. Click here for more details.
- data/.gitignore +2 -1
- data/CHANGELOG.md +9 -0
- data/{README.rdoc → README.md} +161 -125
- data/benchmark/encode.rb +0 -1
- data/benchmark/encode_json_and_marshal.rb +0 -1
- data/benchmark/encode_json_and_yaml.rb +0 -1
- data/benchmark/http.rb +0 -1
- data/benchmark/parse.rb +0 -1
- data/benchmark/parse_json_and_marshal.rb +0 -1
- data/benchmark/parse_json_and_yaml.rb +0 -1
- data/benchmark/parse_stream.rb +0 -1
- data/examples/encoding/chunked_encoding.rb +0 -1
- data/examples/encoding/one_shot.rb +0 -1
- data/examples/encoding/to_an_io.rb +0 -1
- data/examples/http/twitter_search_api.rb +0 -1
- data/examples/http/twitter_stream_api.rb +0 -1
- data/examples/parsing/from_file.rb +0 -1
- data/examples/parsing/from_stdin.rb +0 -1
- data/examples/parsing/from_string.rb +0 -1
- data/ext/yajl/extconf.rb +1 -2
- data/ext/yajl/yajl_ext.c +2 -0
- data/lib/yajl.rb +0 -1
- data/lib/yajl/bzip2.rb +1 -1
- data/lib/yajl/bzip2/stream_reader.rb +0 -1
- data/lib/yajl/bzip2/stream_writer.rb +0 -1
- data/lib/yajl/deflate.rb +1 -1
- data/lib/yajl/deflate/stream_reader.rb +0 -1
- data/lib/yajl/deflate/stream_writer.rb +0 -1
- data/lib/yajl/gzip.rb +1 -1
- data/lib/yajl/gzip/stream_reader.rb +0 -1
- data/lib/yajl/gzip/stream_writer.rb +0 -1
- data/lib/yajl/http_stream.rb +2 -1
- data/lib/yajl/json_gem.rb +2 -1
- data/lib/yajl/json_gem/encoding.rb +0 -1
- data/lib/yajl/json_gem/parsing.rb +0 -1
- data/lib/yajl/version.rb +1 -1
- data/spec/encoding/encoding_spec.rb +24 -0
- data/spec/global/global_spec.rb +0 -1
- data/spec/http/http_delete_spec.rb +0 -1
- data/spec/http/http_error_spec.rb +0 -1
- data/spec/http/http_get_spec.rb +0 -1
- data/spec/http/http_post_spec.rb +0 -1
- data/spec/http/http_put_spec.rb +0 -1
- data/spec/http/http_stream_options_spec.rb +0 -1
- data/spec/parsing/chunked_spec.rb +0 -1
- data/spec/parsing/fixtures_spec.rb +0 -1
- data/spec/spec_helper.rb +0 -1
- data/tasks/compile.rake +4 -6
- data/yajl-ruby.gemspec +1 -4
- metadata +52 -86
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,14 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## 1.0.0 (September 13th, 2011)
|
4
|
+
* add deprecation notice for Yajl's Bzip2 support
|
5
|
+
* add deprecation notice for Yajl's Deflate support
|
6
|
+
* add deprecation notice for Yajl's Gzip support
|
7
|
+
* add deprecation notice for Yajl's JSON gem compatibility API
|
8
|
+
* add deprecation notice for Yajl::HttpStream
|
9
|
+
* change the path the extension is copied into to be 'lib/yajl'
|
10
|
+
* remove 'ext' from the loadpath
|
11
|
+
|
3
12
|
## 0.8.3 (August 16th, 2011)
|
4
13
|
* fix bug where Yajl::HttpStream wouldn't pass through a user-specified socket
|
5
14
|
* fix incorrect Ruby initialization hook method name
|
data/{README.rdoc → README.md}
RENAMED
@@ -1,10 +1,10 @@
|
|
1
|
-
|
1
|
+
# YAJL C Bindings for Ruby
|
2
2
|
|
3
3
|
This gem is a C binding to the excellent YAJL JSON parsing and generation library.
|
4
4
|
|
5
5
|
You can read more info at the project's website http://lloyd.github.com/yajl or check out its code at http://github.com/lloyd/yajl.
|
6
6
|
|
7
|
-
|
7
|
+
## Features
|
8
8
|
|
9
9
|
* JSON parsing and encoding directly to and from an IO stream (file, socket, etc) or String. Compressed stream parsing and encoding supported for Bzip2, Gzip and Deflate.
|
10
10
|
* Parse and encode *multiple* JSON objects to and from streams or strings continuously.
|
@@ -17,94 +17,111 @@ You can read more info at the project's website http://lloyd.github.com/yajl or
|
|
17
17
|
* ~1.5x faster than Marshal.load
|
18
18
|
* ~2x faster than Marshal.dump
|
19
19
|
|
20
|
-
|
20
|
+
## How to install
|
21
21
|
|
22
22
|
Go ahead and install it as usual:
|
23
23
|
|
24
|
-
|
24
|
+
```
|
25
|
+
gem install yajl-ruby
|
26
|
+
```
|
25
27
|
|
26
|
-
|
28
|
+
## Example of use
|
27
29
|
|
28
30
|
NOTE: I'm building up a collection of small examples in the examples (http://github.com/brianmario/yajl-ruby/tree/master/examples) folder.
|
29
31
|
|
30
32
|
First, you're probably gonna want to require it:
|
31
33
|
|
32
|
-
|
34
|
+
``` ruby
|
35
|
+
require 'yajl'
|
36
|
+
```
|
33
37
|
|
34
|
-
|
38
|
+
### Parsing
|
35
39
|
|
36
40
|
Then maybe parse some JSON from:
|
37
41
|
|
38
42
|
a File IO
|
39
43
|
|
40
|
-
|
41
|
-
|
42
|
-
|
44
|
+
``` ruby
|
45
|
+
json = File.new('test.json', 'r')
|
46
|
+
parser = Yajl::Parser.new
|
47
|
+
hash = parser.parse(json)
|
48
|
+
```
|
43
49
|
|
44
50
|
or maybe a StringIO
|
45
51
|
|
46
|
-
|
47
|
-
|
48
|
-
|
52
|
+
``` ruby
|
53
|
+
json = StringIO.new("...some JSON...")
|
54
|
+
parser = Yajl::Parser.new
|
55
|
+
hash = parser.parse(json)
|
56
|
+
```
|
49
57
|
|
50
58
|
or maybe STDIN
|
51
59
|
|
52
|
-
|
53
|
-
|
60
|
+
```
|
61
|
+
cat someJsonFile.json | ruby -ryajl -e "puts Yajl::Parser.parse(STDIN).inspect"
|
62
|
+
```
|
54
63
|
|
55
64
|
Or lets say you didn't have access to the IO object that contained JSON data, but instead
|
56
65
|
only had access to chunks of it at a time. No problem!
|
57
66
|
|
58
67
|
(Assume we're in an EventMachine::Connection instance)
|
59
68
|
|
60
|
-
|
61
|
-
|
62
|
-
|
69
|
+
``` ruby
|
70
|
+
def post_init
|
71
|
+
@parser = Yajl::Parser.new(:symbolize_keys => true)
|
72
|
+
end
|
63
73
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
end
|
68
|
-
|
69
|
-
def connection_completed
|
70
|
-
# once a full JSON object has been parsed from the stream
|
71
|
-
# object_parsed will be called, and passed the constructed object
|
72
|
-
@parser.on_parse_complete = method(:object_parsed)
|
74
|
+
def object_parsed(obj)
|
75
|
+
puts "Sometimes one pays most for the things one gets for nothing. - Albert Einstein"
|
76
|
+
puts obj.inspect
|
73
77
|
end
|
74
78
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
+
def connection_completed
|
80
|
+
# once a full JSON object has been parsed from the stream
|
81
|
+
# object_parsed will be called, and passed the constructed object
|
82
|
+
@parser.on_parse_complete = method(:object_parsed)
|
83
|
+
end
|
84
|
+
|
85
|
+
def receive_data(data)
|
86
|
+
# continue passing chunks
|
87
|
+
@parser << data
|
88
|
+
end
|
89
|
+
```
|
79
90
|
|
80
91
|
Or if you don't need to stream it, it'll just return the built object from the parse when it's done.
|
81
|
-
|
82
|
-
|
92
|
+
NOTE: if there are going to be multiple JSON strings in the input, you *must* specify a block or callback as this
|
93
|
+
is how yajl-ruby will hand you (the caller) each object as it's parsed off the input.
|
83
94
|
|
84
|
-
|
95
|
+
``` ruby
|
96
|
+
obj = Yajl::Parser.parse(str_or_io)
|
97
|
+
```
|
85
98
|
|
86
99
|
Or how about a JSON API HTTP request?
|
87
100
|
This actually makes a request using a raw TCPSocket, then parses the JSON body right off the socket. While it's being received over the wire!
|
88
101
|
|
89
|
-
|
90
|
-
|
102
|
+
``` ruby
|
103
|
+
require 'uri'
|
104
|
+
require 'yajl/http_stream'
|
91
105
|
|
92
|
-
|
93
|
-
|
106
|
+
url = URI.parse("http://search.twitter.com/search.json?q=engineyard")
|
107
|
+
results = Yajl::HttpStream.get(url)
|
108
|
+
```
|
94
109
|
|
95
110
|
Or do the same request, with Gzip and Deflate output compression support (also supports Bzip2, if loaded):
|
96
111
|
(this does the same raw socket Request, but transparently parses the compressed response body)
|
97
112
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
113
|
+
``` ruby
|
114
|
+
require 'uri'
|
115
|
+
require 'yajl/gzip'
|
116
|
+
require 'yajl/deflate'
|
117
|
+
require 'yajl/http_stream'
|
102
118
|
|
103
|
-
|
104
|
-
|
119
|
+
url = URI.parse("http://search.twitter.com/search.json?q=engineyard")
|
120
|
+
results = Yajl::HttpStream.get(url)
|
121
|
+
```
|
105
122
|
|
106
123
|
Since yajl-ruby parses JSON as a stream, supporting API's like Twitter's Streaming API are a piece-of-cake.
|
107
|
-
You can simply supply a block to Yajl::HttpStream.get
|
124
|
+
You can simply supply a block to `Yajl::HttpStream.get`, which is used as the callback for when a JSON object has been
|
108
125
|
unserialized off the stream. For the case of this Twitter Streaming API call, the callback gets fired a few times a second (depending on your connection speed).
|
109
126
|
The code below is all that's needed to make the request and stream unserialized Ruby hashes off the response, continuously.
|
110
127
|
You'll note that I've enabled the :symbolize_keys parser option as well. Doing so is much more efficient for parsing JSON streams with
|
@@ -112,86 +129,100 @@ lots of repetitive keys - for things like result sets or multiple API requests -
|
|
112
129
|
This is because Ruby will reuse (and never GC) its symbol table. Be that as it may, if you want to parse JSON strings with random key names
|
113
130
|
it's much better to leave string keys enabled (the default), so they can get GC'd later.
|
114
131
|
|
115
|
-
|
116
|
-
|
132
|
+
``` ruby
|
133
|
+
require 'uri'
|
134
|
+
require 'yajl/http_stream'
|
117
135
|
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
136
|
+
uri = URI.parse("http://#{username}:#{password}@stream.twitter.com/spritzer.json")
|
137
|
+
Yajl::HttpStream.get(uri, :symbolize_keys => true) do |hash|
|
138
|
+
puts hash.inspect
|
139
|
+
end
|
140
|
+
```
|
122
141
|
|
123
142
|
Or how about parsing directly from a compressed file?
|
124
143
|
|
125
|
-
|
144
|
+
``` ruby
|
145
|
+
require 'yajl/bzip2'
|
126
146
|
|
127
|
-
|
128
|
-
|
147
|
+
file = File.new('some.json.bz2', 'r')
|
148
|
+
result = Yajl::Bzip2::StreamReader.parse(file)
|
149
|
+
```
|
129
150
|
|
130
|
-
|
151
|
+
### Encoding
|
131
152
|
|
132
153
|
Since yajl-ruby does everything using streams, you simply need to pass the object to encode, and the IO to write the stream to (this happens in chunks).
|
133
154
|
|
134
155
|
This allows you to encode JSON as a stream, writing directly to a socket
|
135
156
|
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
Yajl::Encoder.
|
157
|
+
``` ruby
|
158
|
+
socket = TCPSocket.new('192.168.1.101', 9000)
|
159
|
+
hash = {:foo => 12425125, :bar => "some string", ... }
|
160
|
+
encoder = Yajl::Encoder.new
|
161
|
+
Yajl::Encoder.encode(hash, socket)
|
162
|
+
```
|
140
163
|
|
141
164
|
Or what if you wanted to compress the stream over the wire?
|
142
165
|
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
166
|
+
``` ruby
|
167
|
+
require 'yajl/gzip'
|
168
|
+
socket = TCPSocket.new('192.168.1.101', 9000)
|
169
|
+
hash = {:foo => 12425125, :bar => "some string", ... }
|
170
|
+
Yajl::Gzip::StreamWriter.encode(hash, socket)
|
171
|
+
```
|
147
172
|
|
148
173
|
Or what about encoding multiple objects to JSON over the same stream?
|
149
174
|
This example will encode and send 50 JSON objects over the same stream, continuously.
|
150
175
|
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
176
|
+
``` ruby
|
177
|
+
socket = TCPSocket.new('192.168.1.101', 9000)
|
178
|
+
encoder = Yajl::Encoder.new
|
179
|
+
50.times do
|
180
|
+
hash = {:current_time => Time.now.to_f, :foo => 12425125}
|
181
|
+
encoder.encode(hash, socket)
|
182
|
+
end
|
183
|
+
```
|
184
|
+
|
185
|
+
Using `EventMachine` and you want to encode and send in chunks?
|
186
|
+
(Assume we're in an `EventMachine::Connection` instance)
|
187
|
+
|
188
|
+
``` ruby
|
189
|
+
def post_init
|
190
|
+
# Passing a :terminator character will let us determine when the encoder
|
191
|
+
# is done encoding the current object
|
192
|
+
@encoder = Yajl::Encoder.new
|
193
|
+
motd_contents = File.read("/path/to/motd.txt")
|
194
|
+
status = File.read("/path/to/huge/status_file.txt")
|
195
|
+
@motd = {:motd => motd_contents, :system_status => status}
|
196
|
+
end
|
197
|
+
|
198
|
+
def connection_completed
|
199
|
+
# The encoder will do its best to hand you data in chunks that
|
200
|
+
# are around 8kb (but you may see some that are larger)
|
201
|
+
#
|
202
|
+
# It should be noted that you could have also assigned the _on_progress_ callback
|
203
|
+
# much like you can assign the _on_parse_complete_ callback with the parser class.
|
204
|
+
# Passing a block (like below) essentially tells the encoder to use that block
|
205
|
+
# as the callback normally assigned to _on_progress_.
|
206
|
+
#
|
207
|
+
# Send our MOTD and status
|
208
|
+
@encoder.encode(@motd) do |chunk|
|
209
|
+
if chunk.nil? # got our terminator, encoding is done
|
210
|
+
close_connection_after_writing
|
211
|
+
else
|
212
|
+
send_data(chunk)
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
216
|
+
```
|
188
217
|
|
189
218
|
But to make things simple, you might just want to let yajl-ruby do all the hard work for you and just hand back
|
190
219
|
a string when it's finished. In that case, just don't provide and IO or block (or assign the on_progress callback).
|
191
220
|
|
192
|
-
|
221
|
+
``` ruby
|
222
|
+
str = Yajl::Encoder.encode(obj)
|
223
|
+
```
|
193
224
|
|
194
|
-
You can also use Yajl::Bzip2::StreamWriter and Yajl::Deflate::StreamWriter
|
225
|
+
You can also use `Yajl::Bzip2::StreamWriter` and `Yajl::Deflate::StreamWriter`. So you can pick whichever fits your CPU/bandwidth sweet-spot.
|
195
226
|
|
196
227
|
=== HTML Safety
|
197
228
|
|
@@ -199,39 +230,44 @@ If you plan on embedding the output from the encoder in the DOM, you'll want to
|
|
199
230
|
|
200
231
|
Meaning the following should be perfectly safe:
|
201
232
|
|
202
|
-
|
203
|
-
|
204
|
-
</script>
|
233
|
+
``` html
|
234
|
+
<script type="text/javascript">
|
235
|
+
var escaped_str = <%= Yajl::Encoder.encode("</script><script>alert('hi!');</script>", :html_safe => true) %>;
|
236
|
+
</script>
|
237
|
+
```
|
205
238
|
|
206
239
|
== JSON gem Compatibility API
|
207
240
|
|
208
241
|
The JSON gem compatibility API isn't enabled by default. You have to explicitly require it like so:
|
209
242
|
|
210
|
-
|
243
|
+
``` ruby
|
244
|
+
require 'yajl/json_gem'
|
245
|
+
```
|
211
246
|
|
212
|
-
That's right, you can just replace "require 'json'" with the line above and you're done!
|
247
|
+
That's right, you can just replace `"require 'json'"` with the line above and you're done!
|
213
248
|
|
214
249
|
This will require yajl-ruby itself, as well as enable its JSON gem compatibility API.
|
215
250
|
|
216
251
|
This includes the following API:
|
217
252
|
|
218
|
-
|
219
|
-
|
253
|
+
JSON.parse, JSON.generate, JSON.pretty_generate, JSON.load, JSON.dump
|
254
|
+
and all of the #to_json instance method overrides for Ruby's primitive objects
|
255
|
+
|
220
256
|
|
221
257
|
Once the compatibility API is enabled, your existing or new project should work as if the JSON gem itself were being used. Only you'll be using Yajl ;)
|
222
258
|
|
223
259
|
There are a lot more possibilities that I'd love to see other gems/plugins for someday.
|
224
260
|
|
225
261
|
Some ideas:
|
262
|
+
|
226
263
|
* parsing logs in JSON format
|
227
264
|
* a Rails plugin - DONE! (http://github.com/technoweenie/yajl-rails)
|
228
265
|
* official support in Rails 3 - DONE (http://github.com/rails/rails/commit/a96bf4ab5e73fccdafb78b99e8a122cc2172b505)
|
229
|
-
|
266
|
+
* and is the default (if installed) - http://github.com/rails/rails/commit/63bb955a99eb46e257655c93dd64e86ebbf05651
|
230
267
|
* Rack middleware (ideally the JSON body could be handed to the parser while it's still being received, this is apparently possible with Unicorn)
|
231
268
|
* JSON API clients (http://github.com/brianmario/freckle-api)
|
232
|
-
* ???
|
233
269
|
|
234
|
-
|
270
|
+
## Benchmarks
|
235
271
|
|
236
272
|
After I finished implementation - this library performs close to the same as the current JSON.parse (C gem) does on small/medium files.
|
237
273
|
|
@@ -242,69 +278,69 @@ Since it's able to parse the stream in chunks, its memory requirements are very,
|
|
242
278
|
|
243
279
|
Here's what parsing a 2.43MB JSON file off the filesystem 20 times looks like:
|
244
280
|
|
245
|
-
|
281
|
+
### Memory Usage
|
246
282
|
|
247
|
-
|
283
|
+
#### Average
|
248
284
|
|
249
285
|
* Yajl::Parser#parse: 32MB
|
250
286
|
* JSON.parse: 54MB
|
251
287
|
* ActiveSupport::JSON.decode: 63MB
|
252
288
|
|
253
|
-
|
289
|
+
#### Peak
|
254
290
|
|
255
291
|
* Yajl::Parser#parse: 32MB
|
256
292
|
* JSON.parse: 57MB
|
257
293
|
* ActiveSupport::JSON.decode: 67MB
|
258
294
|
|
259
|
-
|
295
|
+
### Parse Time
|
260
296
|
|
261
297
|
* Yajl::Parser#parse: 4.54s
|
262
298
|
* JSON.parse: 5.47s
|
263
299
|
* ActiveSupport::JSON.decode: 64.42s
|
264
300
|
|
265
|
-
|
301
|
+
### Encode Time
|
266
302
|
|
267
303
|
* Yajl::Encoder#encode: 3.59s
|
268
304
|
* JSON#to_json: 6.2s
|
269
305
|
* ActiveSupport::JSON.encode: 45.58s
|
270
306
|
|
271
|
-
|
307
|
+
### Compared to YAML
|
272
308
|
|
273
309
|
NOTE: I converted the 2.4MB JSON file to YAML for this test.
|
274
310
|
|
275
|
-
|
311
|
+
#### Parse Time (from their respective formats)
|
276
312
|
|
277
313
|
* Yajl::Parser#parse: 4.33s
|
278
314
|
* JSON.parse: 5.37s
|
279
315
|
* YAML.load: 19.47s
|
280
316
|
|
281
|
-
|
317
|
+
#### Encode Time (to their respective formats)
|
282
318
|
|
283
319
|
* Yajl::Encoder#encode: 3.47s
|
284
320
|
* JSON#to_json: 6.6s
|
285
321
|
* YAML.dump(obj, io): 1309.93s
|
286
322
|
|
287
|
-
|
323
|
+
### Compared to Marshal.load/Marshal.dump
|
288
324
|
|
289
325
|
NOTE: I converted the 2.4MB JSON file to a Hash and a dump file from Marshal.dump for this test.
|
290
326
|
|
291
|
-
|
327
|
+
#### Parse Time (from their respective formats)
|
292
328
|
|
293
329
|
* Yajl::Parser#parse: 4.54s
|
294
330
|
* JSON.parse: 7.40s
|
295
331
|
* Marshal.load: 7s
|
296
332
|
|
297
|
-
|
333
|
+
#### Encode Time (to their respective formats)
|
298
334
|
|
299
335
|
* Yajl::Encoder#encode: 2.39s
|
300
336
|
* JSON#to_json: 8.37s
|
301
337
|
* Marshal.dump: 4.66s
|
302
338
|
|
303
|
-
|
339
|
+
## Third Party Sources Bundled
|
304
340
|
|
305
341
|
This project includes code from the BSD licensed yajl project, copyright 2007-2009 Lloyd Hilaiel
|
306
342
|
|
307
|
-
|
343
|
+
## Special Thanks & Contributors
|
308
344
|
|
309
345
|
For those of you using yajl-ruby out in the wild, please hit me up on Twitter (brianmario) or send me a message here on the Githubs describing the site and how you're using it. I'd love to get a list going!
|
310
346
|
|
data/benchmark/encode.rb
CHANGED
data/benchmark/http.rb
CHANGED
data/benchmark/parse.rb
CHANGED
data/benchmark/parse_stream.rb
CHANGED
data/ext/yajl/extconf.rb
CHANGED
data/ext/yajl/yajl_ext.c
CHANGED
@@ -187,11 +187,13 @@ void yajl_encode_part(void * wrapper, VALUE obj, VALUE io) {
|
|
187
187
|
default:
|
188
188
|
if (rb_respond_to(obj, intern_to_json)) {
|
189
189
|
str = rb_funcall(obj, intern_to_json, 0);
|
190
|
+
Check_Type(str, T_STRING);
|
190
191
|
cptr = RSTRING_PTR(str);
|
191
192
|
len = RSTRING_LEN(str);
|
192
193
|
status = yajl_gen_number(w->encoder, cptr, len);
|
193
194
|
} else {
|
194
195
|
str = rb_funcall(obj, intern_to_s, 0);
|
196
|
+
Check_Type(str, T_STRING);
|
195
197
|
cptr = RSTRING_PTR(str);
|
196
198
|
len = RSTRING_LEN(str);
|
197
199
|
status = yajl_gen_string(w->encoder, (const unsigned char *)cptr, len);
|
data/lib/yajl.rb
CHANGED
data/lib/yajl/bzip2.rb
CHANGED
data/lib/yajl/deflate.rb
CHANGED
data/lib/yajl/gzip.rb
CHANGED
data/lib/yajl/http_stream.rb
CHANGED
data/lib/yajl/json_gem.rb
CHANGED
data/lib/yajl/version.rb
CHANGED
@@ -7,6 +7,18 @@ class Dummy2
|
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
|
+
class TheMindKiller
|
11
|
+
def to_json
|
12
|
+
@wait_i_dont_exist_this_is_a_horrible_mistake_dont_do_it_aiiiiiiiiieeeeeee
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class TheMindKillerDuce
|
17
|
+
def to_s
|
18
|
+
@wait_i_dont_exist_this_is_a_horrible_mistake_dont_do_it_aiiiiiiiiieeeeeee
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
10
22
|
describe "Yajl JSON encoder" do
|
11
23
|
FILES = Dir[File.dirname(__FILE__)+'/../../benchmark/subjects/*.json']
|
12
24
|
|
@@ -244,4 +256,16 @@ describe "Yajl JSON encoder" do
|
|
244
256
|
unsafe_encoder = Yajl::Encoder.new
|
245
257
|
unsafe_encoder.encode("</script>").should_not eql("\"<\\/script>\"")
|
246
258
|
end
|
259
|
+
|
260
|
+
it "return value of #to_json must be a string" do
|
261
|
+
lambda {
|
262
|
+
Yajl::Encoder.encode(TheMindKiller.new)
|
263
|
+
}.should raise_error(TypeError)
|
264
|
+
end
|
265
|
+
|
266
|
+
it "return value of #to_s must be a string" do
|
267
|
+
lambda {
|
268
|
+
Yajl::Encoder.encode(TheMindKillerDuce.new)
|
269
|
+
}.should raise_error(TypeError)
|
270
|
+
end
|
247
271
|
end
|
data/spec/global/global_spec.rb
CHANGED
data/spec/http/http_get_spec.rb
CHANGED
data/spec/http/http_post_spec.rb
CHANGED
data/spec/http/http_put_spec.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
data/tasks/compile.rake
CHANGED
@@ -1,11 +1,10 @@
|
|
1
|
-
|
2
|
-
require "rake/extensiontask"
|
1
|
+
require 'rake/extensiontask'
|
3
2
|
|
4
3
|
def gemspec
|
5
4
|
@clean_gemspec ||= eval(File.read(File.expand_path('../../yajl-ruby.gemspec', __FILE__)))
|
6
5
|
end
|
7
6
|
|
8
|
-
Rake::ExtensionTask.new(
|
7
|
+
Rake::ExtensionTask.new('yajl', gemspec) do |ext|
|
9
8
|
# automatically add build options to avoid need of manual input
|
10
9
|
ext.cross_compile = true
|
11
10
|
ext.cross_platform = ['x86-mingw32', 'x86-mswin32-60']
|
@@ -23,15 +22,14 @@ end
|
|
23
22
|
Rake::Task[:spec].prerequisites << :compile
|
24
23
|
|
25
24
|
file 'lib/yajl/yajl.rb' do |t|
|
26
|
-
name = gemspec.name
|
27
25
|
File.open(t.name, 'wb') do |f|
|
28
26
|
f.write <<-eoruby
|
29
27
|
RUBY_VERSION =~ /(\\d+.\\d+)/
|
30
|
-
require "
|
28
|
+
require "yajl/\#{$1}/yajl"
|
31
29
|
eoruby
|
32
30
|
end
|
33
31
|
end
|
34
32
|
|
35
33
|
if Rake::Task.task_defined?(:cross)
|
36
|
-
Rake::Task[:cross].prerequisites <<
|
34
|
+
Rake::Task[:cross].prerequisites << 'lib/yajl/yajl.rb'
|
37
35
|
end
|
data/yajl-ruby.gemspec
CHANGED
@@ -7,12 +7,9 @@ Gem::Specification.new do |s|
|
|
7
7
|
s.date = Time.now.utc.strftime("%Y-%m-%d")
|
8
8
|
s.email = %q{seniorlopez@gmail.com}
|
9
9
|
s.extensions = ["ext/yajl/extconf.rb"]
|
10
|
-
s.extra_rdoc_files = [
|
11
|
-
"README.rdoc"
|
12
|
-
]
|
13
10
|
s.files = `git ls-files`.split("\n")
|
14
11
|
s.homepage = %q{http://github.com/brianmario/yajl-ruby}
|
15
|
-
s.require_paths = ["lib"
|
12
|
+
s.require_paths = ["lib"]
|
16
13
|
s.rubygems_version = %q{1.4.2}
|
17
14
|
s.summary = %q{Ruby C bindings to the excellent Yajl JSON stream-based parser library.}
|
18
15
|
s.test_files = `git ls-files spec examples`.split("\n")
|
metadata
CHANGED
@@ -1,99 +1,75 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: yajl-ruby
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 8
|
9
|
-
- 3
|
10
|
-
version: 0.8.3
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Brian Lopez
|
14
9
|
- Lloyd Hilaiel
|
15
10
|
autorequire:
|
16
11
|
bindir: bin
|
17
12
|
cert_chain: []
|
18
|
-
|
19
|
-
date: 2011-08-16 00:00:00 -07:00
|
13
|
+
date: 2011-09-16 00:00:00.000000000 -07:00
|
20
14
|
default_executable:
|
21
|
-
dependencies:
|
22
|
-
- !ruby/object:Gem::Dependency
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
23
17
|
name: rake-compiler
|
24
|
-
|
25
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
18
|
+
requirement: &70167590495720 !ruby/object:Gem::Requirement
|
26
19
|
none: false
|
27
|
-
requirements:
|
28
|
-
- -
|
29
|
-
- !ruby/object:Gem::Version
|
30
|
-
hash: 9
|
31
|
-
segments:
|
32
|
-
- 0
|
33
|
-
- 7
|
34
|
-
- 5
|
20
|
+
requirements:
|
21
|
+
- - ! '>='
|
22
|
+
- !ruby/object:Gem::Version
|
35
23
|
version: 0.7.5
|
36
24
|
type: :development
|
37
|
-
version_requirements: *id001
|
38
|
-
- !ruby/object:Gem::Dependency
|
39
|
-
name: rspec
|
40
25
|
prerelease: false
|
41
|
-
|
26
|
+
version_requirements: *70167590495720
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: &70167590495220 !ruby/object:Gem::Requirement
|
42
30
|
none: false
|
43
|
-
requirements:
|
44
|
-
- -
|
45
|
-
- !ruby/object:Gem::Version
|
46
|
-
hash: 15
|
47
|
-
segments:
|
48
|
-
- 2
|
49
|
-
- 0
|
50
|
-
- 0
|
31
|
+
requirements:
|
32
|
+
- - ! '>='
|
33
|
+
- !ruby/object:Gem::Version
|
51
34
|
version: 2.0.0
|
52
35
|
type: :development
|
53
|
-
version_requirements: *id002
|
54
|
-
- !ruby/object:Gem::Dependency
|
55
|
-
name: activesupport
|
56
36
|
prerelease: false
|
57
|
-
|
37
|
+
version_requirements: *70167590495220
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: activesupport
|
40
|
+
requirement: &70167590494840 !ruby/object:Gem::Requirement
|
58
41
|
none: false
|
59
|
-
requirements:
|
60
|
-
- -
|
61
|
-
- !ruby/object:Gem::Version
|
62
|
-
|
63
|
-
segments:
|
64
|
-
- 0
|
65
|
-
version: "0"
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
66
46
|
type: :development
|
67
|
-
version_requirements: *id003
|
68
|
-
- !ruby/object:Gem::Dependency
|
69
|
-
name: json
|
70
47
|
prerelease: false
|
71
|
-
|
48
|
+
version_requirements: *70167590494840
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: json
|
51
|
+
requirement: &70167590494380 !ruby/object:Gem::Requirement
|
72
52
|
none: false
|
73
|
-
requirements:
|
74
|
-
- -
|
75
|
-
- !ruby/object:Gem::Version
|
76
|
-
|
77
|
-
segments:
|
78
|
-
- 0
|
79
|
-
version: "0"
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
80
57
|
type: :development
|
81
|
-
|
58
|
+
prerelease: false
|
59
|
+
version_requirements: *70167590494380
|
82
60
|
description:
|
83
61
|
email: seniorlopez@gmail.com
|
84
62
|
executables: []
|
85
|
-
|
86
|
-
extensions:
|
63
|
+
extensions:
|
87
64
|
- ext/yajl/extconf.rb
|
88
|
-
extra_rdoc_files:
|
89
|
-
|
90
|
-
files:
|
65
|
+
extra_rdoc_files: []
|
66
|
+
files:
|
91
67
|
- .gitignore
|
92
68
|
- .rspec
|
93
69
|
- CHANGELOG.md
|
94
70
|
- Gemfile
|
95
71
|
- MIT-LICENSE
|
96
|
-
- README.
|
72
|
+
- README.md
|
97
73
|
- Rakefile
|
98
74
|
- benchmark/encode.rb
|
99
75
|
- benchmark/encode_json_and_marshal.rb
|
@@ -239,39 +215,29 @@ files:
|
|
239
215
|
has_rdoc: true
|
240
216
|
homepage: http://github.com/brianmario/yajl-ruby
|
241
217
|
licenses: []
|
242
|
-
|
243
218
|
post_install_message:
|
244
219
|
rdoc_options: []
|
245
|
-
|
246
|
-
require_paths:
|
220
|
+
require_paths:
|
247
221
|
- lib
|
248
|
-
|
249
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
222
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
250
223
|
none: false
|
251
|
-
requirements:
|
252
|
-
- -
|
253
|
-
- !ruby/object:Gem::Version
|
254
|
-
|
255
|
-
|
256
|
-
- 0
|
257
|
-
version: "0"
|
258
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
224
|
+
requirements:
|
225
|
+
- - ! '>='
|
226
|
+
- !ruby/object:Gem::Version
|
227
|
+
version: '0'
|
228
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
259
229
|
none: false
|
260
|
-
requirements:
|
261
|
-
- -
|
262
|
-
- !ruby/object:Gem::Version
|
263
|
-
|
264
|
-
segments:
|
265
|
-
- 0
|
266
|
-
version: "0"
|
230
|
+
requirements:
|
231
|
+
- - ! '>='
|
232
|
+
- !ruby/object:Gem::Version
|
233
|
+
version: '0'
|
267
234
|
requirements: []
|
268
|
-
|
269
235
|
rubyforge_project:
|
270
236
|
rubygems_version: 1.6.2
|
271
237
|
signing_key:
|
272
238
|
specification_version: 3
|
273
239
|
summary: Ruby C bindings to the excellent Yajl JSON stream-based parser library.
|
274
|
-
test_files:
|
240
|
+
test_files:
|
275
241
|
- examples/encoding/chunked_encoding.rb
|
276
242
|
- examples/encoding/one_shot.rb
|
277
243
|
- examples/encoding/to_an_io.rb
|