Skip to content

Commit

Permalink
Fix JSONValue’s interpretation of 0 and 1
Browse files Browse the repository at this point in the history
It was incorrectly interpreting them as booleans. Mistake in 80e8585.
  • Loading branch information
lawrence-forooghian committed Dec 12, 2024
1 parent afeb0a1 commit 7ce9918
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
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

0 comments on commit 7ce9918

Please sign in to comment.