JASON icon indicating copy to clipboard operation
JASON copied to clipboard

Decoding large Double

Open VasilyKrainov opened this issue 2 years ago • 0 comments

This code makes the exception:

struct Response: Codable {
  let success: Bool
  let value: Double
}
func testJASON() {
  let string: String =
    """
    {"success":true,"value":100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000≥0}
    """
  let data = string.data(using: .utf8)!
  let json = JASON.JSON(data)
  let val = json["value"].double
  print("\(val)")
}

If you remove one of zeroes in "string" then the decoding will be OK. So the limit is 1e165, after which the decoding fails regardless of Double type can really fit.

VasilyKrainov avatar Jul 15 '22 15:07 VasilyKrainov