multi_json 1.5.0 → 1.12.1

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.
@@ -1,127 +0,0 @@
1
- shared_examples_for "an adapter" do |adapter|
2
-
3
- before do
4
- begin
5
- MultiJson.use adapter
6
- rescue LoadError
7
- pending "Adapter #{adapter} couldn't be loaded (not installed?)"
8
- end
9
- end
10
-
11
- describe '.dump' do
12
- it 'writes decodable JSON' do
13
- [
14
- {'abc' => 'def'},
15
- [1, 2, 3, "4"],
16
- ].each do |example|
17
- expect(MultiJson.load(MultiJson.dump(example))).to eq example
18
- end
19
- end
20
-
21
- it 'dumps symbol and fixnum keys as strings' do
22
- [
23
- [
24
- {:foo => {:bar => 'baz'}},
25
- {'foo' => {'bar' => 'baz'}},
26
- ],
27
- [
28
- [{:foo => {:bar => 'baz'}}],
29
- [{'foo' => {'bar' => 'baz'}}],
30
- ],
31
- [
32
- {:foo => [{:bar => 'baz'}]},
33
- {'foo' => [{'bar' => 'baz'}]},
34
- ],
35
- [
36
- {1 => {2 => {3 => 'bar'}}},
37
- {'1' => {'2' => {'3' => 'bar'}}}
38
- ]
39
- ].each do |example, expected|
40
- dumped_json = MultiJson.dump(example)
41
- expect(MultiJson.load(dumped_json)).to eq expected
42
- end
43
- end
44
-
45
- it 'dumps rootless JSON' do
46
- expect(MultiJson.dump("random rootless string")).to eq "\"random rootless string\""
47
- expect(MultiJson.dump(123)).to eq "123"
48
- end
49
-
50
- it 'passes options to the adapter' do
51
- MultiJson.adapter.should_receive(:dump).with('foo', {:bar => :baz})
52
- MultiJson.dump('foo', :bar => :baz)
53
- end
54
-
55
- if adapter == 'json_gem' || adapter == 'json_pure'
56
- describe 'with :pretty option set to true' do
57
- it 'passes default pretty options' do
58
- object = 'foo'
59
- object.should_receive(:to_json).with(JSON::PRETTY_STATE_PROTOTYPE.to_h)
60
- MultiJson.dump(object,:pretty => true)
61
- end
62
- end
63
- end
64
-
65
- it 'dumps custom objects which implement as_json' do
66
- expect(MultiJson.dump(TimeWithZone.new)).to eq "\"2005-02-01T15:15:10Z\""
67
- end
68
-
69
- it 'allow to dump JSON values' do
70
- expect(MultiJson.dump(42)).to eq '42'
71
- end
72
-
73
- end
74
-
75
- describe '.load' do
76
- it 'properly loads valid JSON' do
77
- expect(MultiJson.load('{"abc":"def"}')).to eq({'abc' => 'def'})
78
- end
79
-
80
- it 'raises MultiJson::DecodeError on invalid JSON' do
81
- expect{MultiJson.load('{"abc"}')}.to raise_error(MultiJson::DecodeError)
82
- end
83
-
84
- it 'raises MultiJson::DecodeError with data on invalid JSON' do
85
- data = '{invalid}'
86
- begin
87
- MultiJson.load(data)
88
- rescue MultiJson::DecodeError => de
89
- expect(de.data).to eq data
90
- end
91
- end
92
-
93
- it 'stringifys symbol keys when encoding' do
94
- dumped_json = MultiJson.dump(:a => 1, :b => {:c => 2})
95
- expect(MultiJson.load(dumped_json)).to eq({"a" => 1, "b" => {"c" => 2}})
96
- end
97
-
98
- it 'properly loads valid JSON in StringIOs' do
99
- json = StringIO.new('{"abc":"def"}')
100
- expect(MultiJson.load(json)).to eq({'abc' => 'def'})
101
- end
102
-
103
- it 'allows for symbolization of keys' do
104
- [
105
- [
106
- '{"abc":{"def":"hgi"}}',
107
- {:abc => {:def => 'hgi'}},
108
- ],
109
- [
110
- '[{"abc":{"def":"hgi"}}]',
111
- [{:abc => {:def => 'hgi'}}],
112
- ],
113
- [
114
- '{"abc":[{"def":"hgi"}]}',
115
- {:abc => [{:def => 'hgi'}]},
116
- ],
117
- ].each do |example, expected|
118
- expect(MultiJson.load(example, :symbolize_keys => true)).to eq expected
119
- end
120
- end
121
-
122
- it 'allow to load JSON values' do
123
- expect(MultiJson.load('42')).to eq 42
124
- end
125
-
126
- end
127
- end
data/spec/helper.rb DELETED
@@ -1,39 +0,0 @@
1
- def jruby?
2
- defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
3
- end
4
-
5
- def macruby?
6
- defined?(RUBY_ENGINE) && RUBY_ENGINE == 'macruby'
7
- end
8
-
9
- unless ENV['CI'] || macruby?
10
- require 'simplecov'
11
- SimpleCov.start do
12
- add_filter 'spec'
13
- end
14
- end
15
-
16
- require 'multi_json'
17
- require 'rspec'
18
-
19
- RSpec.configure do |config|
20
- config.expect_with :rspec do |c|
21
- c.syntax = :expect
22
- end
23
- end
24
-
25
- class MockDecoder
26
- def self.load(string, options={})
27
- {'abc' => 'def'}
28
- end
29
-
30
- def self.dump(string)
31
- '{"abc":"def"}'
32
- end
33
- end
34
-
35
- class TimeWithZone
36
- def to_json(options={})
37
- "\"2005-02-01T15:15:10Z\""
38
- end
39
- end
@@ -1,100 +0,0 @@
1
- require 'helper'
2
- require 'adapter_shared_example'
3
- require 'stringio'
4
-
5
- describe 'MultiJson' do
6
- context 'adapters' do
7
- before do
8
- MultiJson.use nil
9
- end
10
- context 'when no other json implementations are available' do
11
- before do
12
- @old_map = MultiJson::REQUIREMENT_MAP
13
- @old_json = Object.const_get :JSON if Object.const_defined?(:JSON)
14
- @old_oj = Object.const_get :Oj if Object.const_defined?(:Oj)
15
- @old_yajl = Object.const_get :Yajl if Object.const_defined?(:Yajl)
16
- MultiJson::REQUIREMENT_MAP.each_with_index do |(library, adapter), index|
17
- MultiJson::REQUIREMENT_MAP[index] = ["foo/#{library}", adapter]
18
- end
19
- Object.send :remove_const, :JSON if @old_json
20
- Object.send :remove_const, :Oj if @old_oj
21
- Object.send :remove_const, :Yajl if @old_yajl
22
- end
23
-
24
- after do
25
- @old_map.each_with_index do |(library, adapter), index|
26
- MultiJson::REQUIREMENT_MAP[index] = [library, adapter]
27
- end
28
- Object.const_set :JSON, @old_json if @old_json
29
- Object.const_set :Oj, @old_oj if @old_oj
30
- Object.const_set :Yajl, @old_yajl if @old_yajl
31
- end
32
-
33
- it 'defaults to ok_json if no other json implementions are available' do
34
- expect(MultiJson.default_adapter).to eq :ok_json
35
- end
36
-
37
- it 'prints a warning' do
38
- Kernel.should_receive(:warn).with(/warning/i)
39
- MultiJson.default_adapter
40
- end
41
- end
42
-
43
- it 'defaults to the best available gem' do
44
- # Clear cache variable already set by previous tests
45
- MultiJson.send(:remove_instance_variable, :@adapter)
46
- unless jruby?
47
- require 'oj'
48
- expect(MultiJson.adapter.name).to eq 'MultiJson::Adapters::Oj'
49
- else
50
- require 'json'
51
- expect(MultiJson.adapter.name).to eq 'MultiJson::Adapters::JsonGem'
52
- end
53
- end
54
-
55
- it 'is settable via a symbol' do
56
- MultiJson.use :json_gem
57
- expect(MultiJson.adapter.name).to eq 'MultiJson::Adapters::JsonGem'
58
- end
59
-
60
- it 'is settable via a class' do
61
- MultiJson.use MockDecoder
62
- expect(MultiJson.adapter.name).to eq 'MockDecoder'
63
- end
64
-
65
- context "using one-shot parser" do
66
- before(:each) do
67
- require 'multi_json/adapters/json_pure'
68
- MultiJson::Adapters::JsonPure.should_receive(:dump).exactly(1).times.and_return('dump_something')
69
- MultiJson::Adapters::JsonPure.should_receive(:load).exactly(1).times.and_return('load_something')
70
- end
71
-
72
- it "should use the defined parser just for the call" do
73
- MultiJson.use :json_gem
74
- expect(MultiJson.dump('', :adapter => :json_pure)).to eq 'dump_something'
75
- expect(MultiJson.load('', :adapter => :json_pure)).to eq 'load_something'
76
- expect(MultiJson.adapter.name).to eq "MultiJson::Adapters::JsonGem"
77
- end
78
- end
79
- end
80
-
81
- it 'can set adapter for a block' do
82
- MultiJson.use :ok_json
83
- MultiJson.with_adapter(:json_pure) do
84
- expect(MultiJson.adapter.name).to eq 'MultiJson::Adapters::JsonPure'
85
- end
86
- MultiJson.with_engine(:yajl) do
87
- expect(MultiJson.adapter.name).to eq 'MultiJson::Adapters::Yajl'
88
- end
89
- expect(MultiJson.adapter.name).to eq 'MultiJson::Adapters::OkJson'
90
- end
91
-
92
- %w(json_gem json_pure nsjsonserialization oj ok_json yajl).each do |adapter|
93
- next if !macruby? && adapter == 'nsjsonserialization'
94
- next if jruby? && (adapter == 'oj' || adapter == 'yajl')
95
-
96
- context adapter do
97
- it_behaves_like "an adapter", adapter
98
- end
99
- end
100
- end