diff --git a/lib/masamune/schema/map.rb b/lib/masamune/schema/map.rb index 9657952..56207e1 100644 --- a/lib/masamune/schema/map.rb +++ b/lib/masamune/schema/map.rb @@ -34,7 +34,7 @@ def gets(*a) line = __getobj__.gets(*a) return unless line return line if skip? - encode(line.split(separator)).join(separator) + encode(line, separator).join(separator) end private @@ -43,12 +43,36 @@ def skip? @store.json_encoding == :quoted end - def encode(fields = []) - fields.map { |field| field =~ /^{|}$/ ? quote(field) : field } + def encode(line, separator) + fields = [] + buffer = '' + nested = false + line.strip.each_char do |char| + case char + when '{' + buffer << char + nested = true + when '}' + buffer << char + nested = false + when separator + if nested + buffer << char + else + fields << quote(buffer) + buffer = '' + end + else + buffer << char + end + end + fields << quote(buffer) + fields.compact end - def quote(field) - %Q{"#{field.gsub(/(?