Skip to content
This repository was archived by the owner on Oct 19, 2024. It is now read-only.

Commit a916e9d

Browse files
authored
Fix wrong ObjC type mapping (#69)
* Fix wrong conversion from ObjC type * Update unit tests
1 parent 11423ef commit a916e9d

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

Sources/AnyCodable/AnyEncodable.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ extension _AnyEncodable {
5353

5454
switch value {
5555
#if canImport(Foundation)
56-
case let number as NSNumber:
57-
try encode(nsnumber: number, into: &container)
5856
case is NSNull:
5957
try container.encodeNil()
6058
#endif
@@ -89,6 +87,8 @@ extension _AnyEncodable {
8987
case let string as String:
9088
try container.encode(string)
9189
#if canImport(Foundation)
90+
case let number as NSNumber:
91+
try encode(nsnumber: number, into: &container)
9292
case let date as Date:
9393
try container.encode(date)
9494
case let url as URL:
@@ -109,21 +109,21 @@ extension _AnyEncodable {
109109
#if canImport(Foundation)
110110
private func encode(nsnumber: NSNumber, into container: inout SingleValueEncodingContainer) throws {
111111
switch Character(Unicode.Scalar(UInt8(nsnumber.objCType.pointee))) {
112-
case "c", "C":
112+
case "B":
113113
try container.encode(nsnumber.boolValue)
114-
case "s":
114+
case "c":
115115
try container.encode(nsnumber.int8Value)
116-
case "i":
116+
case "s":
117117
try container.encode(nsnumber.int16Value)
118-
case "l":
118+
case "i", "l":
119119
try container.encode(nsnumber.int32Value)
120120
case "q":
121121
try container.encode(nsnumber.int64Value)
122-
case "S":
122+
case "C":
123123
try container.encode(nsnumber.uint8Value)
124-
case "I":
124+
case "S":
125125
try container.encode(nsnumber.uint16Value)
126-
case "L":
126+
case "I", "L":
127127
try container.encode(nsnumber.uint32Value)
128128
case "Q":
129129
try container.encode(nsnumber.uint64Value)

Tests/AnyCodableTests/AnyEncodableTests.swift

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,16 @@ class AnyEncodableTests: XCTestCase {
7070
func testEncodeNSNumber() throws {
7171
let dictionary: [String: NSNumber] = [
7272
"boolean": true,
73-
"integer": 42,
73+
"char": -127,
74+
"int": -32767,
75+
"short": -32767,
76+
"long": -2147483647,
77+
"longlong": -9223372036854775807,
78+
"uchar": 255,
79+
"uint": 65535,
80+
"ushort": 65535,
81+
"ulong": 4294967295,
82+
"ulonglong": 18446744073709615,
7483
"double": 3.141592653589793,
7584
]
7685

@@ -82,15 +91,36 @@ class AnyEncodableTests: XCTestCase {
8291
let expected = """
8392
{
8493
"boolean": true,
85-
"integer": 42,
94+
"char": -127,
95+
"int": -32767,
96+
"short": -32767,
97+
"long": -2147483647,
98+
"longlong": -9223372036854775807,
99+
"uchar": 255,
100+
"uint": 65535,
101+
"ushort": 65535,
102+
"ulong": 4294967295,
103+
"ulonglong": 18446744073709615,
86104
"double": 3.141592653589793,
87105
}
88106
""".data(using: .utf8)!
89107
let expectedJSONObject = try JSONSerialization.jsonObject(with: expected, options: []) as! NSDictionary
90108

91109
XCTAssertEqual(encodedJSONObject, expectedJSONObject)
92110
XCTAssert(encodedJSONObject["boolean"] is Bool)
93-
XCTAssert(encodedJSONObject["integer"] is Int)
111+
112+
XCTAssert(encodedJSONObject["char"] is Int8)
113+
XCTAssert(encodedJSONObject["int"] is Int16)
114+
XCTAssert(encodedJSONObject["short"] is Int32)
115+
XCTAssert(encodedJSONObject["long"] is Int32)
116+
XCTAssert(encodedJSONObject["longlong"] is Int64)
117+
118+
XCTAssert(encodedJSONObject["uchar"] is UInt8)
119+
XCTAssert(encodedJSONObject["uint"] is UInt16)
120+
XCTAssert(encodedJSONObject["ushort"] is UInt32)
121+
XCTAssert(encodedJSONObject["ulong"] is UInt32)
122+
XCTAssert(encodedJSONObject["ulonglong"] is UInt64)
123+
94124
XCTAssert(encodedJSONObject["double"] is Double)
95125
}
96126

0 commit comments

Comments
 (0)