Skip to content
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

Fix JSONValue’s interpretation of 0 and 1 #194

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions Sources/AblyChat/JSONValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,15 @@ internal extension JSONValue {
self = .array(array.map { .init(ablyCocoaPresenceData: $0) })
case let string as String:
self = .string(string)
// The order here is important, since a Bool can satisfy the NSNumber check
case let bool as Bool:
self = .bool(bool)
case let number as NSNumber:
self = .number(number.doubleValue)
// We need to be careful to distinguish booleans from numbers of value 0 or 1; technique taken from https://forums.swift.org/t/jsonserialization-turns-bool-value-to-nsnumber/31909/3
if number === kCFBooleanTrue {
self = .bool(true)
} else if number === kCFBooleanFalse {
self = .bool(false)
} else {
self = .number(number.doubleValue)
}
case is NSNull:
self = .null
default:
Expand Down
12 changes: 12 additions & 0 deletions Tests/AblyChatTests/JSONValueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ struct JSONValueTests {
// string
(ablyCocoaPresenceData: "someString", expectedResult: "someString"),
// number
(ablyCocoaPresenceData: NSNumber(value: 0), expectedResult: 0),
(ablyCocoaPresenceData: NSNumber(value: 1), expectedResult: 1),
(ablyCocoaPresenceData: NSNumber(value: 123), expectedResult: 123),
(ablyCocoaPresenceData: NSNumber(value: 123.456), expectedResult: 123.456),
// bool
Expand All @@ -33,6 +35,8 @@ struct JSONValueTests {
"someArray": [
{
"someStringKey": "someString",
"zero": 0,
"one": 1,
"someIntegerKey": 123,
"someFloatKey": 123.456,
"someTrueKey": true,
Expand All @@ -53,6 +57,8 @@ struct JSONValueTests {
"someArray": [
[
"someStringKey": "someString",
"zero": 0,
"one": 1,
"someIntegerKey": 123,
"someFloatKey": 123.456,
"someTrueKey": true,
Expand All @@ -79,6 +85,8 @@ struct JSONValueTests {
// string
(value: "someString", expectedResult: "someString"),
// number
(value: 0, expectedResult: NSNumber(value: 0)),
(value: 1, expectedResult: NSNumber(value: 1)),
(value: 123, expectedResult: NSNumber(value: 123)),
(value: 123.456, expectedResult: NSNumber(value: 123.456)),
// bool
Expand All @@ -100,6 +108,8 @@ struct JSONValueTests {
"someArray": [
[
"someStringKey": "someString",
"zero": 0,
"one": 1,
"someIntegerKey": 123,
"someFloatKey": 123.456,
"someTrueKey": true,
Expand All @@ -120,6 +130,8 @@ struct JSONValueTests {
"someStringKey": "someString",
"someIntegerKey": 123,
"someFloatKey": 123.456,
"zero": 0,
"one": 1,
"someTrueKey": true,
"someFalseKey": false,
"someNullKey": null
Expand Down
Loading