Class: JSON::Minify::Repairer
- Inherits:
-
Object
- Object
- JSON::Minify::Repairer
- Defined in:
- lib/json/minify/repairer.rb
Constant Summary collapse
- TOKENS =
{ /\s+/ => [:whitespace, :skip], /[{},:\]\[]/ => [:punctuation, :keep], /""|(".*?[^\\])?"/ => [:string, :keep], /(true|false|null)/ => [:reserved, :keep], /-?\d+([.]\d+)?([eE][-+]?[0-9]+)?/ => [:number, :keep], %r{//.*?(\r?\n|$)} => [:comment, :skip], %r{/[*].*?[*]/} => [:comment, :skip] }.freeze
- KEEP =
combine(TOKENS, :keep)
- SKIP =
combine(TOKENS, :skip)
- ALL =
Regexp.new(TOKENS.keys.join('|'))
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(str) ⇒ Repairer
constructor
A new instance of Repairer.
- #minify_parse(buf) ⇒ Object
- #parse ⇒ Object
- #tokens ⇒ Object
- #transform(token) ⇒ Object
Constructor Details
#initialize(str) ⇒ Repairer
Returns a new instance of Repairer.
31 32 33 |
# File 'lib/json/minify/repairer.rb', line 31 def initialize(str) @str = str end |
Class Method Details
.combine(tokens, type) ⇒ Object
20 21 22 23 24 25 |
# 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 |
Instance Method Details
#minify_parse(buf) ⇒ Object
78 79 80 |
# File 'lib/json/minify/repairer.rb', line 78 def minify_parse(buf) JSON.parse(minify(buf)) end |
#parse ⇒ Object
67 68 69 |
# File 'lib/json/minify/repairer.rb', line 67 def parse JSON.parse(tokens.join) end |
#tokens ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# 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) ⇒ Object
71 72 73 74 75 76 |
# File 'lib/json/minify/repairer.rb', line 71 def transform(token) case token when 'NaN' '0' end end |