Class: DrJson

Inherits:
Object
  • Object
show all
Defined in:
lib/drjson.rb

Defined Under Namespace

Classes: UnexpectedTokenError

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ DrJson

Returns a new instance of DrJson.



41
42
43
# File 'lib/drjson.rb', line 41

def initialize(options = {})
  @debug = options.fetch(:debug, false)
end

Instance Method Details

#repair(json_str) ⇒ Object

Parses and repairs the possbily cut-off input ‘json_str` in recursive descent fashion. Returns `json_str` with necesarry closing tags appended.



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/drjson.rb', line 26

def repair(json_str)
  @input = StringScanner.new(json_str)
  @result = ""
 
  begin
    # Real world JSON has arrays as top level elements, yep.
    parse_object || parse_array
    spaces
  rescue UnexpectedTokenError => e
    raise e if debug
  end
  result
end