Skip to content

Instantly share code, notes, and snippets.

@acalism
Created October 27, 2017 13:17
Show Gist options
  • Save acalism/aa35527f5e0e3068a54245d85167f21e to your computer and use it in GitHub Desktop.
Save acalism/aa35527f5e0e3068a54245d85167f21e to your computer and use it in GitHub Desktop.
JSONEncoder, ObjectMapper, HandyJson
let TransformInt64 = TransformOf<Int64, String>(fromJSON: { (value: String?) -> Int64? in
guard let v = value else {
return nil
}
return Int64(value!)
}, toJSON: { (value: Int64?) -> String? in
return String(value ?? 0)
})
let TransformUInt64 = TransformOf<UInt64, String>(fromJSON: { (value: String?) -> UInt64? in
guard let v = value else {
return nil
}
return UInt64(value!)
}, toJSON: { (value: UInt64?) -> String? in
return String(value ?? 0)
})
struct UserInfoOut: Codable, Mappable {
var userId : Int64 = 0
var uin : String = "uin"
var unionid : String = "unionid"
var openid : String = "openid"
var nickName : String = "nick name"
var trueName : String = "true name"
var headimgURL : String = "http://im.qq.com"
var backgroundURL : String = ""
var country : String = "美国"
var province : String = "加州"
var city : String = "西雅图"
var phone : String = "18126516589"
var memo : String = ""
var clientIp : String = ""
var createTime : String = ""
var modifyTime : String = ""
var userType : Int32 = 0
var sysType : Int32 = 0
var sex : Int32 = 0
var channel : Int32 = 0
var state : Int32 = 0
private enum CodingKeys: String, CodingKey {
case userId = "user_id"
case uin = "uin"
case unionid = "unionid"
case openid = "openid"
case nickName = "nick_name"
case trueName = "true_name"
case headimgURL = "headimg_url"
case backgroundURL = "background_url"
case sex = "sex"
case country = "country"
case province = "province"
case city = "city"
case phone = "phone"
case channel = "channel"
case state = "state"
case memo = "memo"
case clientIp = "client_ip"
case createTime = "create_time"
case modifyTime = "modify_time"
case userType = "user_type"
case sysType = "sys_type"
}
init() {}
init?(map: Map) {
self.init()
}
mutating func mapping(map : Map) {
userId <- (map["user_id"], TransformInt64)
uin <- map["uin"]
unionid <- map["unionid"]
openid <- map["openid"]
nickName <- map["nick_name"]
trueName <- map["true_name"]
headimgURL <- map["headimg_url"]
backgroundURL <- map["background_url"]
sex <- map["sex"]
country <- map["country"]
province <- map["province"]
city <- map["city"]
phone <- map["phone"]
channel <- map["channel"]
state <- map["state"]
memo <- map["memo"]
clientIp <- map["client_ip"]
createTime <- map["create_time"]
modifyTime <- map["modify_time"]
userType <- map["user_type"]
sysType <- map["sys_type"]
}
}
struct ClassRoom: Codable, Mappable {
var students = [UserInfoOut].init(repeating: UserInfoOut(), count: 45)
var teachers = [UserInfoOut].init(repeating: UserInfoOut(), count: 3)
var header = UserInfoOut()
var number = 1024
init() {}
init?(map: Map) {
self.init()
}
mutating func mapping(map: Map) {
students <- map["students"]
teachers <- map["teachers"]
header <- map["header"]
number <- map["number"]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment