Skip to content

Commit

Permalink
added StringIO support to json gem and okjson
Browse files Browse the repository at this point in the history
  • Loading branch information
joshk committed Apr 18, 2011
1 parent 5bd1afc commit 1706b11
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/multi_json/engines/json_gem.rb
Expand Up @@ -9,6 +9,7 @@ class JsonGem
def self.decode(string, options = {}) #:nodoc:
opts = {}
opts[:symbolize_names] = options[:symbolize_keys]
string = string.read if string.respond_to?(:read)
::JSON.parse(string, opts)
end

Expand Down
1 change: 1 addition & 0 deletions lib/multi_json/engines/json_pure.rb
Expand Up @@ -9,6 +9,7 @@ class JsonPure
def self.decode(string, options = {}) #:nodoc:
opts = {}
opts[:symbolize_names] = options[:symbolize_keys]
string = string.read if string.respond_to?(:read)
::JSON.parse(string, opts)
end

Expand Down
1 change: 1 addition & 0 deletions lib/multi_json/engines/ok_json.rb
Expand Up @@ -6,6 +6,7 @@ class OkJson
ParseError = ::OkJson::Error

def self.decode(string, options = {}) #:nodoc:
string = string.read if string.respond_to?(:read)
result = ::OkJson.decode(string)
options[:symbolize_keys] ? symbolize_keys(result) : result
end
Expand Down
8 changes: 7 additions & 1 deletion spec/multi_json_spec.rb
@@ -1,5 +1,6 @@
require 'spec_helper'

require 'stringio'

class MockDecoder
def self.decode(string, options = {})
{ 'abc' => 'def' }
Expand Down Expand Up @@ -75,6 +76,11 @@ def self.encode(string)
encoded_json = MultiJson.encode(:a => 1, :b => {:c => 2})
MultiJson.decode(encoded_json).should == { "a" => 1, "b" => { "c" => 2 } }
end

it "properly decodes valid JSON in StringIOs" do
json = StringIO.new('{"abc":"def"}')
MultiJson.decode(json).should == { 'abc' => 'def' }
end

it 'allows for symbolization of keys' do
MultiJson.decode('{"abc":{"def":"hgi"}}', :symbolize_keys => true).should == { :abc => { :def => 'hgi' } }
Expand Down

0 comments on commit 1706b11

Please sign in to comment.