123456789_123456789_123456789_123456789_123456789_

Exception: JSON::ParserError

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, JSONError, StandardError
Instance Chain:
self, JSONError, StandardError
Inherits: JSON::JSONError
Defined in: ext/json/lib/json/common.rb

Overview

This exception is raised if a parser error occurs.

Instance Attribute Summary

  • #column readonly

    Column number where the parser encountered an error.

  • #line readonly

    Line number where the parser encountered an error.

Instance Method Summary

Instance Attribute Details

#column (readonly)

Column number where the parser encountered an error. Is nil when raised by ResumableParser.

[ GitHub ]

  
# File 'ext/json/lib/json/common.rb', line 151

attr_reader :column

#line (readonly)

Line number where the parser encountered an error. Is nil when raised by ResumableParser.

[ GitHub ]

  
# File 'ext/json/lib/json/common.rb', line 147

attr_reader :line

Instance Method Details

#build_json_path(segments) (private)

[ GitHub ]

  
# File 'ext/json/lib/json/common.rb', line 173

def build_json_path(segments)
  error = false
  path = segments.filter_map do |segment|
    next if error

    case segment
    when Integer
      "[#{segment}]"
    when String, Symbol
      if segment.match?(/\A[a-zA-Z\$\_][a-zA-Z\$\_0-9]*\z/)
        ".#{segment}"
      else
        segment = segment.to_s.gsub(/["\\]/, { '"' => '\\"', '\\' => '\\\\' })
        %{["#{segment}"]}
      end
    else
      error = true
      nil
    end
  end.join
  "$#{path}".freeze
end

#json_path

Returns a best effort JSONPath string representing where in the document the parser encountered an error:

begin
JSON.parse('{"articles": [ { "title": invalid } ]}')
rescue JSON::ParserError => error
error.json_path # => "$.articles[0].title"
end
[ GitHub ]

  
# File 'ext/json/lib/json/common.rb', line 161

def json_path
  return @json_path if String === @json_path

  if Array === @json_path
    path = build_json_path(@json_path)
    @json_path = path unless frozen?
    return path
  end
end