File: encoding_spec.rb

package info (click to toggle)
ruby-yajl 1.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 4,036 kB
  • sloc: ansic: 3,068; ruby: 2,619; makefile: 4; sh: 3
file content (341 lines) | stat: -rw-r--r-- 11,185 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# encoding: UTF-8
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')
require 'tmpdir'
require 'zlib'

class Dummy2
  def to_json
    "{\"hawtness\":true}"
  end
end

class TheMindKiller
  def to_json
    nil
  end
end

class TheMindKillerDuce
  def to_s
    nil
  end
end

describe "Yajl JSON encoder" do
  FILES = Dir[File.dirname(__FILE__)+'/../../benchmark/subjects/*.json']

  FILES.each do |file|
     it "should encode #{File.basename(file)} to an StringIO" do
       # we don't care about testing the stream subject as it has multiple JSON strings in it
       if File.basename(file) != 'twitter_stream.json'
         input = File.new(File.expand_path(file), 'r')
         io = StringIO.new
         encoder = Yajl::Encoder.new
         hash = Yajl::Parser.parse(input)
         encoder.encode(hash, io)
         io.rewind
         hash2 = Yajl::Parser.parse(io)
         io.close
         input.close
         expect(hash).to eq(hash2)
       end
     end
   end

   FILES.each do |file|
     it "should encode #{File.basename(file)} to a Zlib::GzipWriter" do
      # we don't care about testing the stream subject as it has multiple JSON strings in it
      if File.basename(file) != 'twitter_stream.json'
         hash = File.open(File.expand_path(file), 'r') do |input|
            Yajl::Parser.parse(input)
         end
         hash2 = Dir.mktmpdir do |tmp_dir|
            output_filename = File.join(tmp_dir, 'output.json')
            Zlib::GzipWriter.open(output_filename) do |writer|
               Yajl::Encoder.encode(hash, writer)
            end
            Zlib::GzipReader.open(output_filename) do |reader|
               Yajl::Parser.parse(reader.read)
            end
         end
         expect(hash).to eq(hash2)
      end
     end
   end

   FILES.each do |file|
     it "should encode #{File.basename(file)} and return a String" do
       # we don't care about testing the stream subject as it has multiple JSON strings in it
       if File.basename(file) != 'twitter_stream.json'
         input = File.new(File.expand_path(file), 'r')
         encoder = Yajl::Encoder.new
         hash = Yajl::Parser.parse(input)
         output = encoder.encode(hash)
         hash2 = Yajl::Parser.parse(output)
         input.close
         expect(hash).to eq(hash2)
       end
     end
   end

   FILES.each do |file|
     it "should encode #{File.basename(file)} call the passed block, passing it a String" do
       # we don't care about testing the stream subject as it has multiple JSON strings in it
       if File.basename(file) != 'twitter_stream.json'
         input = File.new(File.expand_path(file), 'r')
         encoder = Yajl::Encoder.new
         hash = Yajl::Parser.parse(input)
         output = ''
         encoder.encode(hash) do |json_str|
           output << json_str
         end
         hash2 = Yajl::Parser.parse(output)
         input.close
         expect(hash).to eq(hash2)
       end
     end
   end

  it "should encode with :pretty turned on and a single space indent, to an IO" do
    output = "{\n \"foo\": 1234\n}"
    obj = {:foo => 1234}
    io = StringIO.new
    encoder = Yajl::Encoder.new(:pretty => true, :indent => ' ')
    encoder.encode(obj, io)
    io.rewind
    expect(io.read).to eq(output)
  end

  it "should encode with :pretty turned on and a single space indent, and return a String" do
    output = "{\n \"foo\": 1234\n}"
    obj = {:foo => 1234}
    encoder = Yajl::Encoder.new(:pretty => true, :indent => ' ')
    output = encoder.encode(obj)
    expect(output).to eq(output)
  end

  it "should encode with :pretty turned on and a tab character indent, to an IO" do
    output = "{\n\t\"foo\": 1234\n}"
    obj = {:foo => 1234}
    io = StringIO.new
    encoder = Yajl::Encoder.new(:pretty => true, :indent => "\t")
    encoder.encode(obj, io)
    io.rewind
    expect(io.read).to eq(output)
  end

  it "should encode with :pretty turned on and a tab character indent, and return a String" do
    output = "{\n\t\"foo\": 1234\n}"
    obj = {:foo => 1234}
    encoder = Yajl::Encoder.new(:pretty => true, :indent => "\t")
    output = encoder.encode(obj)
    expect(output).to eq(output)
  end

  it "should encode with it's class method with :pretty and a tab character indent options set, to an IO" do
    output = "{\n\t\"foo\": 1234\n}"
    obj = {:foo => 1234}
    io = StringIO.new
    Yajl::Encoder.encode(obj, io, :pretty => true, :indent => "\t")
    io.rewind
    expect(io.read).to eq(output)
  end

  it "should encode with it's class method with :pretty and a tab character indent options set, and return a String" do
    output = "{\n\t\"foo\": 1234\n}"
    obj = {:foo => 1234}
    output = Yajl::Encoder.encode(obj, :pretty => true, :indent => "\t")
    expect(output).to eq(output)
  end

  it "should encode with it's class method with :pretty and a tab character indent options set, to a block" do
    output = "{\n\t\"foo\": 1234\n}"
    obj = {:foo => 1234}
    output = ''
    Yajl::Encoder.encode(obj, :pretty => true, :indent => "\t") do |json_str|
      output = json_str
    end
    expect(output).to eq(output)
  end

  it "should encode multiple objects into a single stream, to an IO" do
    io = StringIO.new
    obj = {:foo => 1234}
    encoder = Yajl::Encoder.new
    5.times do
      encoder.encode(obj, io)
    end
    io.rewind
    output = "{\"foo\":1234}{\"foo\":1234}{\"foo\":1234}{\"foo\":1234}{\"foo\":1234}"
    expect(io.read).to eq(output)
  end

  it "should encode multiple objects into a single stream, and return a String" do
    obj = {:foo => 1234}
    encoder = Yajl::Encoder.new
    json_output = ''
    5.times do
      json_output << encoder.encode(obj)
    end
    output = "{\"foo\":1234}{\"foo\":1234}{\"foo\":1234}{\"foo\":1234}{\"foo\":1234}"
    expect(json_output).to eq(output)
  end

  it "should encode all map keys as strings" do
    expect(Yajl::Encoder.encode({1=>1})).to eql("{\"1\":1}")
  end

  it "should check for and call #to_json if it exists on custom objects" do
    d = Dummy2.new
    expect(Yajl::Encoder.encode({:foo => d})).to eql('{"foo":{"hawtness":true}}')
  end

  it "should encode a hash where the key and value can be symbols" do
    expect(Yajl::Encoder.encode({:foo => :bar})).to eql('{"foo":"bar"}')
  end

  it "should encode using a newline or nil terminator" do
    expect(Yajl::Encoder.new(:terminator => "\n").encode({:foo => :bar})).to eql("{\"foo\":\"bar\"}\n")
    expect(Yajl::Encoder.new(:terminator => nil).encode({:foo => :bar})).to eql("{\"foo\":\"bar\"}")
  end

  it "should encode using a newline or nil terminator, to an IO" do
    s = StringIO.new
    Yajl::Encoder.new(:terminator => "\n").encode({:foo => :bar}, s)
    s.rewind
    expect(s.read).to eql("{\"foo\":\"bar\"}\n")

    s = StringIO.new
    Yajl::Encoder.new(:terminator => nil).encode({:foo => :bar}, s)
    s.rewind
    expect(s.read).to eql("{\"foo\":\"bar\"}")
  end

  it "should encode using a newline or nil terminator, using a block" do
    s = StringIO.new
    Yajl::Encoder.new(:terminator => "\n").encode({:foo => :bar}) do |chunk|
      s << chunk
    end
    s.rewind
    expect(s.read).to eql("{\"foo\":\"bar\"}\n")

    s = StringIO.new
    nilpassed = false
    Yajl::Encoder.new(:terminator => nil).encode({:foo => :bar}) do |chunk|
      nilpassed = true if chunk.nil?
      s << chunk
    end
    expect(nilpassed).to be_truthy
    s.rewind
    expect(s.read).to eql("{\"foo\":\"bar\"}")
  end

  it "should encode all integers correctly" do
    0.upto(129).each do |b|
      b = 1 << b
      [b, b-1, b-2, b+1, b+2].each do |i|
        expect(Yajl::Encoder.encode(i)).to eq(i.to_s)
        expect(Yajl::Encoder.encode(-i)).to eq((-i).to_s)
      end
    end
  end

  it "should not encode NaN" do
    expect {
      Yajl::Encoder.encode(0.0/0.0)
    }.to raise_error(Yajl::EncodeError)
  end

  it "should not encode Infinity or -Infinity" do
    expect {
      Yajl::Encoder.encode(1.0/0.0)
    }.to raise_error(Yajl::EncodeError)
    expect {
      Yajl::Encoder.encode(-1.0/0.0)
    }.to raise_error(Yajl::EncodeError)
  end

  it "should encode with unicode chars in the key" do
    hash = {"浅草" => "<- those are unicode"}
    expect(Yajl::Encoder.encode(hash)).to eql("{\"浅草\":\"<- those are unicode\"}")
  end

  if RUBY_VERSION =~ /^1.9/
    it "should return a string encoded in utf-8 if Encoding.default_internal is nil" do
      Encoding.default_internal = nil
      hash = {"浅草" => "<- those are unicode"}
      expect(Yajl::Encoder.encode(hash).encoding).to eql(Encoding.find('utf-8'))
    end

    it "should return a string encoded in utf-8 even if Encoding.default_internal *is* set" do
      Encoding.default_internal = Encoding.find('utf-8')
      hash = {"浅草" => "<- those are unicode"}
      expect(Yajl::Encoder.encode(hash).encoding).to eql(Encoding.default_internal)
      Encoding.default_internal = Encoding.find('us-ascii')
      hash = {"浅草" => "<- those are unicode"}
      expect(Yajl::Encoder.encode(hash).encoding).to eql(Encoding.find('utf-8'))
    end
  end

  it "should be able to escape / characters if html_safe is enabled" do
    unsafe_encoder = Yajl::Encoder.new(:html_safe => false)
    safe_encoder   = Yajl::Encoder.new(:html_safe => true)

    expect(unsafe_encoder.encode("</script>")).not_to eql("\"<\\/script>\"")
    expect(safe_encoder.encode("</script>")).to eql("\"<\\/script>\"")
  end

  it "should not encode characters with entities by default" do
    expect(Yajl.dump("\u2028\u2029><&")).to eql("\"\u2028\u2029><&\"")
  end

  it "should encode characters with entities when enabled" do
    expect(Yajl.dump("\u2028\u2029><&", entities: true)).to eql("\"\\u2028\\u2029\\u003E\\u003C\\u0026\"")
  end

  it "should default to *not* escaping / characters" do
    unsafe_encoder = Yajl::Encoder.new
    expect(unsafe_encoder.encode("</script>")).not_to eql("\"<\\/script>\"")
  end

  it "should encode slashes when enabled" do
    unsafe_encoder = Yajl::Encoder.new(:entities => false)
    safe_encoder   = Yajl::Encoder.new(:entities => true)

    expect(unsafe_encoder.encode("</script>")).not_to eql("\"<\\/script>\"")
    expect(safe_encoder.encode("</script>")).to eql("\"\\u003C\\/script\\u003E\"")
  end

  it "return value of #to_json must be a string" do
    expect {
      Yajl::Encoder.encode(TheMindKiller.new)
    }.to raise_error(TypeError)
  end

  it "return value of #to_s must be a string" do
    expect {
      if TheMindKillerDuce.send(:method_defined?, :to_json)
        TheMindKillerDuce.send(:undef_method, :to_json)
      end
      Yajl::Encoder.encode(TheMindKillerDuce.new)
    }.to raise_error(TypeError)
  end

  it "should raise an exception for deeply nested arrays" do
    root = []
    a = root
    (Yajl::MAX_DEPTH + 1).times { |_| a << []; a = a[0] }
    expect {
      Yajl::Encoder.encode(root)
    }.to raise_error(Yajl::EncodeError)
  end

  it "should raise an exception for deeply nested hashes" do
    root = {}
    a = root
    (Yajl::MAX_DEPTH + 1).times { |_| a["a"] = {}; a = a["a"] }
    expect {
      Yajl::Encoder.encode(root)
    }.to raise_error(Yajl::EncodeError)
  end
end