lib/jason.rb in jason-0.3.0 vs lib/jason.rb in jason-0.3.1

- old
+ new

@@ -30,21 +30,32 @@ # @return [String] the compiled template def self.compile(template) "#{eruby_template(template).src}; Jason.process(_buf)" end + # Process a rendered buffer. + # + # Removes any trailing commas and compresses the buffer. + # + # Usually, you should not have to call this method. + # + # @param [String] buffer def self.process(buffer) JSON.load(remove_trailing_commas(buffer)).to_json end private def self.eruby_template(template) JSONEruby.new(template) end + # Based upon the string scanner found in jnunemaker's crack. + # + # @see https://github.com/jnunemaker/crack crack def self.remove_trailing_commas(json) + json = json.dup comma_position = nil quoting = nil scanner = StringScanner.new(json) while scanner.scan_until(/(\\['"]|['",\]\}])/) @@ -54,10 +65,10 @@ quoting = char elsif quoting == char quoting = nil end when ']', '}' - if comma_position && json[comma_position + 1...scanner.pos - 1] =~ /^\s*$/ + if comma_position && json[comma_position + 1...scanner.pos - 1] =~ /\A\s*\z/ json[comma_position] = '' scanner.pos -= 1 comma_position = nil end when ',' \ No newline at end of file