Skip to content

Commit

Permalink
Merge branch 'release/2.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewcheok committed Dec 23, 2015
2 parents e5c1993 + f4f18bf commit 50680a4
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 129 deletions.
2 changes: 1 addition & 1 deletion JSONCodable.podspec
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'JSONCodable'
s.version = '2.0.1'
s.version = '2.1'
s.ios.deployment_target = '8.0'
s.osx.deployment_target = '10.10'
s.license = { :type => 'MIT', :file => 'LICENSE' }
Expand Down
2 changes: 1 addition & 1 deletion JSONCodable.xcodeproj/project.pbxproj
Expand Up @@ -60,7 +60,7 @@
9EDB390B1B59D00B00C63019 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
9EDB39231B59D01D00C63019 /* JSONCodable.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = JSONCodable.framework; sourceTree = BUILT_PRODUCTS_DIR; };
9EDB393C1B59D0AF00C63019 /* JSONCodable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONCodable.swift; sourceTree = "<group>"; };
9EDB393D1B59D0AF00C63019 /* JSONDecodable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONDecodable.swift; sourceTree = "<group>"; };
9EDB393D1B59D0AF00C63019 /* JSONDecodable.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 4; lastKnownFileType = sourcecode.swift; path = JSONDecodable.swift; sourceTree = "<group>"; tabWidth = 4; };
9EDB393E1B59D0AF00C63019 /* JSONEncodable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONEncodable.swift; sourceTree = "<group>"; };
9EDB393F1B59D0AF00C63019 /* JSONHelpers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONHelpers.swift; sourceTree = "<group>"; };
9EDB39411B59D0AF00C63019 /* JSONString.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONString.swift; sourceTree = "<group>"; };
Expand Down
33 changes: 20 additions & 13 deletions JSONCodable/JSONDecodable.swift
Expand Up @@ -48,16 +48,26 @@ public enum JSONDecodableError: ErrorType, CustomStringConvertible {
// Dictionary -> Struct

public protocol JSONDecodable {
init?(JSONDictionary: JSONObject)
init(object: JSONObject) throws
}

public extension JSONDecodable {
public init?(optional: JSONObject) {
do {
try self.init(object: optional)
} catch {
return nil
}
}
}

public extension Array where Element: JSONDecodable {
init(JSONArray: [AnyObject]) {
self.init(JSONArray.flatMap {
init(JSONArray: [AnyObject]) throws {
self.init(try JSONArray.flatMap {
guard let json = $0 as? [String : AnyObject] else {
return nil
throw JSONDecodableError.DictionaryTypeExpectedError(key: "n/a", elementType: $0.dynamicType)
}
return Element(JSONDictionary: json)
return try Element(object: json)
})
}
}
Expand Down Expand Up @@ -110,10 +120,7 @@ public class JSONDecoder {
guard let object = value as? JSONObject else {
throw JSONDecodableError.DictionaryTypeExpectedError(key: key, elementType: value.dynamicType)
}
guard let decodable = Decodable(JSONDictionary: object) else {
throw JSONDecodableError.IncompatibleTypeError(key: key, elementType: value.dynamicType, expectedType: Decodable.self)
}
return decodable
return try Decodable(object: object)
}

// JSONDecodable?
Expand All @@ -124,7 +131,7 @@ public class JSONDecoder {
guard let object = value as? JSONObject else {
throw JSONDecodableError.DictionaryTypeExpectedError(key: key, elementType: value.dynamicType)
}
return Decodable(JSONDictionary: object)
return try Decodable(object: object)
}

// Enum
Expand Down Expand Up @@ -185,7 +192,7 @@ public class JSONDecoder {
guard let array = value as? [JSONObject] else {
throw JSONDecodableError.ArrayTypeExpectedError(key: key, elementType: value.dynamicType)
}
return array.flatMap {Element(JSONDictionary: $0)}
return try array.flatMap { try Element(object: $0)}
}

// [JSONDecodable]?
Expand All @@ -196,7 +203,7 @@ public class JSONDecoder {
guard let array = value as? [JSONObject] else {
throw JSONDecodableError.ArrayTypeExpectedError(key: key, elementType: value.dynamicType)
}
return array.flatMap {Element(JSONDictionary: $0)}
return try array.flatMap { try Element(object: $0)}
}

// [Enum]
Expand Down Expand Up @@ -270,4 +277,4 @@ public class JSONDecoder {
}
return result
}
}
}
36 changes: 12 additions & 24 deletions JSONCodable/JSONString.swift
Expand Up @@ -56,45 +56,33 @@ public extension Optional where Wrapped: JSONEncodable {
}

public extension JSONDecodable {
init?(JSONString: String) {
init(JSONString: String) throws {
guard let data = JSONString.dataUsingEncoding(NSUTF8StringEncoding) else {
return nil
}

let result: AnyObject
do {
result = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(rawValue: 0))
}
catch {
return nil
throw JSONDecodableError.IncompatibleTypeError(key: "n/a", elementType: JSONString.dynamicType, expectedType: String.self)
}

let result: AnyObject = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(rawValue: 0))

guard let converted = result as? [String: AnyObject] else {
return nil
throw JSONDecodableError.DictionaryTypeExpectedError(key: "n/a", elementType: result.dynamicType)
}

self.init(JSONDictionary: converted)
try self.init(object: converted)
}
}

public extension Array where Element: JSONDecodable {
init?(JSONString: String) {
init(JSONString: String) throws {
guard let data = JSONString.dataUsingEncoding(NSUTF8StringEncoding) else {
return nil
}

let result: AnyObject
do {
result = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(rawValue: 0))
}
catch {
return nil
throw JSONDecodableError.IncompatibleTypeError(key: "n/a", elementType: JSONString.dynamicType, expectedType: String.self)
}

let result: AnyObject = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(rawValue: 0))

guard let converted = result as? [AnyObject] else {
return nil
throw JSONDecodableError.ArrayTypeExpectedError(key: "n/a", elementType: result.dynamicType)
}

self.init(JSONArray: converted)
try self.init(JSONArray: converted)
}
}
13 changes: 4 additions & 9 deletions JSONCodableTests/Company.swift
Expand Up @@ -21,14 +21,9 @@ func ==(lhs: Company, rhs: Company) -> Bool {
extension Company: JSONEncodable {}

extension Company: JSONDecodable {
init?(JSONDictionary: JSONObject) {
let decoder = JSONDecoder(object: JSONDictionary)
do {
name = try decoder.decode("name")
address = try decoder.decode("address")
}
catch {
return nil
}
init(object: JSONObject) throws {
let decoder = JSONDecoder(object: object)
name = try decoder.decode("name")
address = try decoder.decode("address")
}
}
4 changes: 2 additions & 2 deletions JSONCodableTests/EnumTests.swift
Expand Up @@ -17,14 +17,14 @@ class EnumTests: XCTestCase {
let decodedValue2 = Food(name: "Seaweed Pasta", cuisines: [.Italian, .Japanese])

func testDecodingEnum() {
guard let fruit = Fruit(JSONDictionary: encodedValue) else {
guard let fruit = try? Fruit(object: encodedValue) else {
XCTFail()
return
}

XCTAssertEqual(fruit, decodedValue)

guard let food = Food(JSONDictionary: encodedValue2) else {
guard let food = try? Food(object: encodedValue2) else {
XCTFail()
return
}
Expand Down
13 changes: 4 additions & 9 deletions JSONCodableTests/Food.swift
Expand Up @@ -32,15 +32,10 @@ func ==(lhs: Food, rhs: Food) -> Bool {
}

extension Food: JSONCodable {
init?(JSONDictionary: JSONObject) {
let decoder = JSONDecoder(object: JSONDictionary)
do {
name = try decoder.decode("name")
cuisines = try decoder.decode("cuisines")
}
catch {
return nil
}
init(object: JSONObject) throws {
let decoder = JSONDecoder(object: object)
name = try decoder.decode("name")
cuisines = try decoder.decode("cuisines")
}

func toJSON() throws -> AnyObject {
Expand Down
13 changes: 4 additions & 9 deletions JSONCodableTests/Fruit.swift
Expand Up @@ -24,15 +24,10 @@ func ==(lhs: Fruit, rhs: Fruit) -> Bool {
}

extension Fruit: JSONCodable {
init?(JSONDictionary: JSONObject) {
let decoder = JSONDecoder(object: JSONDictionary)
do {
name = try decoder.decode("name")
color = try decoder.decode("color")
}
catch {
return nil
}
init(object: JSONObject) throws {
let decoder = JSONDecoder(object: object)
name = try decoder.decode("name")
color = try decoder.decode("color")
}

func toJSON() throws -> AnyObject {
Expand Down
13 changes: 4 additions & 9 deletions JSONCodableTests/ImageAsset.swift
Expand Up @@ -28,14 +28,9 @@ extension ImageAsset: JSONEncodable {
}

extension ImageAsset: JSONDecodable {
init?(JSONDictionary: JSONObject) {
let decoder = JSONDecoder(object: JSONDictionary)
do {
name = try decoder.decode("name")
uri = try decoder.decode("uri", transformer: JSONTransformers.StringToNSURL)
}
catch {
return nil
}
init(object: JSONObject) throws {
let decoder = JSONDecoder(object: object)
name = try decoder.decode("name")
uri = try decoder.decode("uri", transformer: JSONTransformers.StringToNSURL)
}
}
4 changes: 2 additions & 2 deletions JSONCodableTests/RegularTests.swift
Expand Up @@ -34,14 +34,14 @@ class RegularTests: XCTestCase {
])

func testDecodingRegular() {
guard let user = User(JSONDictionary: encodedValue) else {
guard let user = try? User(object: encodedValue) else {
XCTFail()
return
}

XCTAssertEqual(user, decodedValue)
}

func testEncodingRegular() {
guard let json = try? decodedValue.toJSON() else {
XCTFail()
Expand Down
2 changes: 1 addition & 1 deletion JSONCodableTests/TransformerTests.swift
Expand Up @@ -20,7 +20,7 @@ class TransformerTests: XCTestCase {
)

func testDecodingTransformer() {
guard let asset = ImageAsset(JSONDictionary: encodedValue) else {
guard let asset = try? ImageAsset(object: encodedValue) else {
XCTFail()
return
}
Expand Down
19 changes: 7 additions & 12 deletions JSONCodableTests/User.swift
Expand Up @@ -38,17 +38,12 @@ extension User: JSONEncodable {
}

extension User: JSONDecodable {
init?(JSONDictionary: JSONObject) {
let decoder = JSONDecoder(object: JSONDictionary)
do {
id = try decoder.decode("id")
name = try decoder.decode("full_name")
email = try decoder.decode("email")
company = try decoder.decode("company")
friends = try decoder.decode("friends")
}
catch {
return nil
}
init(object: JSONObject) throws {
let decoder = JSONDecoder(object: object)
id = try decoder.decode("id")
name = try decoder.decode("full_name")
email = try decoder.decode("email")
company = try decoder.decode("company")
friends = try decoder.decode("friends")
}
}

0 comments on commit 50680a4

Please sign in to comment.