Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xcode 10.2 & Swift 5 #87

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 27 additions & 8 deletions JSONCodable.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
9EDB39491B59D0AF00C63019 /* JSONHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EDB393F1B59D0AF00C63019 /* JSONHelpers.swift */; };
9EDB394D1B59D0AF00C63019 /* JSONString.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EDB39411B59D0AF00C63019 /* JSONString.swift */; };
9EDB394F1B59D0AF00C63019 /* JSONTransformer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EDB39421B59D0AF00C63019 /* JSONTransformer.swift */; };
9EDB39501B59D0AF00C63019 /* JSONTransformer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EDB39421B59D0AF00C63019 /* JSONTransformer.swift */; };
9EDB39511B59D15400C63019 /* JSONCodable.h in Headers */ = {isa = PBXBuildFile; fileRef = 9EDB39091B59D00B00C63019 /* JSONCodable.h */; settings = {ATTRIBUTES = (Public, ); }; };
A10DFC4C1DF71BF400B7D6D7 /* ClassInheritanceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A10DFC4B1DF71BF400B7D6D7 /* ClassInheritanceTests.swift */; };
A1B71C7C1D37E6BD006DA33A /* JSONEncodable+Mirror.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B71C7B1D37E6BD006DA33A /* JSONEncodable+Mirror.swift */; };
A1B71C7E1D37E90B006DA33A /* MirrorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B71C7D1D37E90B006DA33A /* MirrorTests.swift */; };
Expand Down Expand Up @@ -241,22 +239,25 @@
9EDF80101B59CFCE00E4A2D6 /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0810;
LastUpgradeCheck = 1020;
TargetAttributes = {
9E455BF61BCE185B00070A4F = {
CreatedOnToolsVersion = 7.0.1;
LastSwiftMigration = 1020;
};
9EDB39051B59D00B00C63019 = {
CreatedOnToolsVersion = 7.0;
LastSwiftMigration = 1020;
};
};
};
buildConfigurationList = 9EDF80131B59CFCE00E4A2D6 /* Build configuration list for PBXProject "JSONCodable" */;
compatibilityVersion = "Xcode 6.3";
developmentRegion = English;
developmentRegion = en;
hasScannedForEncodings = 0;
knownRegions = (
en,
Base,
);
mainGroup = 9EDF800F1B59CFCE00E4A2D6;
productRefGroup = 9EDB39071B59D00B00C63019 /* Products */;
Expand Down Expand Up @@ -384,7 +385,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = macosx;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 5.0;
};
name = Debug;
};
Expand Down Expand Up @@ -427,7 +428,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = macosx;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 5.0;
};
name = Release;
};
Expand Down Expand Up @@ -483,7 +484,7 @@
SDKROOT = iphoneos;
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2,3,4";
TVOS_DEPLOYMENT_TARGET = 9.0;
VERSIONING_SYSTEM = "apple-generic";
Expand Down Expand Up @@ -538,7 +539,7 @@
SDKROOT = iphoneos;
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2,3,4";
TVOS_DEPLOYMENT_TARGET = 9.0;
VALIDATE_PRODUCT = YES;
Expand All @@ -551,12 +552,21 @@
9EDF80141B59CFCE00E4A2D6 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
Expand All @@ -582,12 +592,21 @@
9EDF80151B59CFCE00E4A2D6 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0810"
LastUpgradeVersion = "1020"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0810"
LastUpgradeVersion = "1020"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
8 changes: 8 additions & 0 deletions JSONCodable.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
27 changes: 14 additions & 13 deletions JSONCodable/JSONDecodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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))
}
Expand All @@ -96,12 +96,13 @@ 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()
internal func parseArrayIndex(_ key: String) -> Int? {
var chars = key
let first = chars.first
chars = String(chars.dropFirst())
let last = chars.popLast()
if first == "[" && last == "]" {
return Int(String(chars))
return Int(chars)
} else {
return nil
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -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]?
Expand All @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion JSONCodable/JSONEncodable+Mirror.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions JSONCodable/JSONEncodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -246,7 +246,7 @@ public class JSONEncoder {

// [Enum]
public func encode<Enum: RawRepresentable>(_ 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)
Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions JSONCodable/JSONString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -27,8 +27,8 @@ public extension JSONEncodable {
}

private func escapeJSONString(_ str: String) -> String {
var chars = String.CharacterView("\"")
for c in str.characters {
var chars = "\""
for c in str {
switch c {
case "\\":
chars.append("\\")
Expand All @@ -41,11 +41,11 @@ private func escapeJSONString(_ str: String) -> String {
}
}
chars.append("\"")
return String(chars)
return chars
}

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()
Expand Down
16 changes: 15 additions & 1 deletion JSONCodableTests/EncodeNestingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ class EncodeNestingTests: XCTestCase {
XCTFail()
return
}
XCTAssert(String(describing:json1) == String(describing:propertyItemArray), "failed to convert to \(propertyItemArray)")

XCTAssertEqual(json1["class"] as! String, propertyItemArray["class"] as! String)
XCTAssertEqual(json1["class"] as! String, propertyItemArray["class"] as! String)

let properties = propertyItemArray["properties"] as! [String: Any]
let properties1 = json1["properties"] as! [String: Any]
XCTAssertEqual(properties1["name"] as! String, properties["name"] as! String)

let location = properties["location"] as! [String: Any]
let location1 = properties1["location"] as! [String: Any]

let coord = location["coord"] as! [String: Any]
let coord1 = location1["coord"] as! [String: Any]
XCTAssertEqual(coord["lat"] as! Double, coord1["lat"] as! Double)
XCTAssertEqual(coord["long"] as! Double, coord1["long"] as! Double)
}
}