Class: JSON::Repairer

Inherits:
Object
  • Object
show all
Includes:
JSON::Repair::StringUtils
Defined in:
lib/json/repairer.rb

Constant Summary collapse

CONTROL_CHARACTERS =
{
  "\b" => '\b',
  "\f" => '\f',
  "\n" => '\n',
  "\r" => '\r',
  "\t" => '\t'
}.freeze
ESCAPE_CHARACTERS =
{
  '"' => '"',
  '\\' => '\\',
  '/' => '/',
  'b' => "\b",
  'f' => "\f",
  'n' => "\n",
  'r' => "\r",
  't' => "\t"
}.freeze

Constants included from JSON::Repair::StringUtils

JSON::Repair::StringUtils::ACUTE_ACCENT, JSON::Repair::StringUtils::ASTERISK, JSON::Repair::StringUtils::BACKSLASH, JSON::Repair::StringUtils::BACKSPACE, JSON::Repair::StringUtils::CLOSE_PARENTHESIS, JSON::Repair::StringUtils::CLOSING_BRACE, JSON::Repair::StringUtils::CLOSING_BRACKET, JSON::Repair::StringUtils::COLON, JSON::Repair::StringUtils::COMMA, JSON::Repair::StringUtils::DOT, JSON::Repair::StringUtils::DOUBLE_QUOTE, JSON::Repair::StringUtils::DOUBLE_QUOTE_LEFT, JSON::Repair::StringUtils::DOUBLE_QUOTE_RIGHT, JSON::Repair::StringUtils::EN_QUAD, JSON::Repair::StringUtils::FORM_FEED, JSON::Repair::StringUtils::GRAVE_ACCENT, JSON::Repair::StringUtils::HAIR_SPACE, JSON::Repair::StringUtils::IDEOGRAPHIC_SPACE, JSON::Repair::StringUtils::LOWERCASE_A, JSON::Repair::StringUtils::LOWERCASE_E, JSON::Repair::StringUtils::LOWERCASE_F, JSON::Repair::StringUtils::MEDIUM_MATHEMATICAL_SPACE, JSON::Repair::StringUtils::MINUS, JSON::Repair::StringUtils::NARROW_NO_BREAK_SPACE, JSON::Repair::StringUtils::NEWLINE, JSON::Repair::StringUtils::NINE, JSON::Repair::StringUtils::NON_BREAKING_SPACE, JSON::Repair::StringUtils::OPENING_BRACE, JSON::Repair::StringUtils::OPENING_BRACKET, JSON::Repair::StringUtils::OPEN_PARENTHESIS, JSON::Repair::StringUtils::PLUS, JSON::Repair::StringUtils::QUOTE, JSON::Repair::StringUtils::QUOTE_LEFT, JSON::Repair::StringUtils::QUOTE_RIGHT, JSON::Repair::StringUtils::REGEX_DELIMITER, JSON::Repair::StringUtils::REGEX_START_OF_VALUE, JSON::Repair::StringUtils::RETURN, JSON::Repair::StringUtils::SEMICOLON, JSON::Repair::StringUtils::SLASH, JSON::Repair::StringUtils::SPACE, JSON::Repair::StringUtils::TAB, JSON::Repair::StringUtils::UPPERCASE_A, JSON::Repair::StringUtils::UPPERCASE_E, JSON::Repair::StringUtils::UPPERCASE_F, JSON::Repair::StringUtils::ZERO

Instance Method Summary collapse

Methods included from JSON::Repair::StringUtils

#control_character?, #delimiter?, #delimiter_except_slash?, #digit?, #double_quote?, #double_quote_like?, #ends_with_comma_or_newline?, #function_name?, #hex?, #insert_before_last_whitespace, #parse_keyword, #parse_keywords, #quote?, #remove_at_index, #single_quote?, #single_quote_like?, #special_whitespace?, #start_of_value?, #strip_last_occurrence, #valid_string_character?, #whitespace?

Constructor Details

#initialize(json) ⇒ Repairer

Returns a new instance of Repairer.



28
29
30
31
32
# File 'lib/json/repairer.rb', line 28

def initialize(json)
  @json = json
  @index = 0
  @output = ''
end

Instance Method Details

#repairObject



34
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
66
67
68
# File 'lib/json/repairer.rb', line 34

def repair
  processed = parse_value

  throw_unexpected_end unless processed

  processed_comma = parse_character(COMMA)
  parse_whitespace_and_skip_comments if processed_comma

  if start_of_value?(@json[@index]) && ends_with_comma_or_newline?(@output)
    # start of a new value after end of the root level object: looks like
    # newline delimited JSON -> turn into a root level array
    unless processed_comma
      # repair missing comma
      @output = insert_before_last_whitespace(@output, ',')
    end

    parse_newline_delimited_json
  elsif processed_comma
    # repair: remove trailing comma
    @output = strip_last_occurrence(@output, ',')
  end

  # repair redundant end quotes
  while @json[@index] == CLOSING_BRACE || @json[@index] == CLOSING_BRACKET
    @index += 1
    parse_whitespace_and_skip_comments
  end

  if @index >= @json.length
    # reached the end of the document properly
    return @output
  end

  throw_unexpected_character
end