Skip to content
This repository has been archived by the owner on Oct 5, 2023. It is now read-only.

Commit

Permalink
Ensure double quotes are not escaped twice
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Andrews committed Mar 17, 2015
1 parent 90b2a55 commit bc3c145
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/masamune/schema/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ def skip?
end

def encode(fields = [])
fields.map { |field| field =~ /^{|}$/ ? %Q{"#{field.gsub('"', '""')}"} : field }
fields.map { |field| field =~ /^{|}$/ ? quote(field) : field }
end

def quote(field)
%Q{"#{field.gsub(/(?<!")"(?!")/, '""')}"}
end

def separator
Expand Down
32 changes: 32 additions & 0 deletions spec/masamune/schema/map_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -530,4 +530,36 @@
it_behaves_like 'apply input/output'
end
end

describe Masamune::Schema::Map::JSONEncoder do
let(:io) { StringIO.new }
let(:store) { double(json_encoding: :raw, format: :csv) }
let(:encoder) { described_class.new(io, store) }

subject { encoder.gets }

context 'with raw json' do
before do
io.write '{"enabled":true}'
io.rewind
end
it { is_expected.to eq(%Q{"{""enabled"":true}"}) }
end

context 'with quoted json' do
before do
io.write '"{""enabled"":true}"'
io.rewind
end
it { is_expected.to eq(%Q{"{""enabled"":true}"}) }
end

context 'with partially quoted json' do
before do
io.write '{""enabled"":true}'
io.rewind
end
it { is_expected.to eq(%Q{"{""enabled"":true}"}) }
end
end
end

0 comments on commit bc3c145

Please sign in to comment.