Skip to content

Commit

Permalink
<-- no longer throws
Browse files Browse the repository at this point in the history
  • Loading branch information
isair committed Jun 13, 2016
1 parent b37bf6b commit f56cb4c
Show file tree
Hide file tree
Showing 17 changed files with 138 additions and 132 deletions.
4 changes: 2 additions & 2 deletions JSONHelper.podspec
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Pod::Spec.new do |s|
s.name = 'JSONHelper'
s.version = '2.0.0'
s.version = '2.1.0'
s.license = { :type => 'zlib', :file => 'LICENSE' }
s.summary = 'Lightning fast JSON deserialization and value conversion library for iOS, tvOS, and OS X written in Swift.'
s.summary = 'Convert anything into anything in one operation; hex strings into UIColor/NSColor, JSON strings into class instances, y/n strings to booleans, arrays and dictionaries of these; anything you can make sense of!'

s.homepage = 'https://github.com/isair/JSONHelper'
s.author = { 'Baris Sencan' => '[email protected]' }
Expand Down
70 changes: 42 additions & 28 deletions JSONHelper/Conversion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public protocol Convertible {

// MARK: - Basic Conversion

public func <-- <T, U>(inout lhs: T?, rhs: U?) throws -> T? {
public func <-- <T, U>(inout lhs: T?, rhs: U?) -> T? {
if !(lhs is NSNull) {
lhs = JSONHelper.convertToNilIfNull(rhs) as? T
} else {
Expand All @@ -35,28 +35,41 @@ public func <-- <T, U>(inout lhs: T?, rhs: U?) throws -> T? {
return lhs
}

public func <-- <T, U>(inout lhs: T, rhs: U?) throws -> T {
public func <-- <T, U>(inout lhs: T, rhs: U?) -> T {
var newValue: T?
try newValue <-- rhs
newValue <-- rhs
lhs = newValue ?? lhs
return lhs
}

public func <-- <C: Convertible, T>(inout lhs: C?, rhs: T?) throws -> C? {
lhs = try C.convertFromValue(JSONHelper.convertToNilIfNull(rhs))
public func <-- <C: Convertible, T>(inout lhs: C?, rhs: T?) -> C? {
lhs = nil

do {
lhs = try C.convertFromValue(JSONHelper.convertToNilIfNull(rhs))
} catch ConversionError.InvalidValue {
#if DEBUG
print("Invalid value \(rhs.debugDescription) for supported type.")
#endif
} catch ConversionError.UnsupportedType {
#if DEBUG
print("Unsupported type.")
#endif
} catch {}

return lhs
}

public func <-- <C: Convertible, T>(inout lhs: C, rhs: T?) throws -> C {
public func <-- <C: Convertible, T>(inout lhs: C, rhs: T?) -> C {
var newValue: C?
try newValue <-- rhs
newValue <-- rhs
lhs = newValue ?? lhs
return lhs
}

// MARK: - Array Conversion

public func <-- <C: Convertible, T>(inout lhs: [C]?, rhs: [T]?) throws -> [C]? {
public func <-- <C: Convertible, T>(inout lhs: [C]?, rhs: [T]?) -> [C]? {
guard let rhs = rhs else {
lhs = nil
return lhs
Expand All @@ -65,7 +78,8 @@ public func <-- <C: Convertible, T>(inout lhs: [C]?, rhs: [T]?) throws -> [C]? {
lhs = [C]()
for element in rhs {
var convertedElement: C?
try convertedElement <-- element
convertedElement <-- element

if let convertedElement = convertedElement {
lhs?.append(convertedElement)
}
Expand All @@ -74,36 +88,36 @@ public func <-- <C: Convertible, T>(inout lhs: [C]?, rhs: [T]?) throws -> [C]? {
return lhs
}

public func <-- <C: Convertible, T>(inout lhs: [C], rhs: [T]?) throws -> [C] {
public func <-- <C: Convertible, T>(inout lhs: [C], rhs: [T]?) -> [C] {
var newValue: [C]?
try newValue <-- rhs
newValue <-- rhs
lhs = newValue ?? lhs
return lhs
}

public func <-- <C: Convertible, T>(inout lhs: [C]?, rhs: T?) throws -> [C]? {
public func <-- <C: Convertible, T>(inout lhs: [C]?, rhs: T?) -> [C]? {
guard let rhs = rhs else {
lhs = nil
return lhs
}

if let elements = rhs as? NSArray as? [AnyObject] {
return try lhs <-- elements
return lhs <-- elements
}

throw ConversionError.UnsupportedType
return nil
}

public func <-- <C: Convertible, T>(inout lhs: [C], rhs: T?) throws -> [C] {
public func <-- <C: Convertible, T>(inout lhs: [C], rhs: T?) -> [C] {
var newValue: [C]?
try newValue <-- rhs
newValue <-- rhs
lhs = newValue ?? lhs
return lhs
}

// MARK: - Dictionary Conversion

public func <-- <T, C: Convertible, U>(inout lhs: [T : C]?, rhs: [T : U]?) throws -> [T : C]? {
public func <-- <T, C: Convertible, U>(inout lhs: [T : C]?, rhs: [T : U]?) -> [T : C]? {
guard let rhs = rhs else {
lhs = nil
return lhs
Expand All @@ -112,7 +126,7 @@ public func <-- <T, C: Convertible, U>(inout lhs: [T : C]?, rhs: [T : U]?) throw
lhs = [T : C]()
for (key, value) in rhs {
var convertedValue: C?
try convertedValue <-- value
convertedValue <-- value
if let convertedValue = convertedValue {
lhs?[key] = convertedValue
}
Expand All @@ -121,36 +135,36 @@ public func <-- <T, C: Convertible, U>(inout lhs: [T : C]?, rhs: [T : U]?) throw
return lhs
}

public func <-- <T, C: Convertible, U>(inout lhs: [T : C], rhs: [T : U]?) throws -> [T : C] {
public func <-- <T, C: Convertible, U>(inout lhs: [T : C], rhs: [T : U]?) -> [T : C] {
var newValue: [T : C]?
try newValue <-- rhs
newValue <-- rhs
lhs = newValue ?? lhs
return lhs
}

public func <-- <T, C: Convertible, U>(inout lhs: [T : C]?, rhs: U?) throws -> [T : C]? {
public func <-- <T, C: Convertible, U>(inout lhs: [T : C]?, rhs: U?) -> [T : C]? {
guard let rhs = rhs else {
lhs = nil
return lhs
}

if let elements = rhs as? NSDictionary as? [T : AnyObject] {
return try lhs <-- elements
return lhs <-- elements
}

throw ConversionError.UnsupportedType
return nil
}

public func <-- <T, C: Convertible, U>(inout lhs: [T : C], rhs: U?) throws -> [T : C] {
public func <-- <T, C: Convertible, U>(inout lhs: [T : C], rhs: U?) -> [T : C] {
var newValue: [T : C]?
try newValue <-- rhs
newValue <-- rhs
lhs = newValue ?? lhs
return lhs
}

// MARK: - Enum Conversion

public func <-- <T: RawRepresentable, U>(inout lhs: T?, rhs: U?) throws -> T? {
public func <-- <T: RawRepresentable, U>(inout lhs: T?, rhs: U?) -> T? {
var newValue: T?

if let
Expand All @@ -163,9 +177,9 @@ public func <-- <T: RawRepresentable, U>(inout lhs: T?, rhs: U?) throws -> T? {
return lhs
}

public func <-- <T: RawRepresentable, U>(inout lhs: T, rhs: U?) throws -> T {
public func <-- <T: RawRepresentable, U>(inout lhs: T, rhs: U?) -> T {
var newValue: T?
try newValue <-- rhs
newValue <-- rhs
lhs = newValue ?? lhs
return lhs
}
50 changes: 26 additions & 24 deletions JSONHelper/Deserialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Foundation
public protocol Deserializable {

/// TODOC
init(dictionary: [String : AnyObject]) throws
init(dictionary: [String : AnyObject])
}

// MARK: - Helper Methods
Expand All @@ -24,36 +24,37 @@ private func dataStringToObject(dataString: String) -> AnyObject? {

// MARK: - Basic Deserialization

public func <-- <D: Deserializable, T>(inout lhs: D?, rhs: T?) throws -> D? {
public func <-- <D: Deserializable, T>(inout lhs: D?, rhs: T?) -> D? {
let cleanedValue = JSONHelper.convertToNilIfNull(rhs)

if let jsonObject = cleanedValue as? NSDictionary as? [String : AnyObject] {
lhs = try D(dictionary: jsonObject)
lhs = D(dictionary: jsonObject)
} else if let string = cleanedValue as? String {
try lhs <-- dataStringToObject(string)
lhs <-- dataStringToObject(string)
} else {
lhs = nil
}

return lhs
}

public func <-- <D: Deserializable, T>(inout lhs: D, rhs: T?) throws -> D {
public func <-- <D: Deserializable, T>(inout lhs: D, rhs: T?) -> D {
var newValue: D?
try newValue <-- rhs
newValue <-- rhs
lhs = newValue ?? lhs
return lhs
}

// MARK: - Array Deserialization

public func <-- <D: Deserializable, T>(inout lhs: [D]?, rhs: [T]?) throws -> [D]? {
public func <-- <D: Deserializable, T>(inout lhs: [D]?, rhs: [T]?) -> [D]? {
guard let rhs = rhs else { return nil }

lhs = [D]()
for element in rhs {
var convertedElement: D?
try convertedElement <-- element
convertedElement <-- element

if let convertedElement = convertedElement {
lhs?.append(convertedElement)
}
Expand All @@ -62,33 +63,33 @@ public func <-- <D: Deserializable, T>(inout lhs: [D]?, rhs: [T]?) throws -> [D]
return lhs
}

public func <-- <D: Deserializable, T>(inout lhs: [D], rhs: [T]?) throws -> [D] {
public func <-- <D: Deserializable, T>(inout lhs: [D], rhs: [T]?) -> [D] {
var newValue: [D]?
try newValue <-- rhs
newValue <-- rhs
lhs = newValue ?? lhs
return lhs
}

public func <-- <D: Deserializable, T>(inout lhs: [D]?, rhs: T?) throws -> [D]? {
public func <-- <D: Deserializable, T>(inout lhs: [D]?, rhs: T?) -> [D]? {
guard let rhs = rhs else { return nil }

if let elements = rhs as? NSArray as? [AnyObject] {
return try lhs <-- elements
return lhs <-- elements
}

throw ConversionError.UnsupportedType
return nil
}

public func <-- <D: Deserializable, T>(inout lhs: [D], rhs: T?) throws -> [D] {
public func <-- <D: Deserializable, T>(inout lhs: [D], rhs: T?) -> [D] {
var newValue: [D]?
try newValue <-- rhs
newValue <-- rhs
lhs = newValue ?? lhs
return lhs
}

// MARK: - Dictionary Deserialization

public func <-- <T, D: Deserializable, U>(inout lhs: [T : D]?, rhs: [T : U]?) throws -> [T : D]? {
public func <-- <T, D: Deserializable, U>(inout lhs: [T : D]?, rhs: [T : U]?) -> [T : D]? {
guard let rhs = rhs else {
lhs = nil
return lhs
Expand All @@ -97,7 +98,8 @@ public func <-- <T, D: Deserializable, U>(inout lhs: [T : D]?, rhs: [T : U]?) th
lhs = [T : D]()
for (key, value) in rhs {
var convertedValue: D?
try convertedValue <-- value
convertedValue <-- value

if let convertedValue = convertedValue {
lhs?[key] = convertedValue
}
Expand All @@ -106,29 +108,29 @@ public func <-- <T, D: Deserializable, U>(inout lhs: [T : D]?, rhs: [T : U]?) th
return lhs
}

public func <-- <T, D: Deserializable, U>(inout lhs: [T : D], rhs: [T : U]?) throws -> [T : D] {
public func <-- <T, D: Deserializable, U>(inout lhs: [T : D], rhs: [T : U]?) -> [T : D] {
var newValue: [T : D]?
try newValue <-- rhs
newValue <-- rhs
lhs = newValue ?? lhs
return lhs
}

public func <-- <T, D: Deserializable, U>(inout lhs: [T : D]?, rhs: U?) throws -> [T : D]? {
public func <-- <T, D: Deserializable, U>(inout lhs: [T : D]?, rhs: U?) -> [T : D]? {
guard let rhs = rhs else {
lhs = nil
return lhs
}

if let elements = rhs as? NSDictionary as? [T : AnyObject] {
return try lhs <-- elements
return lhs <-- elements
}

throw ConversionError.UnsupportedType
return nil
}

public func <-- <T, D: Deserializable, U>(inout lhs: [T : D], rhs: U?) throws -> [T : D] {
public func <-- <T, D: Deserializable, U>(inout lhs: [T : D], rhs: U?) -> [T : D] {
var newValue: [T : D]?
try newValue <-- rhs
newValue <-- rhs
lhs = newValue ?? lhs
return lhs
}
12 changes: 6 additions & 6 deletions JSONHelperTests/ArrayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ArrayTests: XCTestCase {

func testArrayToConvertibleArray() {
var urls = [NSURL]()
try! urls <-- urlStrings
urls <-- urlStrings

XCTAssertEqual(urls[0].host, urlHosts[0])
XCTAssertEqual(urls[1].host, urlHosts[1])
Expand All @@ -24,7 +24,7 @@ class ArrayTests: XCTestCase {

func testArrayAsAnyToConvertibleArray() {
var urls = [NSURL]()
try! urls <-- (urlStrings as Any)
urls <-- (urlStrings as Any)

XCTAssertEqual(urls[0].host, urlHosts[0])
XCTAssertEqual(urls[1].host, urlHosts[1])
Expand All @@ -38,8 +38,8 @@ class ArrayTests: XCTestCase {

var name: String?

init(dictionary: [String: AnyObject]) throws {
try name <-- dictionary[Item.nameKey]
init(dictionary: [String: AnyObject]) {
name <-- dictionary[Item.nameKey]
}
}

Expand All @@ -50,14 +50,14 @@ class ArrayTests: XCTestCase {

func testArrayToDeserializableArray() {
var items = [Item]()
try! items <-- dictionaries
items <-- dictionaries
XCTAssertEqual(items[0].name, "a")
XCTAssertEqual(items[1].name, "b")
}

func testArrayAsAnyToDeserializableArray() {
var items = [Item]()
try! items <-- (dictionaries as Any)
items <-- (dictionaries as Any)
XCTAssertEqual(items[0].name, "a")
XCTAssertEqual(items[1].name, "b")
}
Expand Down
Loading

0 comments on commit f56cb4c

Please sign in to comment.