diff --git a/lib/serialized_hashie/hash.rb b/lib/serialized_hashie/hash.rb index 27481df..00d5a3e 100644 --- a/lib/serialized_hashie/hash.rb +++ b/lib/serialized_hashie/hash.rb @@ -65,7 +65,13 @@ def load_hash(hash) def load_value(value) if value.is_a?(::Hash) hash = SerializedHashie.load_hash_extensions.run(value) - return load_hash(hash) + + # If the result is still a hash, we'll return that here + return load_hash(hash) if hash.is_a?(::Hash) + + # If the result is not a hash, we'll just return whatever + # was returned as a normal value. + return load_value(hash) end return value.map { |v| load_value(v) } if value.is_a?(Array) diff --git a/spec/serialized_hashie/hash_spec.rb b/spec/serialized_hashie/hash_spec.rb index c260f71..b0fda91 100644 --- a/spec/serialized_hashie/hash_spec.rb +++ b/spec/serialized_hashie/hash_spec.rb @@ -52,7 +52,10 @@ end context '.load' do - after(:each) { SerializedHashie.load_extensions.reset } + after(:each) do + SerializedHashie.load_extensions.reset + SerializedHashie.load_hash_extensions.reset + end it 'should create a Hashie::Mash from the given JSON' do hash = described_class.load('{"hello":"world"}') @@ -106,5 +109,13 @@ expect(hash).to be_a Hashie::Mash expect(hash).to eq({ 'some_hash' => { 'NAME' => 'Michael' } }) end + + it 'should pass hashses through their own extension and return non-hash values properly' do + SerializedHashie.load_hash_extensions.add(:test) { |hash| hash.key?('name') ? hash['name'] : hash } + hash = described_class.load('{"some_hash":{"name":"Michael"}}') + expect(hash).to be_a SerializedHashie::Hash + expect(hash).to be_a Hashie::Mash + expect(hash).to eq({ 'some_hash' => 'Michael' }) + end end end