diff --git a/Sources/Jay/NativeTypeConversions.swift b/Sources/Jay/NativeTypeConversions.swift index 104b28b..46115f1 100644 --- a/Sources/Jay/NativeTypeConversions.swift +++ b/Sources/Jay/NativeTypeConversions.swift @@ -75,6 +75,10 @@ struct NativeTypeConverter { func parseNSArray(_ array: NSArray) throws -> JSON? { return try self.convertArray(array.map { $0 as Any }) } + + func parseArray(_ array: [Any]) throws -> JSON? { + return try self.convertArray(array) + } func parseNSDictionary(_ dict: NSDictionary) throws -> JSON? { var dOut = [String: Any]() @@ -88,6 +92,10 @@ struct NativeTypeConverter { } return try self.dictionaryToJayType(dOut) } + + func parseDictionary(_ dict: [String: Any]) throws -> JSON? { + return try self.dictionaryToJayType(dict) + } func arrayToJayType(_ maybeArray: Any) throws -> JSON? { @@ -126,13 +134,31 @@ struct NativeTypeConverter { guard let json = js else { return .null } if json is NSNull { return .null } + if let nativeDict = json as? [String: Any] { + guard let dict = try self.parseDictionary(nativeDict) else { + throw JayError.unsupportedType(nativeDict) + } + return dict + } + + // On OS X, this check also succeeds for [:] dicts, so this check needs + // to come after the previous one. if let nsdict = json as? NSDictionary { guard let dict = try self.parseNSDictionary(nsdict) else { throw JayError.unsupportedType(nsdict) } return dict } + + if let nativeArray = json as? [Any] { + guard let array = try self.parseArray(nativeArray) else { + throw JayError.unsupportedType(nativeArray) + } + return array + } + // On OS X, this check also succeeds for [] arrays, so this check needs + // to come after the previous one. if let nsarray = json as? NSArray { guard let array = try self.parseNSArray(nsarray) else { throw JayError.unsupportedType(nsarray) diff --git a/Tests/JayTests/FormattingTests.swift b/Tests/JayTests/FormattingTests.swift index 3b7ac9b..24cd571 100644 --- a/Tests/JayTests/FormattingTests.swift +++ b/Tests/JayTests/FormattingTests.swift @@ -17,6 +17,7 @@ extension FormattingTests { ("testNSDictionary_Empty", testNSDictionary_Empty), ("testNSDictionary_Simple", testNSDictionary_Simple), ("testObject_Simple", testObject_Simple), + ("testObject_StringInt", testObject_StringInt), ("testObject_Normal", testObject_Normal), ("testObject_Nested", testObject_Nested), ("testObject_AllTypes", testObject_AllTypes), @@ -70,6 +71,12 @@ class FormattingTests: XCTestCase { let data = try! Jay().dataFromJson(any: json) XCTAssertEqual(data, "{\"hello\":\"world\"}".chars()) } + + func testObject_StringInt() { + let json = ["hello": 42] + let data = try! Jay().dataFromJson(any: json) + XCTAssertEqual(String(bytes: data, encoding: String.Encoding.utf8)!, "{\"hello\":42}") + } func testObject_Normal() { let json: JSON = [