Skip to content

Commit

Permalink
Fix: faster compilation for JSON::MappingError (#6111)
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite authored and Serdar Dogruyol committed May 22, 2018
1 parent 06e763b commit 8c24616
Showing 1 changed file with 5 additions and 18 deletions.
23 changes: 5 additions & 18 deletions src/json/mapping.cr
Expand Up @@ -108,7 +108,7 @@ module JSON
begin
%pull.read_begin_object
rescue exc : ::JSON::ParseException
raise ::JSON::MappingError.new(self.class, *%location, cause: exc)
raise ::JSON::MappingError.new(exc.message, self.class.to_s, nil, *%location, exc)
end
while %pull.kind != :end_object
%key_location = %pull.location
Expand Down Expand Up @@ -139,12 +139,12 @@ module JSON

{% if value[:nilable] || value[:default] != nil %} } {% end %}
rescue exc : ::JSON::ParseException
raise ::JSON::MappingError.new(self.class, {{value[:key] || value[:key_id].stringify}}, *%key_location, cause: exc)
raise ::JSON::MappingError.new(exc.message, self.class.to_s, {{value[:key] || value[:key_id].stringify}}, *%key_location, exc)
end
{% end %}
else
{% if strict %}
raise ::JSON::MappingError.new("Unknown JSON attribute: #{key}", self.class, *%key_location)
raise ::JSON::MappingError.new("Unknown JSON attribute: #{key}", self.class.to_s, nil, *%key_location, nil)
{% else %}
%pull.skip
{% end %}
Expand All @@ -155,7 +155,7 @@ module JSON
{% for key, value in _properties_ %}
{% unless value[:nilable] || value[:default] != nil %}
if %var{key.id}.nil? && !%found{key.id} && !::Union({{value[:type]}}).nilable?
raise ::JSON::MappingError.new("Missing JSON attribute: {{(value[:key] || value[:key_id]).id}}", self.class, *%location)
raise ::JSON::MappingError.new("Missing JSON attribute: {{(value[:key] || value[:key_id]).id}}", self.class.to_s, nil, *%location, nil)
end
{% end %}

Expand Down Expand Up @@ -235,19 +235,7 @@ module JSON
getter klass : String
getter attribute : String?

def self.new(klass : Class, line_number, column_number, *, cause : JSON::ParseException)
new(cause.message, klass, nil, line_number, column_number, cause: cause)
end

def self.new(klass : Class, attribute, line_number, column_number, *, cause : JSON::ParseException)
new(cause.message, klass, attribute, line_number, column_number, cause: cause)
end

def self.new(message : String?, klass : String | Class, line_number : Int32, column_number : Int32, cause = nil)
new(message, klass, nil, line_number, column_number, cause)
end

def initialize(message : String?, klass : String | Class, @attribute : String?, line_number : Int32, column_number : Int32, cause = nil)
def initialize(message : String?, @klass : String, @attribute : String?, line_number : Int32, column_number : Int32, cause)
message = String.build do |io|
io << message
io << "\n parsing "
Expand All @@ -260,7 +248,6 @@ module JSON
if cause
@line_number, @column_number = cause.location
end
@klass = klass.to_s
end
end
end

0 comments on commit 8c24616

Please sign in to comment.