Skip to content

Commit e90fd6c

Browse files
committed
Implement load/dump; deprecate decode/encode
Closes #36.
1 parent 43c7395 commit e90fd6c

10 files changed

Lines changed: 57 additions & 45 deletions

File tree

lib/multi_json.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,31 @@ def engine=(new_engine)
7272
end
7373
end
7474

75+
# TODO: Remove for 2.0 release (but not any sooner)
76+
def decode(string, options={})
77+
warn "#{Kernel.caller.first}: [DEPRECATION] MultiJson.decode is deprecated and will be removed in the next major version. Use MultiJson.load instead."
78+
load(string, options)
79+
end
80+
81+
# TODO: Remove for 2.0 release (but not any sooner)
82+
def encode(object, options={})
83+
warn "#{Kernel.caller.first}: [DEPRECATION] MultiJson.encode is deprecated and will be removed in the next major version. Use MultiJson.dump instead."
84+
dump(object, options)
85+
end
86+
7587
# Decode a JSON string into Ruby.
7688
#
7789
# <b>Options</b>
7890
#
7991
# <tt>:symbolize_keys</tt> :: If true, will use symbols instead of strings for the keys.
80-
def decode(string, options = {})
81-
engine.decode(string, options)
92+
def load(string, options={})
93+
engine.load(string, options)
8294
rescue engine::ParseError => exception
8395
raise DecodeError.new(exception.message, exception.backtrace, string)
8496
end
8597

8698
# Encodes a Ruby object as JSON.
87-
def encode(object, options = {})
88-
engine.encode(object, options)
99+
def dump(object, options={})
100+
engine.dump(object, options)
89101
end
90102
end

lib/multi_json/engines/json_common.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ module MultiJson
22
module Engines
33
module JsonCommon
44

5-
def decode(string, options={})
5+
def load(string, options={})
66
string = string.read if string.respond_to?(:read)
77
::JSON.parse(string, :symbolize_names => options[:symbolize_keys])
88
end
99

10-
def encode(object, options={})
10+
def dump(object, options={})
1111
object.to_json(process_options(options))
1212
end
1313

lib/multi_json/engines/json_gem.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
module MultiJson
55
module Engines
6-
# Use the JSON gem to encode/decode.
6+
# Use the JSON gem to dump/load.
77
class JsonGem
88
ParseError = ::JSON::ParserError
99
extend JsonCommon

lib/multi_json/engines/json_pure.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
module MultiJson
55
module Engines
6-
# Use JSON pure to encode/decode.
6+
# Use JSON pure to dump/load.
77
class JsonPure
88
ParseError = ::JSON::ParserError
99
extend JsonCommon

lib/multi_json/engines/nsjsonserialization.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ module Engines
66
class Nsjsonserialization < MultiJson::Engines::OkJson
77
ParseError = ::MultiJson::OkJson::Error
88

9-
def self.decode(string, options = {})
9+
def self.load(string, options={})
1010
string = string.read if string.respond_to?(:read)
1111
data = string.dataUsingEncoding(NSUTF8StringEncoding)
1212
object = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves, error: nil)
1313
if object
1414
object = symbolize_keys(object) if options[:symbolize_keys]
1515
object
1616
else
17-
super(string, options = {})
17+
super(string, options={})
1818
end
1919
end
2020

21-
def self.encode(object, options = {})
21+
def self.dump(object, options={})
2222
pretty = options[:pretty] ? NSJSONWritingPrettyPrinted : 0
2323
object = object.as_json if object.respond_to?(:as_json)
2424
if NSJSONSerialization.isValidJSONObject(object)

lib/multi_json/engines/oj.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
module MultiJson
44
module Engines
5-
# Use the Oj library to encode/decode.
5+
# Use the Oj library to dump/load.
66
class Oj
77
ParseError = SyntaxError
88

99
::Oj.default_options = {:mode => :compat}
1010

11-
def self.decode(string, options = {}) #:nodoc:
11+
def self.load(string, options={}) #:nodoc:
1212
::Oj.load(string, :symbol_keys => options[:symbolize_keys])
1313
end
1414

15-
def self.encode(object, options = {}) #:nodoc:
15+
def self.dump(object, options={}) #:nodoc:
1616
::Oj.dump(object, options)
1717
end
1818
end

lib/multi_json/engines/ok_json.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ module Engines
55
class OkJson
66
ParseError = ::MultiJson::OkJson::Error
77

8-
def self.decode(string, options = {}) #:nodoc:
8+
def self.load(string, options={}) #:nodoc:
99
string = string.read if string.respond_to?(:read)
1010
result = ::MultiJson::OkJson.decode(string)
1111
options[:symbolize_keys] ? symbolize_keys(result) : result
1212
end
1313

14-
def self.encode(object, options = {}) #:nodoc:
14+
def self.dump(object, options={}) #:nodoc:
1515
::MultiJson::OkJson.valenc(stringify_keys(object))
1616
end
1717

lib/multi_json/engines/yajl.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
module MultiJson
44
module Engines
5-
# Use the Yajl-Ruby library to encode/decode.
5+
# Use the Yajl-Ruby library to dump/load.
66
class Yajl
77
ParseError = ::Yajl::ParseError
88

9-
def self.decode(string, options = {}) #:nodoc:
9+
def self.load(string, options={}) #:nodoc:
1010
::Yajl::Parser.new(:symbolize_keys => options[:symbolize_keys]).parse(string)
1111
end
1212

