Skip to content

Commit

Permalink
Merge pull request #80 from FundingCircle/fix_issue_in_json_serializa…
Browse files Browse the repository at this point in the history
…tion

Update JSONSerializer when encoding to return value if already a JSON…
  • Loading branch information
Ahmet Abdi authored Aug 22, 2019
2 parents 7e145d9 + 94da2ba commit 2af00f6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lib/vault/rails/serializers/json_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ module JSONSerializer

def self.encode(raw)
return if raw.nil?
return raw if raw.is_a?(String)
JSON.fast_generate(raw)
end


def self.decode(raw)
return if raw.nil?
JSON.parse(raw, DECODE_OPTIONS)
Expand Down
16 changes: 12 additions & 4 deletions spec/unit/rails/serializers/json_serializer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
require 'spec_helper'

describe Vault::Rails::Serializers::JSONSerializer do
it 'encodes values to strings' do
expect(subject.encode({"foo" => "bar", "baz" => 1})).to eq '{"foo":"bar","baz":1}'
context '.encode' do
it 'encodes values to strings' do
expect(subject.encode({"foo" => "bar", "baz" => 1})).to eq '{"foo":"bar","baz":1}'
end

it 'returns values already encoded as a JSON string' do
expect(subject.encode('{"anonymised":true}')).to eq('{"anonymised":true}')
end
end

it 'decodes values from strings' do
expect(subject.decode('{"foo":"bar","baz":1}')).to eq({"foo" => "bar", "baz" => 1})
context '.decode' do
it 'decodes values from strings' do
expect(subject.decode('{"foo":"bar","baz":1}')).to eq({"foo" => "bar", "baz" => 1})
end
end
end

0 comments on commit 2af00f6

Please # to comment.