Skip to content

Commit

Permalink
A step in the right direction (restructuring & refactoring)
Browse files Browse the repository at this point in the history
  • Loading branch information
isair committed Jan 23, 2016
1 parent 81d43b1 commit 4d5d759
Show file tree
Hide file tree
Showing 20 changed files with 492 additions and 242 deletions.
128 changes: 91 additions & 37 deletions JSONHelper.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,21 @@
BuildableIdentifier = "primary"
BlueprintIdentifier = "D76C52001ACD46B100B49735"
BuildableName = "JSONHelper.framework"
BlueprintName = "JSONHelper_Mac"
BlueprintName = "JSONHelper-Mac"
ReferencedContainer = "container:JSONHelper.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "5FAD076E1A70F2FC00C4D09E"
BuildableName = "JSONHelperTests.xctest"
BlueprintName = "JSONHelperTests"
ReferencedContainer = "container:JSONHelper.xcodeproj">
</BuildableReference>
</BuildActionEntry>
Expand All @@ -26,9 +40,29 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "5FAD076E1A70F2FC00C4D09E"
BuildableName = "JSONHelperTests.xctest"
BlueprintName = "JSONHelperTests"
ReferencedContainer = "container:JSONHelper.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "D76C52001ACD46B100B49735"
BuildableName = "JSONHelper.framework"
BlueprintName = "JSONHelper-Mac"
ReferencedContainer = "container:JSONHelper.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
Expand All @@ -47,7 +81,7 @@
BuildableIdentifier = "primary"
BlueprintIdentifier = "D76C52001ACD46B100B49735"
BuildableName = "JSONHelper.framework"
BlueprintName = "JSONHelper_Mac"
BlueprintName = "JSONHelper-Mac"
ReferencedContainer = "container:JSONHelper.xcodeproj">
</BuildableReference>
</MacroExpansion>
Expand All @@ -65,7 +99,7 @@
BuildableIdentifier = "primary"
BlueprintIdentifier = "D76C52001ACD46B100B49735"
BuildableName = "JSONHelper.framework"
BlueprintName = "JSONHelper_Mac"
BlueprintName = "JSONHelper-Mac"
ReferencedContainer = "container:JSONHelper.xcodeproj">
</BuildableReference>
</MacroExpansion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "5FAD07631A70F2FC00C4D09E"
BuildableName = "JSONHelper.framework"
BlueprintName = "JSONHelper"
BuildableName = "JSONHelper-iOS.framework"
BlueprintName = "JSONHelper-iOS"
ReferencedContainer = "container:JSONHelper.xcodeproj">
</BuildableReference>
</BuildActionEntry>
Expand All @@ -40,7 +40,8 @@
buildConfiguration = "Release"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
<Testables>
<TestableReference
skipped = "NO">
Expand All @@ -57,8 +58,8 @@
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "5FAD07631A70F2FC00C4D09E"
BuildableName = "JSONHelper.framework"
BlueprintName = "JSONHelper"
BuildableName = "JSONHelper-iOS.framework"
BlueprintName = "JSONHelper-iOS"
ReferencedContainer = "container:JSONHelper.xcodeproj">
</BuildableReference>
</MacroExpansion>
Expand All @@ -79,8 +80,8 @@
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "5FAD07631A70F2FC00C4D09E"
BuildableName = "JSONHelper.framework"
BlueprintName = "JSONHelper"
BuildableName = "JSONHelper-iOS.framework"
BlueprintName = "JSONHelper-iOS"
ReferencedContainer = "container:JSONHelper.xcodeproj">
</BuildableReference>
</MacroExpansion>
Expand All @@ -97,8 +98,8 @@
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "5FAD07631A70F2FC00C4D09E"
BuildableName = "JSONHelper.framework"
BlueprintName = "JSONHelper"
BuildableName = "JSONHelper-iOS.framework"
BlueprintName = "JSONHelper-iOS"
ReferencedContainer = "container:JSONHelper.xcodeproj">
</BuildableReference>
</MacroExpansion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,22 @@
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "5F928D951C0D469F00EF50C4"
BuildableName = "JSONHelper_tvOS.framework"
BlueprintName = "JSONHelper_tvOS"
BuildableName = "JSONHelper-tvOS.framework"
BlueprintName = "JSONHelper-tvOS"
ReferencedContainer = "container:JSONHelper.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "5FAD076E1A70F2FC00C4D09E"
BuildableName = "JSONHelperTests.xctest"
BlueprintName = "JSONHelperTests"
ReferencedContainer = "container:JSONHelper.xcodeproj">
</BuildableReference>
</BuildActionEntry>
Expand All @@ -26,9 +40,29 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "5FAD076E1A70F2FC00C4D09E"
BuildableName = "JSONHelperTests.xctest"
BlueprintName = "JSONHelperTests"
ReferencedContainer = "container:JSONHelper.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "5F928D951C0D469F00EF50C4"
BuildableName = "JSONHelper-tvOS.framework"
BlueprintName = "JSONHelper-tvOS"
ReferencedContainer = "container:JSONHelper.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
Expand All @@ -46,8 +80,8 @@
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "5F928D951C0D469F00EF50C4"
BuildableName = "JSONHelper_tvOS.framework"
BlueprintName = "JSONHelper_tvOS"
BuildableName = "JSONHelper-tvOS.framework"
BlueprintName = "JSONHelper-tvOS"
ReferencedContainer = "container:JSONHelper.xcodeproj">
</BuildableReference>
</MacroExpansion>
Expand All @@ -64,8 +98,8 @@
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "5F928D951C0D469F00EF50C4"
BuildableName = "JSONHelper_tvOS.framework"
BlueprintName = "JSONHelper_tvOS"
BuildableName = "JSONHelper-tvOS.framework"
BlueprintName = "JSONHelper-tvOS"
ReferencedContainer = "container:JSONHelper.xcodeproj">
</BuildableReference>
</MacroExpansion>
Expand Down
106 changes: 15 additions & 91 deletions JSONHelper/JSONHelper.swift → JSONHelper/Conversion.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//
// JSONHelper.swift
// Conversion.swift
//
// Created by Baris Sencan on 28/08/2014.
// Copyright 2014 Baris Sencan
// Created by Barış Şencan on 01/22/2016.
// Copyright © 2016 Barış Şencan
//
// Distributed under the permissive zlib license
// Get the latest version from here:
Expand Down Expand Up @@ -30,42 +30,31 @@

