yajl-ruby 1.0.0-x86-mingw32
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 +12 -0
- data/.rspec +2 -0
- data/CHANGELOG.md +327 -0
- data/Gemfile +3 -0
- data/MIT-LICENSE +20 -0
- data/README.md +362 -0
- data/Rakefile +2 -0
- data/benchmark/encode.rb +72 -0
- data/benchmark/encode_json_and_marshal.rb +42 -0
- data/benchmark/encode_json_and_yaml.rb +53 -0
- data/benchmark/http.rb +32 -0
- data/benchmark/parse.rb +94 -0
- data/benchmark/parse_json_and_marshal.rb +50 -0
- data/benchmark/parse_json_and_yaml.rb +55 -0
- data/benchmark/parse_stream.rb +54 -0
- data/benchmark/subjects/item.json +1 -0
- data/benchmark/subjects/ohai.json +1216 -0
- data/benchmark/subjects/ohai.marshal_dump +0 -0
- data/benchmark/subjects/ohai.yml +975 -0
- data/benchmark/subjects/twitter_search.json +1 -0
- data/benchmark/subjects/twitter_stream.json +430 -0
- data/benchmark/subjects/unicode.json +1 -0
- data/examples/encoding/chunked_encoding.rb +27 -0
- data/examples/encoding/one_shot.rb +13 -0
- data/examples/encoding/to_an_io.rb +12 -0
- data/examples/http/twitter_search_api.rb +12 -0
- data/examples/http/twitter_stream_api.rb +26 -0
- data/examples/parsing/from_file.rb +14 -0
- data/examples/parsing/from_stdin.rb +9 -0
- data/examples/parsing/from_string.rb +13 -0
- data/ext/yajl/api/yajl_common.h +89 -0
- data/ext/yajl/api/yajl_gen.h +161 -0
- data/ext/yajl/api/yajl_parse.h +196 -0
- data/ext/yajl/api/yajl_version.h +23 -0
- data/ext/yajl/extconf.rb +7 -0
- data/ext/yajl/yajl.c +164 -0
- data/ext/yajl/yajl_alloc.c +65 -0
- data/ext/yajl/yajl_alloc.h +50 -0
- data/ext/yajl/yajl_buf.c +119 -0
- data/ext/yajl/yajl_buf.h +73 -0
- data/ext/yajl/yajl_bytestack.h +85 -0
- data/ext/yajl/yajl_encode.c +201 -0
- data/ext/yajl/yajl_encode.h +52 -0
- data/ext/yajl/yajl_ext.c +905 -0
- data/ext/yajl/yajl_ext.h +135 -0
- data/ext/yajl/yajl_gen.c +344 -0
- data/ext/yajl/yajl_lex.c +748 -0
- data/ext/yajl/yajl_lex.h +135 -0
- data/ext/yajl/yajl_parser.c +450 -0
- data/ext/yajl/yajl_parser.h +82 -0
- data/ext/yajl/yajl_version.c +7 -0
- data/lib/yajl.rb +75 -0
- data/lib/yajl/1.8/yajl.so +0 -0
- data/lib/yajl/1.9/yajl.so +0 -0
- data/lib/yajl/bzip2.rb +11 -0
- data/lib/yajl/bzip2/stream_reader.rb +31 -0
- data/lib/yajl/bzip2/stream_writer.rb +14 -0
- data/lib/yajl/deflate.rb +6 -0
- data/lib/yajl/deflate/stream_reader.rb +43 -0
- data/lib/yajl/deflate/stream_writer.rb +20 -0
- data/lib/yajl/gzip.rb +6 -0
- data/lib/yajl/gzip/stream_reader.rb +30 -0
- data/lib/yajl/gzip/stream_writer.rb +13 -0
- data/lib/yajl/http_stream.rb +212 -0
- data/lib/yajl/json_gem.rb +15 -0
- data/lib/yajl/json_gem/encoding.rb +51 -0
- data/lib/yajl/json_gem/parsing.rb +26 -0
- data/lib/yajl/version.rb +3 -0
- data/lib/yajl/yajl.rb +2 -0
- data/spec/encoding/encoding_spec.rb +271 -0
- data/spec/global/global_spec.rb +54 -0
- data/spec/http/fixtures/http.bzip2.dump +0 -0
- data/spec/http/fixtures/http.chunked.dump +11 -0
- data/spec/http/fixtures/http.deflate.dump +0 -0
- data/spec/http/fixtures/http.error.dump +12 -0
- data/spec/http/fixtures/http.gzip.dump +0 -0
- data/spec/http/fixtures/http.html.dump +1220 -0
- data/spec/http/fixtures/http.raw.dump +1226 -0
- data/spec/http/http_delete_spec.rb +98 -0
- data/spec/http/http_error_spec.rb +32 -0
- data/spec/http/http_get_spec.rb +109 -0
- data/spec/http/http_post_spec.rb +123 -0
- data/spec/http/http_put_spec.rb +105 -0
- data/spec/http/http_stream_options_spec.rb +27 -0
- data/spec/json_gem_compatibility/compatibility_spec.rb +203 -0
- data/spec/parsing/active_support_spec.rb +64 -0
- data/spec/parsing/chunked_spec.rb +96 -0
- data/spec/parsing/fixtures/fail.15.json +1 -0
- data/spec/parsing/fixtures/fail.16.json +1 -0
- data/spec/parsing/fixtures/fail.17.json +1 -0
- data/spec/parsing/fixtures/fail.26.json +1 -0
- data/spec/parsing/fixtures/fail11.json +1 -0
- data/spec/parsing/fixtures/fail12.json +1 -0
- data/spec/parsing/fixtures/fail13.json +1 -0
- data/spec/parsing/fixtures/fail14.json +1 -0
- data/spec/parsing/fixtures/fail19.json +1 -0
- data/spec/parsing/fixtures/fail20.json +1 -0
- data/spec/parsing/fixtures/fail21.json +1 -0
- data/spec/parsing/fixtures/fail22.json +1 -0
- data/spec/parsing/fixtures/fail23.json +1 -0
- data/spec/parsing/fixtures/fail24.json +1 -0
- data/spec/parsing/fixtures/fail25.json +1 -0
- data/spec/parsing/fixtures/fail27.json +2 -0
- data/spec/parsing/fixtures/fail28.json +2 -0
- data/spec/parsing/fixtures/fail3.json +1 -0
- data/spec/parsing/fixtures/fail4.json +1 -0
- data/spec/parsing/fixtures/fail5.json +1 -0
- data/spec/parsing/fixtures/fail6.json +1 -0
- data/spec/parsing/fixtures/fail9.json +1 -0
- data/spec/parsing/fixtures/pass.array.json +6 -0
- data/spec/parsing/fixtures/pass.codepoints_from_unicode_org.json +1 -0
- data/spec/parsing/fixtures/pass.contacts.json +1 -0
- data/spec/parsing/fixtures/pass.db100.xml.json +1 -0
- data/spec/parsing/fixtures/pass.db1000.xml.json +1 -0
- data/spec/parsing/fixtures/pass.dc_simple_with_comments.json +11 -0
- data/spec/parsing/fixtures/pass.deep_arrays.json +1 -0
- data/spec/parsing/fixtures/pass.difficult_json_c_test_case.json +1 -0
- data/spec/parsing/fixtures/pass.difficult_json_c_test_case_with_comments.json +1 -0
- data/spec/parsing/fixtures/pass.doubles.json +1 -0
- data/spec/parsing/fixtures/pass.empty_array.json +1 -0
- data/spec/parsing/fixtures/pass.empty_string.json +1 -0
- data/spec/parsing/fixtures/pass.escaped_bulgarian.json +4 -0
- data/spec/parsing/fixtures/pass.escaped_foobar.json +1 -0
- data/spec/parsing/fixtures/pass.item.json +1 -0
- data/spec/parsing/fixtures/pass.json-org-sample1.json +23 -0
- data/spec/parsing/fixtures/pass.json-org-sample2.json +11 -0
- data/spec/parsing/fixtures/pass.json-org-sample3.json +26 -0
- data/spec/parsing/fixtures/pass.json-org-sample4-nows.json +88 -0
- data/spec/parsing/fixtures/pass.json-org-sample4.json +89 -0
- data/spec/parsing/fixtures/pass.json-org-sample5.json +27 -0
- data/spec/parsing/fixtures/pass.map-spain.xml.json +1 -0
- data/spec/parsing/fixtures/pass.ns-invoice100.xml.json +1 -0
- data/spec/parsing/fixtures/pass.ns-soap.xml.json +1 -0
- data/spec/parsing/fixtures/pass.numbers-fp-4k.json +6 -0
- data/spec/parsing/fixtures/pass.numbers-fp-64k.json +61 -0
- data/spec/parsing/fixtures/pass.numbers-int-4k.json +11 -0
- data/spec/parsing/fixtures/pass.numbers-int-64k.json +154 -0
- data/spec/parsing/fixtures/pass.twitter-search.json +1 -0
- data/spec/parsing/fixtures/pass.twitter-search2.json +1 -0
- data/spec/parsing/fixtures/pass.unicode.json +3315 -0
- data/spec/parsing/fixtures/pass.yelp.json +1 -0
- data/spec/parsing/fixtures/pass1.json +56 -0
- data/spec/parsing/fixtures/pass2.json +1 -0
- data/spec/parsing/fixtures/pass3.json +6 -0
- data/spec/parsing/fixtures_spec.rb +40 -0
- data/spec/parsing/one_off_spec.rb +85 -0
- data/spec/rcov.opts +3 -0
- data/spec/spec_helper.rb +16 -0
- data/tasks/compile.rake +35 -0
- data/tasks/rspec.rake +16 -0
- data/yajl-ruby.gemspec +24 -0
- metadata +335 -0
@@ -0,0 +1,15 @@
|
|
1
|
+
puts "DEPRECATION WARNING: Yajl's JSON gem compatibility API is going to be removed in 2.0"
|
2
|
+
|
3
|
+
require 'yajl' unless defined?(Yajl::Parser)
|
4
|
+
require 'yajl/json_gem/parsing'
|
5
|
+
require 'yajl/json_gem/encoding'
|
6
|
+
|
7
|
+
module ::Kernel
|
8
|
+
def JSON(object, opts = {})
|
9
|
+
if object.respond_to? :to_s
|
10
|
+
JSON.parse(object.to_s, JSON.default_options.merge(opts))
|
11
|
+
else
|
12
|
+
JSON.generate(object, opts)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'yajl' unless defined?(Yajl::Parser)
|
2
|
+
|
3
|
+
# NOTE: this is probably temporary until I can split out the JSON compat C code into it's own
|
4
|
+
# extension that can be included when this file is.
|
5
|
+
Yajl::Encoder.enable_json_gem_compatability
|
6
|
+
|
7
|
+
# Our fallback to_json definition
|
8
|
+
unless defined?(ActiveSupport)
|
9
|
+
class Object
|
10
|
+
def to_json(*args, &block)
|
11
|
+
"\"#{to_s}\""
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module JSON
|
17
|
+
class JSONError < StandardError; end unless defined?(JSON::JSONError)
|
18
|
+
class GeneratorError < JSONError; end unless defined?(JSON::GeneratorError)
|
19
|
+
|
20
|
+
def self.generate(obj, opts={})
|
21
|
+
begin
|
22
|
+
options_map = {}
|
23
|
+
if opts.has_key?(:indent)
|
24
|
+
options_map[:pretty] = true
|
25
|
+
options_map[:indent] = opts[:indent]
|
26
|
+
end
|
27
|
+
Yajl::Encoder.encode(obj, options_map)
|
28
|
+
rescue Yajl::EncodeError => e
|
29
|
+
raise JSON::GeneratorError, e.message
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.pretty_generate(obj, opts={})
|
34
|
+
begin
|
35
|
+
options_map = {}
|
36
|
+
options_map[:pretty] = true
|
37
|
+
options_map[:indent] = opts[:indent] if opts.has_key?(:indent)
|
38
|
+
Yajl::Encoder.encode(obj, options_map)
|
39
|
+
rescue Yajl::EncodeError => e
|
40
|
+
raise JSON::GeneratorError, e.message
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.dump(obj, io=nil, *args)
|
45
|
+
begin
|
46
|
+
Yajl::Encoder.encode(obj, io)
|
47
|
+
rescue Yajl::EncodeError => e
|
48
|
+
raise JSON::GeneratorError, e.message
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'yajl' unless defined?(Yajl::Parser)
|
2
|
+
|
3
|
+
module JSON
|
4
|
+
class JSONError < StandardError; end unless defined?(JSON::JSONError)
|
5
|
+
class ParserError < JSONError; end unless defined?(JSON::ParserError)
|
6
|
+
|
7
|
+
def self.default_options
|
8
|
+
@default_options ||= {:symbolize_keys => false}
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.parse(str, opts=JSON.default_options)
|
12
|
+
begin
|
13
|
+
Yajl::Parser.parse(str, opts)
|
14
|
+
rescue Yajl::ParseError => e
|
15
|
+
raise JSON::ParserError, e.message
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.load(input, *args)
|
20
|
+
begin
|
21
|
+
Yajl::Parser.parse(input, default_options)
|
22
|
+
rescue Yajl::ParseError => e
|
23
|
+
raise JSON::ParserError, e.message
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/yajl/version.rb
ADDED
data/lib/yajl/yajl.rb
ADDED
@@ -0,0 +1,271 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')
|
3
|
+
|
4
|
+
class Dummy2
|
5
|
+
def to_json
|
6
|
+
"{\"hawtness\":true}"
|
7
|
+
end
|
8
|
+
end
|
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
|
+
|
22
|
+
describe "Yajl JSON encoder" do
|
23
|
+
FILES = Dir[File.dirname(__FILE__)+'/../../benchmark/subjects/*.json']
|
24
|
+
|
25
|
+
FILES.each do |file|
|
26
|
+
it "should encode #{File.basename(file)} to an IO" do
|
27
|
+
# we don't care about testing the stream subject as it has multiple JSON strings in it
|
28
|
+
if File.basename(file) != 'twitter_stream.json'
|
29
|
+
input = File.new(File.expand_path(file), 'r')
|
30
|
+
io = StringIO.new
|
31
|
+
encoder = Yajl::Encoder.new
|
32
|
+
hash = Yajl::Parser.parse(input)
|
33
|
+
encoder.encode(hash, io)
|
34
|
+
io.rewind
|
35
|
+
hash2 = Yajl::Parser.parse(io)
|
36
|
+
io.close
|
37
|
+
input.close
|
38
|
+
hash.should == hash2
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
FILES.each do |file|
|
44
|
+
it "should encode #{File.basename(file)} and return a String" do
|
45
|
+
# we don't care about testing the stream subject as it has multiple JSON strings in it
|
46
|
+
if File.basename(file) != 'twitter_stream.json'
|
47
|
+
input = File.new(File.expand_path(file), 'r')
|
48
|
+
encoder = Yajl::Encoder.new
|
49
|
+
hash = Yajl::Parser.parse(input)
|
50
|
+
output = encoder.encode(hash)
|
51
|
+
hash2 = Yajl::Parser.parse(output)
|
52
|
+
input.close
|
53
|
+
hash.should == hash2
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
FILES.each do |file|
|
59
|
+
it "should encode #{File.basename(file)} call the passed block, passing it a String" do
|
60
|
+
# we don't care about testing the stream subject as it has multiple JSON strings in it
|
61
|
+
if File.basename(file) != 'twitter_stream.json'
|
62
|
+
input = File.new(File.expand_path(file), 'r')
|
63
|
+
encoder = Yajl::Encoder.new
|
64
|
+
hash = Yajl::Parser.parse(input)
|
65
|
+
output = ''
|
66
|
+
encoder.encode(hash) do |json_str|
|
67
|
+
output << json_str
|
68
|
+
end
|
69
|
+
hash2 = Yajl::Parser.parse(output)
|
70
|
+
input.close
|
71
|
+
hash.should == hash2
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should encode with :pretty turned on and a single space indent, to an IO" do
|
77
|
+
output = "{\n \"foo\": 1234\n}"
|
78
|
+
obj = {:foo => 1234}
|
79
|
+
io = StringIO.new
|
80
|
+
encoder = Yajl::Encoder.new(:pretty => true, :indent => ' ')
|
81
|
+
encoder.encode(obj, io)
|
82
|
+
io.rewind
|
83
|
+
io.read.should == output
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should encode with :pretty turned on and a single space indent, and return a String" do
|
87
|
+
output = "{\n \"foo\": 1234\n}"
|
88
|
+
obj = {:foo => 1234}
|
89
|
+
encoder = Yajl::Encoder.new(:pretty => true, :indent => ' ')
|
90
|
+
output = encoder.encode(obj)
|
91
|
+
output.should == output
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should encode with :pretty turned on and a tab character indent, to an IO" do
|
95
|
+
output = "{\n\t\"foo\": 1234\n}"
|
96
|
+
obj = {:foo => 1234}
|
97
|
+
io = StringIO.new
|
98
|
+
encoder = Yajl::Encoder.new(:pretty => true, :indent => "\t")
|
99
|
+
encoder.encode(obj, io)
|
100
|
+
io.rewind
|
101
|
+
io.read.should == output
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should encode with :pretty turned on and a tab character indent, and return a String" do
|
105
|
+
output = "{\n\t\"foo\": 1234\n}"
|
106
|
+
obj = {:foo => 1234}
|
107
|
+
encoder = Yajl::Encoder.new(:pretty => true, :indent => "\t")
|
108
|
+
output = encoder.encode(obj)
|
109
|
+
output.should == output
|
110
|
+
end
|
111
|
+
|
112
|
+
it "should encode with it's class method with :pretty and a tab character indent options set, to an IO" do
|
113
|
+
output = "{\n\t\"foo\": 1234\n}"
|
114
|
+
obj = {:foo => 1234}
|
115
|
+
io = StringIO.new
|
116
|
+
Yajl::Encoder.encode(obj, io, :pretty => true, :indent => "\t")
|
117
|
+
io.rewind
|
118
|
+
io.read.should == output
|
119
|
+
end
|
120
|
+
|
121
|
+
it "should encode with it's class method with :pretty and a tab character indent options set, and return a String" do
|
122
|
+
output = "{\n\t\"foo\": 1234\n}"
|
123
|
+
obj = {:foo => 1234}
|
124
|
+
output = Yajl::Encoder.encode(obj, :pretty => true, :indent => "\t")
|
125
|
+
output.should == output
|
126
|
+
end
|
127
|
+
|
128
|
+
it "should encode with it's class method with :pretty and a tab character indent options set, to a block" do
|
129
|
+
output = "{\n\t\"foo\": 1234\n}"
|
130
|
+
obj = {:foo => 1234}
|
131
|
+
output = ''
|
132
|
+
Yajl::Encoder.encode(obj, :pretty => true, :indent => "\t") do |json_str|
|
133
|
+
output = json_str
|
134
|
+
end
|
135
|
+
output.should == output
|
136
|
+
end
|
137
|
+
|
138
|
+
it "should encode multiple objects into a single stream, to an IO" do
|
139
|
+
io = StringIO.new
|
140
|
+
obj = {:foo => 1234}
|
141
|
+
encoder = Yajl::Encoder.new
|
142
|
+
5.times do
|
143
|
+
encoder.encode(obj, io)
|
144
|
+
end
|
145
|
+
io.rewind
|
146
|
+
output = "{\"foo\":1234}{\"foo\":1234}{\"foo\":1234}{\"foo\":1234}{\"foo\":1234}"
|
147
|
+
io.read.should == output
|
148
|
+
end
|
149
|
+
|
150
|
+
it "should encode multiple objects into a single stream, and return a String" do
|
151
|
+
obj = {:foo => 1234}
|
152
|
+
encoder = Yajl::Encoder.new
|
153
|
+
json_output = ''
|
154
|
+
5.times do
|
155
|
+
json_output << encoder.encode(obj)
|
156
|
+
end
|
157
|
+
output = "{\"foo\":1234}{\"foo\":1234}{\"foo\":1234}{\"foo\":1234}{\"foo\":1234}"
|
158
|
+
json_output.should == output
|
159
|
+
end
|
160
|
+
|
161
|
+
it "should encode all map keys as strings" do
|
162
|
+
Yajl::Encoder.encode({1=>1}).should eql("{\"1\":1}")
|
163
|
+
end
|
164
|
+
|
165
|
+
it "should check for and call #to_json if it exists on custom objects" do
|
166
|
+
d = Dummy2.new
|
167
|
+
Yajl::Encoder.encode({:foo => d}).should eql('{"foo":{"hawtness":true}}')
|
168
|
+
end
|
169
|
+
|
170
|
+
it "should encode a hash where the key and value can be symbols" do
|
171
|
+
Yajl::Encoder.encode({:foo => :bar}).should eql('{"foo":"bar"}')
|
172
|
+
end
|
173
|
+
|
174
|
+
it "should encode using a newline or nil terminator" do
|
175
|
+
Yajl::Encoder.new(:terminator => "\n").encode({:foo => :bar}).should eql("{\"foo\":\"bar\"}\n")
|
176
|
+
Yajl::Encoder.new(:terminator => nil).encode({:foo => :bar}).should eql("{\"foo\":\"bar\"}")
|
177
|
+
end
|
178
|
+
|
179
|
+
it "should encode using a newline or nil terminator, to an IO" do
|
180
|
+
s = StringIO.new
|
181
|
+
Yajl::Encoder.new(:terminator => "\n").encode({:foo => :bar}, s)
|
182
|
+
s.rewind
|
183
|
+
s.read.should eql("{\"foo\":\"bar\"}\n")
|
184
|
+
|
185
|
+
s = StringIO.new
|
186
|
+
Yajl::Encoder.new(:terminator => nil).encode({:foo => :bar}, s)
|
187
|
+
s.rewind
|
188
|
+
s.read.should eql("{\"foo\":\"bar\"}")
|
189
|
+
end
|
190
|
+
|
191
|
+
it "should encode using a newline or nil terminator, using a block" do
|
192
|
+
s = StringIO.new
|
193
|
+
Yajl::Encoder.new(:terminator => "\n").encode({:foo => :bar}) do |chunk|
|
194
|
+
s << chunk
|
195
|
+
end
|
196
|
+
s.rewind
|
197
|
+
s.read.should eql("{\"foo\":\"bar\"}\n")
|
198
|
+
|
199
|
+
s = StringIO.new
|
200
|
+
nilpassed = false
|
201
|
+
Yajl::Encoder.new(:terminator => nil).encode({:foo => :bar}) do |chunk|
|
202
|
+
nilpassed = true if chunk.nil?
|
203
|
+
s << chunk
|
204
|
+
end
|
205
|
+
nilpassed.should be_true
|
206
|
+
s.rewind
|
207
|
+
s.read.should eql("{\"foo\":\"bar\"}")
|
208
|
+
end
|
209
|
+
|
210
|
+
it "should not encode NaN" do
|
211
|
+
lambda {
|
212
|
+
Yajl::Encoder.encode(0.0/0.0)
|
213
|
+
}.should raise_error(Yajl::EncodeError)
|
214
|
+
end
|
215
|
+
|
216
|
+
it "should not encode Infinity or -Infinity" do
|
217
|
+
lambda {
|
218
|
+
Yajl::Encoder.encode(1.0/0.0)
|
219
|
+
}.should raise_error(Yajl::EncodeError)
|
220
|
+
lambda {
|
221
|
+
Yajl::Encoder.encode(-1.0/0.0)
|
222
|
+
}.should raise_error(Yajl::EncodeError)
|
223
|
+
end
|
224
|
+
|
225
|
+
it "should encode with unicode chars in the key" do
|
226
|
+
hash = {"浅草" => "<- those are unicode"}
|
227
|
+
Yajl::Encoder.encode(hash).should eql("{\"浅草\":\"<- those are unicode\"}")
|
228
|
+
end
|
229
|
+
|
230
|
+
if RUBY_VERSION =~ /^1.9/
|
231
|
+
it "should return a string encoded in utf-8 if Encoding.default_internal is nil" do
|
232
|
+
Encoding.default_internal = nil
|
233
|
+
hash = {"浅草" => "<- those are unicode"}
|
234
|
+
Yajl::Encoder.encode(hash).encoding.should eql(Encoding.find('utf-8'))
|
235
|
+
end
|
236
|
+
|
237
|
+
it "should return a string encoded in utf-8 even if Encoding.default_internal *is* set" do
|
238
|
+
Encoding.default_internal = Encoding.find('utf-8')
|
239
|
+
hash = {"浅草" => "<- those are unicode"}
|
240
|
+
Yajl::Encoder.encode(hash).encoding.should eql(Encoding.default_internal)
|
241
|
+
Encoding.default_internal = Encoding.find('us-ascii')
|
242
|
+
hash = {"浅草" => "<- those are unicode"}
|
243
|
+
Yajl::Encoder.encode(hash).encoding.should eql(Encoding.find('utf-8'))
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
it "should be able to escape / characters if html_safe is enabled" do
|
248
|
+
unsafe_encoder = Yajl::Encoder.new(:html_safe => false)
|
249
|
+
safe_encoder = Yajl::Encoder.new(:html_safe => true)
|
250
|
+
|
251
|
+
unsafe_encoder.encode("</script>").should_not eql("\"<\\/script>\"")
|
252
|
+
safe_encoder.encode("</script>").should eql("\"<\\/script>\"")
|
253
|
+
end
|
254
|
+
|
255
|
+
it "should default to *not* escaping / characters" do
|
256
|
+
unsafe_encoder = Yajl::Encoder.new
|
257
|
+
unsafe_encoder.encode("</script>").should_not eql("\"<\\/script>\"")
|
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
|
271
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')
|
2
|
+
|
3
|
+
describe "Yajl" do
|
4
|
+
context "dump" do
|
5
|
+
it "should exist as a class-method" do
|
6
|
+
Yajl.should respond_to(:dump)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should be able to encode to a string" do
|
10
|
+
Yajl.dump({:a => 1234}).should eql('{"a":1234}')
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should be able to encode to an IO" do
|
14
|
+
io = StringIO.new
|
15
|
+
Yajl.dump({:a => 1234}, io)
|
16
|
+
io.rewind
|
17
|
+
io.read.should eql('{"a":1234}')
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should be able to encode with a block supplied" do
|
21
|
+
Yajl.dump({:a => 1234}) do |chunk|
|
22
|
+
chunk.should eql('{"a":1234}')
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context "load" do
|
28
|
+
it "should exist as a class-method" do
|
29
|
+
Yajl.should respond_to(:load)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should be able to parse from a string" do
|
33
|
+
Yajl.load('{"a":1234}').should eql({"a" => 1234})
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should be able to parse from an IO" do
|
37
|
+
io = StringIO.new('{"a":1234}')
|
38
|
+
Yajl.load(io).should eql({"a" => 1234})
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should be able to parse from a string with a block supplied" do
|
42
|
+
Yajl.load('{"a":1234}') do |h|
|
43
|
+
h.should eql({"a" => 1234})
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should be able to parse from an IO with a block supplied" do
|
48
|
+
io = StringIO.new('{"a":1234}')
|
49
|
+
Yajl.load(io) do |h|
|
50
|
+
h.should eql({"a" => 1234})
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
Binary file
|
@@ -0,0 +1,11 @@
|
|
1
|
+
HTTP/1.1 200 OK
|
2
|
+
Content-Type: application/json
|
3
|
+
Transfer-Encoding: chunked
|
4
|
+
|
5
|
+
12f
|
6
|
+
{"item": {"name": "generated", "cached_tag_list": "", "updated_at": "2009-03-24T05:25:09Z", "updated_by_id": null, "price": 1.99, "delta": false, "cost": 0.597, "account_id": 16, "unit": null, "import_tag": null, "taxable": true, "id": 1, "created_by_id": null, "description": null, "company_id": 0, "sk
|
7
|
+
|
8
|
+
48
|
9
|
+
u": "06317-0306", "created_at": "2009-03-24T05:25:09Z", "active": true}}
|
10
|
+
|
11
|
+
0
|