Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: faster compilation for JSON::MappingError #6111

Merged
merged 1 commit into from
May 22, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 5 additions & 18 deletions src/json/mapping.cr
Original file line number Diff line number Diff line change
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