It's not same as #62
json:
{"id": 70, "title":"xxxx"}
mapper:
class Tigo: Mapper {
var id: Int64? // alway nil
//var id: Int? // id is 70
var title: String?
func mapping(map: Map) {
id <= map["id"]
title <= map["title"]
}
}
the field id can't be mapped when type is Int64
Number values in JSON dictionary is NSNumber in Objective-C nature, and Int, Double, Float, and Bool could be bridged from NSNumber. But because Int64 (and also Int32 and Int16) is not a subtype of NSNumber, it can not be bridged (casted).
So you might be able to parse to Int64 using TransformType explicitly, like:
id <= (map["id"], TransformOf<Int64, Int>(fromJSON: { $0.map { Int64($0) } }, toJSON: { $0.map { Int($0) } }))
Of course you can create your own transform class for reusing.
@ikesyo it works, thank you
but, how about data overflow? when id = Int.MAX + N, the transformed value is still wrong
Is that server-side thing? In that case, you should use UInt64 or String for the type of id?
Int is platform dependent, maybe 32bit or 64bit. Int64 is always 64bit.
iPhone4/4s (iOS 7.x) is 32bit.
When server send 64bit number down, Int will overflow in client.
Ah, are you referring to deserialization to JSON (toJSON conversion)? To take into account that, you could use this signature TransformOf<Int64, NSNumber>, but class ToJSON doesn't handle NSNumber as a JSON value type currently. I'll work on it.
@tianyuanzhonglu I just fixed NSNumber handling on #81, could you check the behavior on that branch with this transform?
id <- (map["id"], TransformOf<Int64, NSNumber>(fromJSON: { $0?.longLongValue }, toJSON: { $0.map { NSNumber(longLong: $0) } }))
@ikesyo I make a test, the transform work correctly. Thank you!
@tianyuanzhonglu That's a good news! You're welcome :sparkles:
Most helpful comment
Number values in JSON dictionary is
NSNumberin Objective-C nature, andInt,Double,Float, andBoolcould be bridged fromNSNumber. But becauseInt64(and alsoInt32andInt16) is not a subtype ofNSNumber, it can not be bridged (casted).So you might be able to parse to
Int64usingTransformTypeexplicitly, like:Of course you can create your own transform class for reusing.