kakaopensource / KakaJSON

Fast conversion between JSON and model in Swift.
MIT License
1.16k stars 125 forks source link

NSNumber mapping error #46

Open zhuangxq opened 4 years ago

zhuangxq commented 4 years ago
let json: Array<Dictionary<String, Any>> = [ [
           "name": "KakaJSON",
           "id" :  NSNumber(6664714503134970889),
           "url": "https://github.com/kakaopensource/KakaJSON"
           ] ,
            [
          "name": "KakaJSON",
          "id" :  6664714503134970881,
          "url": "https://github.com/kakaopensource/KakaJSON"
          ]]

let repo: [Repo] = json.kj.modelArray(type: Repo.self) as! [Repo]
for item in repo {
    print(item.id)
}

after mapping, first item is 6664714503134970880, second item is right

zhuangxq commented 4 years ago
    // digit
    if let digitType = type as? DigitValue.Type {
        return Double("\(decimal)")
            .flatMap { NSNumber(value: $0) }
            .flatMap { digitType.init(truncating: $0) }
    }

Double("(decimal)") change to double , lost last number

454961172 commented 3 years ago

把 NSNumber(value: $0)的 $0转为longlong就没问题了

zhuangxq commented 3 years ago

把 NSNumber(value: $0)的 $0转为longlong就没问题了

// IntType
if let intType = type as? IntegerValue.Type {
    return Int("\(decimal)")
      .flatMap { NSNumber(value: $0) }
      .flatMap { intType.init(truncating: $0) }
}   

我是这样改的, Int类型单独处理,自己fork一份改了