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

Fix wrong ObjC type mapping #69

Merged
merged 2 commits into from
Apr 30, 2022
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
18 changes: 9 additions & 9 deletions Sources/AnyCodable/AnyEncodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ extension _AnyEncodable {

switch value {
#if canImport(Foundation)
case let number as NSNumber:
try encode(nsnumber: number, into: &container)
case is NSNull:
try container.encodeNil()
#endif
Expand Down Expand Up @@ -89,6 +87,8 @@ extension _AnyEncodable {
case let string as String:
try container.encode(string)
#if canImport(Foundation)
case let number as NSNumber:
try encode(nsnumber: number, into: &container)
case let date as Date:
try container.encode(date)
case let url as URL:
Expand All @@ -109,21 +109,21 @@ extension _AnyEncodable {
#if canImport(Foundation)
private func encode(nsnumber: NSNumber, into container: inout SingleValueEncodingContainer) throws {
switch Character(Unicode.Scalar(UInt8(nsnumber.objCType.pointee))) {
case "c", "C":
case "B":
try container.encode(nsnumber.boolValue)
case "s":
case "c":
try container.encode(nsnumber.int8Value)
case "i":
case "s":
try container.encode(nsnumber.int16Value)
case "l":
case "i", "l":
try container.encode(nsnumber.int32Value)
case "q":
try container.encode(nsnumber.int64Value)
case "S":
case "C":
try container.encode(nsnumber.uint8Value)
case "I":
case "S":
try container.encode(nsnumber.uint16Value)
case "L":
case "I", "L":
try container.encode(nsnumber.uint32Value)
case "Q":
try container.encode(nsnumber.uint64Value)
Expand Down
36 changes: 33 additions & 3 deletions Tests/AnyCodableTests/AnyEncodableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,16 @@ class AnyEncodableTests: XCTestCase {
func testEncodeNSNumber() throws {
let dictionary: [String: NSNumber] = [
"boolean": true,
"integer": 42,
"char": -127,
"int": -32767,
"short": -32767,
"long": -2147483647,
"longlong": -9223372036854775807,
"uchar": 255,
"uint": 65535,
"ushort": 65535,
"ulong": 4294967295,
"ulonglong": 18446744073709615,
"double": 3.141592653589793,
]

Expand All @@ -82,15 +91,36 @@ class AnyEncodableTests: XCTestCase {
let expected = """
{
"boolean": true,
"integer": 42,
"char": -127,
"int": -32767,
"short": -32767,
"long": -2147483647,
"longlong": -9223372036854775807,
"uchar": 255,
"uint": 65535,
"ushort": 65535,
"ulong": 4294967295,
"ulonglong": 18446744073709615,
"double": 3.141592653589793,
}
""".data(using: .utf8)!
let expectedJSONObject = try JSONSerialization.jsonObject(with: expected, options: []) as! NSDictionary

XCTAssertEqual(encodedJSONObject, expectedJSONObject)
XCTAssert(encodedJSONObject["boolean"] is Bool)
XCTAssert(encodedJSONObject["integer"] is Int)

XCTAssert(encodedJSONObject["char"] is Int8)
XCTAssert(encodedJSONObject["int"] is Int16)
XCTAssert(encodedJSONObject["short"] is Int32)
XCTAssert(encodedJSONObject["long"] is Int32)
XCTAssert(encodedJSONObject["longlong"] is Int64)

XCTAssert(encodedJSONObject["uchar"] is UInt8)
XCTAssert(encodedJSONObject["uint"] is UInt16)
XCTAssert(encodedJSONObject["ushort"] is UInt32)
XCTAssert(encodedJSONObject["ulong"] is UInt32)
XCTAssert(encodedJSONObject["ulonglong"] is UInt64)

XCTAssert(encodedJSONObject["double"] is Double)
}

Expand Down