From a4576f3e1810077143a6a29a7e5501d3b0773cef Mon Sep 17 00:00:00 2001 From: Michael Andrews Date: Tue, 17 Mar 2015 17:58:27 -0700 Subject: [PATCH 1/2] Do not attempt to quote partially quoted JSON --- lib/masamune/schema/map.rb | 34 +++++++++++++++++++++++++++----- spec/masamune/schema/map_spec.rb | 26 +++++++++++++++--------- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/lib/masamune/schema/map.rb b/lib/masamune/schema/map.rb index 9657952..1122097 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.chomp.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(/(? Date: Wed, 18 Mar 2015 15:49:03 -0700 Subject: [PATCH 2/2] Use strip instead of chomp --- lib/masamune/schema/map.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/masamune/schema/map.rb b/lib/masamune/schema/map.rb index 1122097..56207e1 100644 --- a/lib/masamune/schema/map.rb +++ b/lib/masamune/schema/map.rb @@ -47,7 +47,7 @@ def encode(line, separator) fields = [] buffer = '' nested = false - line.chomp.each_char do |char| + line.strip.each_char do |char| case char when '{' buffer << char