13-
def self.encode(object, options = {}) #:nodoc:
13+
def self.dump(object, options={}) #:nodoc:
1414
::Yajl::Encoder.encode(object, options)
1515
end
1616
end

spec/engine_shared_example.rb

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@
88
end
99
end
1010

11-
describe '.encode' do
11+
describe '.dump' do
1212
it 'writes decodable JSON' do
1313
[
1414
{'abc' => 'def'},
1515
[1, 2, 3, "4"],
1616
].each do |example|
17-
MultiJson.decode(MultiJson.encode(example)).should == example
17+
MultiJson.load(MultiJson.dump(example)).should == example
1818
end
1919
end
2020

21-
it 'encodes symbol keys as strings' do
21+
it 'dumps symbol keys as strings' do
2222
[
2323
[
2424
{:foo => {:bar => 'baz'}},
@@ -33,64 +33,64 @@
3333
{'foo' => [{'bar' => 'baz'}]},
3434
]
3535
].each do |example, expected|
36-
encoded_json = MultiJson.encode(example)
37-
MultiJson.decode(encoded_json).should == expected
36+
dumped_json = MultiJson.dump(example)
37+
MultiJson.load(dumped_json).should == expected
3838
end
3939
end
4040

41-
it 'encodes rootless JSON' do
42-
MultiJson.encode("random rootless string").should == "\"random rootless string\""
43-
MultiJson.encode(123).should == "123"
41+
it 'dumps rootless JSON' do
42+
MultiJson.dump("random rootless string").should == "\"random rootless string\""
43+
MultiJson.dump(123).should == "123"
4444
end
4545

4646
it 'passes options to the engine' do
47-
MultiJson.engine.should_receive(:encode).with('foo', {:bar => :baz})
48-
MultiJson.encode('foo', :bar => :baz)
47+
MultiJson.engine.should_receive(:dump).with('foo', {:bar => :baz})
48+
MultiJson.dump('foo', :bar => :baz)
4949
end
5050

5151
if engine == 'json_gem' || engine == 'json_pure'
5252
describe 'with :pretty option set to true' do
5353
it 'passes default pretty options' do
5454
object = 'foo'
5555
object.should_receive(:to_json).with(JSON::PRETTY_STATE_PROTOTYPE.to_h)
56-
MultiJson.encode(object,:pretty => true)
56+
MultiJson.dump(object,:pretty => true)
5757
end
5858
end
5959
end
6060

61-
it "encodes custom objects which implement as_json" do
62-
MultiJson.encode(TimeWithZone.new).should == "\"2005-02-01T15:15:10Z\""
61+
it 'dumps custom objects which implement as_json' do
62+
MultiJson.dump(TimeWithZone.new).should == "\"2005-02-01T15:15:10Z\""
6363
end
6464
end
6565

66-
describe '.decode' do
67-
it 'properly decodes valid JSON' do
68-
MultiJson.decode('{"abc":"def"}').should == {'abc' => 'def'}
66+
describe '.load' do
67+
it 'properly loads valid JSON' do
68+
MultiJson.load('{"abc":"def"}').should == {'abc' => 'def'}
6969
end
7070

7171
it 'raises MultiJson::DecodeError on invalid JSON' do
7272
lambda do
73-
MultiJson.decode('{"abc"}')
73+
MultiJson.load('{"abc"}')
7474
end.should raise_error(MultiJson::DecodeError)
7575
end
7676

7777
it 'raises MultiJson::DecodeError with data on invalid JSON' do
7878
data = '{invalid}'
7979
begin
80-
MultiJson.decode(data)
80+
MultiJson.load(data)
8181
rescue MultiJson::DecodeError => de
8282
de.data.should == data
8383
end
8484
end
8585

8686
it 'stringifys symbol keys when encoding' do
87-
encoded_json = MultiJson.encode(:a => 1, :b => {:c => 2})
88-
MultiJson.decode(encoded_json).should == {"a" => 1, "b" => {"c" => 2}}
87+
dumped_json = MultiJson.dump(:a => 1, :b => {:c => 2})
88+
MultiJson.load(dumped_json).should == {"a" => 1, "b" => {"c" => 2}}
8989
end
9090

91-
it "properly decodes valid JSON in StringIOs" do
91+
it 'properly loads valid JSON in StringIOs' do
9292
json = StringIO.new('{"abc":"def"}')
93-
MultiJson.decode(json).should == {'abc' => 'def'}
93+
MultiJson.load(json).should == {'abc' => 'def'}
9494
end
9595

9696
it 'allows for symbolization of keys' do
@@ -108,7 +108,7 @@
108108
{:abc => [{:def => 'hgi'}]},
109109
],
110110
].each do |example, expected|
111-
MultiJson.decode(example, :symbolize_keys => true).should == expected
111+
MultiJson.load(example, :symbolize_keys => true).should == expected
112112
end
113113
end
114114
end

spec/helper.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ def macruby?
1414
require 'rspec'
1515

1616
class MockDecoder
17-
def self.decode(string, options = {})
17+
def self.load(string, options={})
1818
{'abc' => 'def'}
1919
end
2020

21-
def self.encode(string)
21+
def self.dump(string)
2222
'{"abc":"def"}'
2323
end
2424
end
2525

2626
class TimeWithZone
27-
def to_json(options = {})
27+
def to_json(options={})
2828
"\"2005-02-01T15:15:10Z\""
2929
end
3030
end

0 commit comments

Comments
 (0)