multi_json 1.0.0.rc2 → 1.0.0.rc3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/multi_json.rb +5 -7
- data/lib/multi_json/engines/json_gem.rb +3 -0
- data/lib/multi_json/engines/json_pure.rb +3 -0
- data/lib/multi_json/engines/{okjson.rb → ok_json.rb} +7 -4
- data/lib/multi_json/engines/yajl.rb +2 -0
- data/lib/multi_json/vendor/{okjson.rb → ok_json.rb} +0 -0
- data/lib/multi_json/version.rb +1 -1
- data/spec/multi_json_spec.rb +17 -11
- metadata +6 -6
data/lib/multi_json.rb
CHANGED
@@ -15,7 +15,7 @@ module MultiJson
|
|
15
15
|
["yajl", :yajl],
|
16
16
|
["json", :json_gem],
|
17
17
|
["json/pure", :json_pure],
|
18
|
-
["
|
18
|
+
["ok_json", :ok_json]
|
19
19
|
]
|
20
20
|
|
21
21
|
# The default engine based on what you currently
|
@@ -41,7 +41,7 @@ module MultiJson
|
|
41
41
|
#
|
42
42
|
# * <tt>:json_gem</tt>
|
43
43
|
# * <tt>:json_pure</tt>
|
44
|
-
# * <tt>:
|
44
|
+
# * <tt>:ok_json</tt>
|
45
45
|
# * <tt>:yajl</tt>
|
46
46
|
def engine=(new_engine)
|
47
47
|
case new_engine
|
@@ -61,11 +61,9 @@ module MultiJson
|
|
61
61
|
#
|
62
62
|
# <tt>:symbolize_keys</tt> :: If true, will use symbols instead of strings for the keys.
|
63
63
|
def decode(string, options = {})
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
raise DecodeError, exception.inspect
|
68
|
-
end
|
64
|
+
engine.decode(string, options)
|
65
|
+
rescue engine::ParseError => exception
|
66
|
+
raise DecodeError, exception.message, exception.backtrace
|
69
67
|
end
|
70
68
|
|
71
69
|
# Encodes a Ruby object as JSON.
|
@@ -4,9 +4,12 @@ module MultiJson
|
|
4
4
|
module Engines
|
5
5
|
# Use the JSON gem to encode/decode.
|
6
6
|
class JsonGem
|
7
|
+
ParseError = ::JSON::ParserError
|
8
|
+
|
7
9
|
def self.decode(string, options = {}) #:nodoc:
|
8
10
|
opts = {}
|
9
11
|
opts[:symbolize_names] = options[:symbolize_keys]
|
12
|
+
string = string.read if string.respond_to?(:read)
|
10
13
|
::JSON.parse(string, opts)
|
11
14
|
end
|
12
15
|
|
@@ -4,9 +4,12 @@ module MultiJson
|
|
4
4
|
module Engines
|
5
5
|
# Use JSON pure to encode/decode.
|
6
6
|
class JsonPure
|
7
|
+
ParseError = ::JSON::ParserError
|
8
|
+
|
7
9
|
def self.decode(string, options = {}) #:nodoc:
|
8
10
|
opts = {}
|
9
11
|
opts[:symbolize_names] = options[:symbolize_keys]
|
12
|
+
string = string.read if string.respond_to?(:read)
|
10
13
|
::JSON.parse(string, opts)
|
11
14
|
end
|
12
15
|
|
@@ -1,15 +1,18 @@
|
|
1
|
-
require "multi_json/vendor/
|
1
|
+
require "multi_json/vendor/ok_json" unless defined?(::OkJson)
|
2
2
|
|
3
3
|
module MultiJson
|
4
4
|
module Engines
|
5
|
-
class
|
5
|
+
class OkJson
|
6
|
+
ParseError = ::OkJson::Error
|
7
|
+
|
6
8
|
def self.decode(string, options = {}) #:nodoc:
|
7
|
-
|
9
|
+
string = string.read if string.respond_to?(:read)
|
10
|
+
result = ::OkJson.decode(string)
|
8
11
|
options[:symbolize_keys] ? symbolize_keys(result) : result
|
9
12
|
end
|
10
13
|
|
11
14
|
def self.encode(object) #:nodoc:
|
12
|
-
OkJson.encode(stringify_keys(object))
|
15
|
+
::OkJson.encode(stringify_keys(object))
|
13
16
|
end
|
14
17
|
|
15
18
|
def self.symbolize_keys(object) #:nodoc:
|
File without changes
|
data/lib/multi_json/version.rb
CHANGED
data/spec/multi_json_spec.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
-
|
2
|
+
require 'stringio'
|
3
|
+
|
3
4
|
class MockDecoder
|
4
5
|
def self.decode(string, options = {})
|
5
6
|
{ 'abc' => 'def' }
|
@@ -12,7 +13,7 @@ end
|
|
12
13
|
|
13
14
|
describe "MultiJson" do
|
14
15
|
context 'engines' do
|
15
|
-
it '
|
16
|
+
it 'defaults to the best available gem' do
|
16
17
|
# the yajl-ruby gem does not work on jruby, so the best engine is the JsonGem engine
|
17
18
|
if jruby?
|
18
19
|
require 'json'
|
@@ -23,18 +24,18 @@ describe "MultiJson" do
|
|
23
24
|
end
|
24
25
|
end
|
25
26
|
|
26
|
-
it '
|
27
|
+
it 'is settable via a symbol' do
|
27
28
|
MultiJson.engine = :json_gem
|
28
29
|
MultiJson.engine.name.should == 'MultiJson::Engines::JsonGem'
|
29
30
|
end
|
30
31
|
|
31
|
-
it '
|
32
|
+
it 'is settable via a class' do
|
32
33
|
MultiJson.engine = MockDecoder
|
33
34
|
MultiJson.engine.name.should == 'MockDecoder'
|
34
35
|
end
|
35
36
|
end
|
36
37
|
|
37
|
-
%w(json_gem json_pure
|
38
|
+
%w(json_gem json_pure ok_json yajl).each do |engine|
|
38
39
|
context engine do
|
39
40
|
before do
|
40
41
|
begin
|
@@ -45,7 +46,7 @@ describe "MultiJson" do
|
|
45
46
|
end
|
46
47
|
|
47
48
|
describe '.encode' do
|
48
|
-
it '
|
49
|
+
it 'writes decodable JSON' do
|
49
50
|
[
|
50
51
|
{ 'abc' => 'def' },
|
51
52
|
[1, 2, 3, "4"]
|
@@ -54,29 +55,34 @@ describe "MultiJson" do
|
|
54
55
|
end
|
55
56
|
end
|
56
57
|
|
57
|
-
it '
|
58
|
+
it 'encodes symbol keys as strings' do
|
58
59
|
encoded_json = MultiJson.encode({ :foo => { :bar => 'baz' } })
|
59
60
|
MultiJson.decode(encoded_json).should == { 'foo' => { 'bar' => 'baz' } }
|
60
61
|
end
|
61
62
|
end
|
62
63
|
|
63
64
|
describe '.decode' do
|
64
|
-
it '
|
65
|
+
it 'properly decodes valid JSON' do
|
65
66
|
MultiJson.decode('{"abc":"def"}').should == { 'abc' => 'def' }
|
66
67
|
end
|
67
68
|
|
68
|
-
it '
|
69
|
+
it 'raises MultiJson::DecodeError on invalid JSON' do
|
69
70
|
lambda do
|
70
71
|
MultiJson.decode('{"abc"}')
|
71
72
|
end.should raise_error(MultiJson::DecodeError)
|
72
73
|
end
|
73
74
|
|
74
|
-
it '
|
75
|
+
it 'stringifys symbol keys when encoding' do
|
75
76
|
encoded_json = MultiJson.encode(:a => 1, :b => {:c => 2})
|
76
77
|
MultiJson.decode(encoded_json).should == { "a" => 1, "b" => { "c" => 2 } }
|
77
78
|
end
|
79
|
+
|
80
|
+
it "properly decodes valid JSON in StringIOs" do
|
81
|
+
json = StringIO.new('{"abc":"def"}')
|
82
|
+
MultiJson.decode(json).should == { 'abc' => 'def' }
|
83
|
+
end
|
78
84
|
|
79
|
-
it '
|
85
|
+
it 'allows for symbolization of keys' do
|
80
86
|
MultiJson.decode('{"abc":{"def":"hgi"}}', :symbolize_keys => true).should == { :abc => { :def => 'hgi' } }
|
81
87
|
end
|
82
88
|
end
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: multi_json
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 15424051
|
5
5
|
prerelease: 6
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
9
|
- 0
|
10
10
|
- rc
|
11
|
-
-
|
12
|
-
version: 1.0.0.
|
11
|
+
- 3
|
12
|
+
version: 1.0.0.rc3
|
13
13
|
platform: ruby
|
14
14
|
authors:
|
15
15
|
- Michael Bleigh
|
@@ -17,7 +17,7 @@ autorequire:
|
|
17
17
|
bindir: bin
|
18
18
|
cert_chain: []
|
19
19
|
|
20
|
-
date: 2011-04-
|
20
|
+
date: 2011-04-19 00:00:00 Z
|
21
21
|
dependencies: []
|
22
22
|
|
23
23
|
description: A gem to provide swappable JSON backends utilizing Yajl::Ruby, the JSON gem, ActiveSupport, or JSON pure.
|
@@ -42,9 +42,9 @@ files:
|
|
42
42
|
- lib/multi_json.rb
|
43
43
|
- lib/multi_json/engines/json_gem.rb
|
44
44
|
- lib/multi_json/engines/json_pure.rb
|
45
|
-
- lib/multi_json/engines/
|
45
|
+
- lib/multi_json/engines/ok_json.rb
|
46
46
|
- lib/multi_json/engines/yajl.rb
|
47
|
-
- lib/multi_json/vendor/
|
47
|
+
- lib/multi_json/vendor/ok_json.rb
|
48
48
|
- lib/multi_json/version.rb
|
49
49
|
- multi_json.gemspec
|
50
50
|
- spec/multi_json_spec.rb
|