Ruby JSON parser with clear errors
Ruby
require 'json'
class JsonInputError < StandardError; end
def parse_json_strict(text)
raise JsonInputError, 'JSON input must be a string' unless text.is_a?(String)
JSON.parse(text)
rescue JSON::ParserError => error
line = error.message[/line (\d+)/, 1]
column = error.message[/column (\d+)/, 1]
where = line ? %Q( at line #{line}, column #{column}) : ''
raise JsonInputError, %Q(invalid JSON#{where}: #{error.message})
end