class JSON::Minify::Repairer

Constants

ALL
KEEP
SKIP
TOKENS

Public Class Methods

combine(tokens, type) click to toggle source
# File lib/json/minify/repairer.rb, line 20
def self.combine(tokens, type)
  regexes = tokens.map do |key, bits|
    key if bits[1] == type
  end.compact
  Regexp.new(regexes.join('|'))
end
new(str) click to toggle source
# File lib/json/minify/repairer.rb, line 31
def initialize(str)
  @str = str
end

Public Instance Methods

minify_parse(buf) click to toggle source
# File lib/json/minify/repairer.rb, line 78
def minify_parse(buf)
  JSON.parse(minify(buf))
end
parse() click to toggle source
# File lib/json/minify/repairer.rb, line 67
def parse
  JSON.parse(tokens.join)
end
tokens() click to toggle source
# File lib/json/minify/repairer.rb, line 35
def tokens
  scanner, buf = StringScanner.new(@str), []

  until scanner.eos?
    token = scanner.scan(KEEP)
    buf << token if token
    skip = scanner.skip(SKIP)

    # Anything else is invalid JSON
    next if skip || token || scanner.eos?

    cur = scanner.pos
    extra = scanner.scan_until(ALL)
    if extra
      invalid_string = scanner.pre_match[cur..-1]
      next_token = scanner.matched
      buf << next_token if KEEP.match(next_token)
    else
      invalid_string = scanner.rest
    end

    alternate_token = transform(invalid_string)
    if alternate_token
      buf << alternate_token
    else
      raise SyntaxError, "Unable to pre-scan string: #{invalid_string}"
    end
  end

  buf
end
transform(token) click to toggle source
# File lib/json/minify/repairer.rb, line 71
def transform(token)
  case token
  when 'NaN'
    '0'
  end
end