From 7ce991839749f178545b11ce1023128ecadede2e Mon Sep 17 00:00:00 2001 From: Lawrence Forooghian Date: Thu, 12 Dec 2024 15:06:05 -0300 Subject: [PATCH] =?UTF-8?q?Fix=20JSONValue=E2=80=99s=20interpretation=20of?= =?UTF-8?q?=200=20and=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It was incorrectly interpreting them as booleans. Mistake in 80e8585. --- Sources/AblyChat/JSONValue.swift | 12 ++++++++---- Tests/AblyChatTests/JSONValueTests.swift | 12 ++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/Sources/AblyChat/JSONValue.swift b/Sources/AblyChat/JSONValue.swift index 2bb383c..4aba898 100644 --- a/Sources/AblyChat/JSONValue.swift +++ b/Sources/AblyChat/JSONValue.swift @@ -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: diff --git a/Tests/AblyChatTests/JSONValueTests.swift b/Tests/AblyChatTests/JSONValueTests.swift index 8572524..93b9c00 100644 --- a/Tests/AblyChatTests/JSONValueTests.swift +++ b/Tests/AblyChatTests/JSONValueTests.swift @@ -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 @@ -33,6 +35,8 @@ struct JSONValueTests { "someArray": [ { "someStringKey": "someString", + "zero": 0, + "one": 1, "someIntegerKey": 123, "someFloatKey": 123.456, "someTrueKey": true, @@ -53,6 +57,8 @@ struct JSONValueTests { "someArray": [ [ "someStringKey": "someString", + "zero": 0, + "one": 1, "someIntegerKey": 123, "someFloatKey": 123.456, "someTrueKey": true, @@ -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 @@ -100,6 +108,8 @@ struct JSONValueTests { "someArray": [ [ "someStringKey": "someString", + "zero": 0, + "one": 1, "someIntegerKey": 123, "someFloatKey": 123.456, "someTrueKey": true, @@ -120,6 +130,8 @@ struct JSONValueTests { "someStringKey": "someString", "someIntegerKey": 123, "someFloatKey": 123.456, + "zero": 0, + "one": 1, "someTrueKey": true, "someFalseKey": false, "someNullKey": null