4

There is a bug in iOS 10, that Swift 4's JSONDecoder does not decode some huge 64 bit integers from JSON with error "Parsed JSON number <1522853867156381000> does not fit in Int64."

This is not happening in iOS 11 and it is a known bug

None of NSNumber, NSDecimalNumber and NSValue implement the Decodable protocol so I have not found a way to decode them from a Decoder.

So the question is how to decode such numbers with Codable/Decodable?

Changing the JSON, to have the numbers as strings, is not an option.

EDIT: As there is no workaround we have chosen the "not an option" and updated the server to return 64 bit integers also as strings.

shelll
  • 3,234
  • 3
  • 33
  • 67
  • Did you try to write an extension for `NSNumber` or `NSDecimalNumber` to implement `Decodable` yourself? – mag_zbc Apr 05 '18 at 07:36
  • 1
    @mag_zbc extension cannot add a required init method which `Decodable` protocol adds. – shelll Apr 05 '18 at 07:41
  • Does it work with NSJSONSerialization? That would be a workaround. – Martin R Apr 05 '18 at 07:49
  • @MartinR `NSJSONSerialization` works with checking for `NSDecimalNumber`. Problem is that my struct with that Int64 is used in several other structs, i.e. nested, and I would have to switch to `NSJSONSerialization` for every such case and it is a big rewrite... I have also some other structs with smaller Int64s but those could lead to the same issue in the future, so I will have to throw away the whole `Codable` :( – shelll Apr 05 '18 at 08:46

0 Answers0