From 4b04ed3457edbf4678eec2e66c6a52e6368b20b1 Mon Sep 17 00:00:00 2001 From: Takeshi Tanaka Date: Thu, 16 Apr 2020 18:08:51 +0900 Subject: [PATCH 1/5] Make code compatible with Swift 4 to enable to build using Xcode 11.x --- JSONCodable/JSONDecodable.swift | 25 +++++++++++++------------ JSONCodable/JSONEncodable.swift | 8 ++++---- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/JSONCodable/JSONDecodable.swift b/JSONCodable/JSONDecodable.swift index 7c45e56..af081ea 100644 --- a/JSONCodable/JSONDecodable.swift +++ b/JSONCodable/JSONDecodable.swift @@ -70,7 +70,7 @@ public extension JSONDecodable { public extension Array where Element: JSONDecodable { init(JSONArray: [Any], filtered: Bool = false) throws { - self.init(try JSONArray.flatMap { + self.init(try JSONArray.compactMap { guard let json = $0 as? [String : Any] else { throw JSONDecodableError.dictionaryTypeExpectedError(key: "n/a", elementType: type(of: $0)) } @@ -97,11 +97,12 @@ public class JSONDecoder { /// Get index from `"[0]"` formatted `String` /// returns `nil` if invalid format (i.e. no brackets or contents not an `Int`) internal func parseArrayIndex(_ key:String) -> Int? { - var chars = key.characters - let first = chars.popFirst() - let last = chars.popLast() + var mkey = key + let first = mkey[mkey.startIndex] + mkey.remove(at: mkey.startIndex) + let last = mkey.popLast() if first == "[" && last == "]" { - return Int(String(chars)) + return Int(mkey) } else { return nil } @@ -129,7 +130,7 @@ public class JSONDecoder { return nil } } - return (result ?? object[key]).flatMap{$0 is NSNull ? nil : $0} + return result ?? object[key] } // JSONCompatible @@ -228,7 +229,7 @@ public class JSONDecoder { guard let array = value as? [JSONObject] else { throw JSONDecodableError.arrayTypeExpectedError(key: key, elementType: type(of: value)) } - return try array.flatMap { + return try array.compactMap { if filter { return try? Element(object: $0) } else { @@ -245,7 +246,7 @@ public class JSONDecoder { guard let array = value as? [JSONObject] else { throw JSONDecodableError.arrayTypeExpectedError(key: key, elementType: type(of: value)) } - return try array.flatMap { + return try array.compactMap { if filter { return try? Element(object: $0) } else { @@ -266,10 +267,10 @@ public class JSONDecoder { for x in array { if filter { - let nested = x.flatMap { try? Element(object: $0)} + let nested = x.compactMap { try? Element(object: $0)} res.append(nested) } else { - let nested = try x.flatMap { try Element(object: $0)} + let nested = try x.compactMap { try Element(object: $0)} res.append(nested) } } @@ -300,7 +301,7 @@ public class JSONDecoder { guard let array = value as? [Enum.RawValue] else { throw JSONDecodableError.arrayTypeExpectedError(key: key, elementType: type(of: value)) } - return array.flatMap { Enum(rawValue: $0) } + return array.compactMap { Enum(rawValue: $0) } } // [Enum]? @@ -311,7 +312,7 @@ public class JSONDecoder { guard let array = value as? [Enum.RawValue] else { throw JSONDecodableError.arrayTypeExpectedError(key: key, elementType: type(of: value)) } - return array.flatMap { Enum(rawValue: $0) } + return array.compactMap { Enum(rawValue: $0) } } // [String:JSONCompatible] diff --git a/JSONCodable/JSONEncodable.swift b/JSONCodable/JSONEncodable.swift index 4e62ec4..5da3dde 100644 --- a/JSONCodable/JSONEncodable.swift +++ b/JSONCodable/JSONEncodable.swift @@ -54,8 +54,8 @@ public extension JSONEncodable { let mirror = Mirror(reflecting: self) #if !swift(>=3.0) - guard let style = mirror.displayStyle where style == .Struct || style == .Class else { - throw JSONEncodableError.IncompatibleTypeError(elementType: self.dynamicType) + guard let style = mirror.displayStyle, (style == .Struct || style == .Class) else { + throw JSONEncodableError.IncompatibleTypeError(elementType: type(of: self)) } #else @@ -246,7 +246,7 @@ public class JSONEncoder { // [Enum] public func encode(_ value: [Enum], key: String) throws { - let result = try value.flatMap { + let result = try value.compactMap { try ($0.rawValue as? JSONCompatible)?.toJSON() } object = update(object: object, keys: key.components(separatedBy: "."), value: result) @@ -257,7 +257,7 @@ public class JSONEncoder { guard let actual = value else { return } - let result = try actual.flatMap { + let result = try actual.compactMap { try ($0.rawValue as? JSONCompatible)?.toJSON() } object = update(object: object, keys: key.components(separatedBy: "."), value: result) From a2e187f2c2976ba0a384f643e9722f808cda3748 Mon Sep 17 00:00:00 2001 From: Takeshi Tanaka Date: Thu, 16 Apr 2020 18:10:35 +0900 Subject: [PATCH 2/5] Remove unnecessary `public` access modifiers --- JSONCodable/JSONDecodable.swift | 4 ++-- JSONCodable/JSONEncodable+Mirror.swift | 2 +- JSONCodable/JSONEncodable.swift | 4 ++-- JSONCodable/JSONString.swift | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/JSONCodable/JSONDecodable.swift b/JSONCodable/JSONDecodable.swift index af081ea..5b0e061 100644 --- a/JSONCodable/JSONDecodable.swift +++ b/JSONCodable/JSONDecodable.swift @@ -54,12 +54,12 @@ public protocol JSONDecodable { public extension JSONDecodable { /// initialize with top-level Array JSON data - public init(object: [JSONObject]) throws { + init(object: [JSONObject]) throws { // use empty string key try self.init(object:["": object]) } - public init?(optional: JSONObject) { + init?(optional: JSONObject) { do { try self.init(object: optional) } catch { diff --git a/JSONCodable/JSONEncodable+Mirror.swift b/JSONCodable/JSONEncodable+Mirror.swift index 45d53eb..0dfbb60 100644 --- a/JSONCodable/JSONEncodable+Mirror.swift +++ b/JSONCodable/JSONEncodable+Mirror.swift @@ -12,7 +12,7 @@ public extension Mirror { - returns: array of Tuples containing the label and value for each property */ - public func getAllProperties() -> [(label: String?, value: Any)] { + func getAllProperties() -> [(label: String?, value: Any)] { var children: [(label: String?, value: Any)] = [] for element in self.children { children.append(element) diff --git a/JSONCodable/JSONEncodable.swift b/JSONCodable/JSONEncodable.swift index 5da3dde..596f210 100644 --- a/JSONCodable/JSONEncodable.swift +++ b/JSONCodable/JSONEncodable.swift @@ -105,7 +105,7 @@ public extension JSONEncodable { public extension Array { //where Element: JSONEncodable { private var wrapped: [Any] { return self.map{$0} } - public func toJSON() throws -> Any { + func toJSON() throws -> Any { var results: [Any] = [] for item in self.wrapped { if let item = item as? JSONEncodable { @@ -122,7 +122,7 @@ public extension Array { //where Element: JSONEncodable { // Dictionary convenience methods public extension Dictionary {//where Key: String, Value: JSONEncodable { - public func toJSON() throws -> Any { + func toJSON() throws -> Any { var result: [String: Any] = [:] for (k, item) in self { if let item = item as? JSONEncodable { diff --git a/JSONCodable/JSONString.swift b/JSONCodable/JSONString.swift index 88ee3c8..c45115e 100644 --- a/JSONCodable/JSONString.swift +++ b/JSONCodable/JSONString.swift @@ -9,7 +9,7 @@ import Foundation public extension JSONEncodable { - public func toJSONString() throws -> String { + func toJSONString() throws -> String { switch self { case let str as String: return escapeJSONString(str) @@ -45,7 +45,7 @@ private func escapeJSONString(_ str: String) -> String { } public extension Optional where Wrapped: JSONEncodable { - public func toJSONString() throws -> String { + func toJSONString() throws -> String { switch self { case let .some(jsonEncodable): return try jsonEncodable.toJSONString() From ac33a935f234ef756f8da762fbc7d454d0995139 Mon Sep 17 00:00:00 2001 From: Takeshi Tanaka Date: Thu, 16 Apr 2020 18:11:23 +0900 Subject: [PATCH 3/5] Change SWIFT_VERSION to 4.0 --- JSONCodable.xcodeproj/project.pbxproj | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/JSONCodable.xcodeproj/project.pbxproj b/JSONCodable.xcodeproj/project.pbxproj index 8d4d68e..66474d4 100644 --- a/JSONCodable.xcodeproj/project.pbxproj +++ b/JSONCodable.xcodeproj/project.pbxproj @@ -384,7 +384,7 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = macosx; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -427,7 +427,7 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = macosx; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; }; name = Release; }; @@ -483,7 +483,7 @@ SDKROOT = iphoneos; SKIP_INSTALL = YES; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = "1,2,3,4"; TVOS_DEPLOYMENT_TARGET = 9.0; VERSIONING_SYSTEM = "apple-generic"; @@ -538,7 +538,7 @@ SDKROOT = iphoneos; SKIP_INSTALL = YES; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = "1,2,3,4"; TVOS_DEPLOYMENT_TARGET = 9.0; VALIDATE_PRODUCT = YES; @@ -574,7 +574,7 @@ ONLY_ACTIVE_ARCH = YES; SUPPORTED_PLATFORMS = "iphoneos macosx appletvos watchos appletvsimulator iphonesimulator watchsimulator"; SWIFT_INSTALL_OBJC_HEADER = NO; - SWIFT_VERSION = 3.0.1; + SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = "1,2,3,4"; }; name = Debug; From 746b902acab3ac7e30310796b11f5dce09423f07 Mon Sep 17 00:00:00 2001 From: Takeshi Tanaka Date: Thu, 16 Apr 2020 18:19:55 +0900 Subject: [PATCH 4/5] Fix tests to pass them --- JSONCodableTests/EncodeNestingTests.swift | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/JSONCodableTests/EncodeNestingTests.swift b/JSONCodableTests/EncodeNestingTests.swift index 3c5e66c..b61e15c 100644 --- a/JSONCodableTests/EncodeNestingTests.swift +++ b/JSONCodableTests/EncodeNestingTests.swift @@ -28,6 +28,15 @@ class EncodeNestingTests: XCTestCase { XCTFail() return } - XCTAssert(String(describing:json1) == String(describing:propertyItemArray), "failed to convert to \(propertyItemArray)") + + XCTAssert(propertyItemArray.isEqual(to: json1), "failed to convert to \(propertyItemArray)") } } + +private extension JSONObject { + + func isEqual(to obj: JSONObject) -> Bool { + (self as NSDictionary).isEqual(to: obj) + } + +} From cb42f3ad615863033f0a1863288142a29d5ed765 Mon Sep 17 00:00:00 2001 From: Takeshi Tanaka Date: Fri, 17 Apr 2020 17:44:28 +0900 Subject: [PATCH 5/5] Fix to handle NSNull as nil --- JSONCodable/JSONDecodable.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JSONCodable/JSONDecodable.swift b/JSONCodable/JSONDecodable.swift index 5b0e061..4d9e81f 100644 --- a/JSONCodable/JSONDecodable.swift +++ b/JSONCodable/JSONDecodable.swift @@ -130,7 +130,7 @@ public class JSONDecoder { return nil } } - return result ?? object[key] + return (result ?? object[key]).flatMap{$0 is NSNull ? nil : $0} } // JSONCompatible