-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NSNumber Initialization #28
Comments
Thank you, great suggestions! My thoughts:
|
Very happy to contribute 😁
|
Merged 🎉, thank you again for the contribution. |
Foundation's
JSONSerialization.jsonObject(with:options:)
transforms aData
type into an object of typeAny
. SinceJSON
s initialiser accepts anAny
, this should work, but currently does not.The primary issue is around the handling of
NSNumber
in the initialiser.JSONSerialization
usesNSNumber
to represent both numbers and booleans. This causes the following test to fail:In
JSON
's initialiser, theNSNumber
is being downcast asFloat
, thus loosing it's boolean semantics. This is best explained here.Here is SwiftyJSON's init for comparison. They also have a convenience function to determine whether an
NSNumber
is derived from a boolean.The handling of
NSNumbers
's numbers are also leading to some odd results.JSONSerialization
's number parsing.NSNumber
toDouble
conversion.Similarly, when creating a
JSON
value with a fairly typical floating point value, say a latitude, results in precision loss:Swift's
Float
represents a 32-bit floating-point number which results in precision loss in the cast above. This would not be the case ifDouble
, which represents a 64-bit, were used.My initial thinking was that we should change
JSON
's number associated value fromFloat
toDouble
to accommodate double floating-point, especially since JavaScript'sNumber
type also uses a 64-bit value. However now, looking at the SwiftyJSON implementation, maybe it should be done away with in favour ofNSNumber
.What are your thoughts?
The text was updated successfully, but these errors were encountered: