crystal tool expand mapping_test.cr
$ cat mapping_test.cr
require "json"
class Location
JSON.mapping( # <---- line: 4, column: 3
lat: Float64,
lng: Float64,
)
end
class House
JSON.mapping(
address: String,
location: {type: Location, nilable: true},
)
end
house = House.from_json(%({"address": "Crystal Road 1234", "location": {"lat": 12.3, "lng": 34.5}}))
house.address # => "Crystal Road 1234"
house.location # => #<Location:0x10cd93d80 @lat=12.3, @lng=34.5>
house.to_json # => %({"address":"Crystal Road 1234","location":{"lat":12.3,"lng":34.5}})
$ crystal tool expand -c mapping_test.cr:4:3 mapping_test.cr
1 expansion found
expansion 1:
JSON.mapping(lat: Float64, lng: Float64)
~> ::JSON.mapping({lat: {type: Float64}, lng: {type: Float64}})
~> @lat : Float64
def lat=(_lat : Float64)
@lat = _lat
end
def lat
@lat
end
@lng : Float64
def lng=(_lng : Float64)
@lng = _lng
end
def lng
@lng
end
def initialize(__temp_25 : ::JSON::PullParser)
__temp_26 = nil
__temp_27 = false
__temp_28 = nil
__temp_29 = false
__temp_25.read_object do |key|
case key
when "lat"
__temp_27 = true
__temp_26 = Float64.new(__temp_25)
when "lng"
__temp_29 = true
__temp_28 = Float64.new(__temp_25)
else
__temp_25.skip
end
end
if ((__temp_26.nil?) && (!__temp_27)) && (!::Union(Float64).nilable?)
raise(::JSON::ParseException.new("Missing json attribute: lat", 0, 0))
end
if ((__temp_28.nil?) && (!__temp_29)) && (!::Union(Float64).nilable?)
raise(::JSON::ParseException.new("Missing json attribute: lng", 0, 0))
end
@lat = __temp_26.as(Float64)
@lng = __temp_28.as(Float64)
end
def to_json(json : ::JSON::Builder)
json.object do
_lat = @lat
if _lat.nil?
else
json.field("lat") do
_lat.to_json(json)
end
end
_lng = @lng
if _lng.nil?
else
json.field("lng") do
_lng.to_json(json)
end
end
end
end