import Foundation

/// An object that can attempt to convert values of unknown types to its own type.
public protocol Convertible {
/// Operator for use in right hand side to left hand side conversion.
infix operator <-- { associativity right precedence 150 }

/// TODOC
static func convertFromValue(value: Any?) -> Self?
}
/// Operator for use in left hand side to right hand side conversion.
infix operator --> { associativity left precedence 150 }

/// Filters out values of type NSNull.
///
/// :param: value Value to check.
///
/// :returns: nil if value is of type NSNull, else the value is returned as-is.
public func convertToNilIfNull(value: Any?) -> Any? {
public func convertToNilIfNull<T>(value: T?) -> T? {
if value is NSNull {
return nil
}
return value
}

/// TODOC
public protocol Deserializable {

/// TODOC
init(jsonObject: JSONObject)
}

/// TODOC
public protocol Serializable {
/// An object that can attempt to convert values of unknown types to its own type.
public protocol Convertible {

/// TODOC
func toJSONValue() -> JSONValue
static func convertFromValue<T>(value: T?) -> Self?
}

/// Operator for use in right hand side to left hand side conversion and deserialization.
infix operator <-- { associativity right precedence 150 }

public func <-- <C: Convertible, T>(inout lhs: C?, rhs: T?) -> C? {
lhs = C.convertFromValue(convertToNilIfNull(rhs))
return lhs
Expand Down Expand Up @@ -130,83 +119,18 @@ public func <-- <C: Convertible, T>(inout lhs: [String: C], rhs: T?) -> [String:
return lhs
}

public func <-- <D: Deserializable, T>(inout lhs: D?, rhs: T?) -> D? {
let cleanedValue = convertToNilIfNull(rhs)
if let jsonObject = cleanedValue as? JSONObject {
lhs = D(jsonObject: jsonObject)
} else if let string = cleanedValue as? String {
lhs <-- dataStringToObject(string)
} else {
lhs = nil
}
return lhs
}

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

public func <-- <D: Deserializable, T>(inout lhs: [D]?, rhs: T?) -> [D]? {
if let array = rhs as? [AnyObject] {
lhs = [D]()
for element in array {
var convertedElement: D?
convertedElement <-- element
if let convertedElement = convertedElement {
lhs?.append(convertedElement)
}
}
} else if let string = convertToNilIfNull(rhs) as? String {
lhs <-- dataStringToObject(string)
} else {
lhs = nil
}
return lhs
}

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

/// Operator for use in left hand side to right hand side conversion and serialization.
infix operator --> { associativity left precedence 150 }

public func --> <C: Convertible>(lhs: Any?, inout rhs: C?) -> C? {
public func --> <C: Convertible, T>(lhs: T?, inout rhs: C?) -> C? {
return rhs <-- lhs
}

public func --> <C: Convertible>(lhs: Any?, inout rhs: C) -> C {
public func --> <C: Convertible, T>(lhs: T?, inout rhs: C) -> C {
return rhs <-- lhs
}

public func --> <C: Convertible>(lhs: AnyObject?, inout rhs: [C]?) -> [C]? {
public func --> <C: Convertible, T>(lhs: T?, inout rhs: [C]?) -> [C]? {
return rhs <-- lhs
}

public func --> <C: Convertible>(lhs: AnyObject?, inout rhs: [C]) -> [C] {
public func --> <C: Convertible, T>(lhs: T?, inout rhs: [C]) -> [C] {
return rhs <-- lhs
}

// TODO: Serialization


// MARK: - Helper methods

private func dataStringToObject(dataString: String) -> AnyObject? {
guard let data: NSData = dataString.dataUsingEncoding(NSUTF8StringEncoding) else { return nil }
var jsonObject: AnyObject?
do {
jsonObject = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(rawValue: 0))
} catch {}
return jsonObject
}
Loading

0 comments on commit 4d5d759

Please sign in to comment.