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

Changed for Swift 3 compliance #50

Open
wants to merge 2 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/Array+Serializable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@ Serializes an array to JSON making use of the Serializable class
import Foundation

extension Array where Element: Serializable {
public func toNSDictionaryArray() -> [NSDictionary] {
var subArray = [NSDictionary]()
for item in self {
subArray.append(item.toDictionary())
}
return subArray
}
public func toNSDictionaryArray() -> [NSDictionary] {
var subArray = [NSDictionary]()
for item in self {
subArray.append(item.toDictionary())
}
return subArray
}
/**
Converts the array to JSON.

:returns: The array as JSON, wrapped in NSData.
*/
public func toJson(prettyPrinted: Bool = false) -> NSData? {
public func toJson(_ prettyPrinted: Bool = false) -> Data? {
let subArray = self.toNSDictionaryArray()

if NSJSONSerialization.isValidJSONObject(subArray) {
if JSONSerialization.isValidJSONObject(subArray) {
do {
let json = try NSJSONSerialization.dataWithJSONObject(subArray, options: (prettyPrinted ? .PrettyPrinted: NSJSONWritingOptions()))
let json = try JSONSerialization.data(withJSONObject: subArray, options: (prettyPrinted ? .prettyPrinted : JSONSerialization.WritingOptions()))
return json
} catch let error as NSError {
print("ERROR: Unable to serialize json, error: \(error)")
Expand All @@ -41,9 +41,9 @@ extension Array where Element: Serializable {

:returns: The array as a JSON string.
*/
public func toJsonString(prettyPrinted: Bool = false) -> String? {
public func toJsonString(_ prettyPrinted : Bool = false) -> String? {
if let jsonData = toJson(prettyPrinted) {
return NSString(data: jsonData, encoding: NSUTF8StringEncoding) as String?
return NSString(data: jsonData, encoding: String.Encoding.utf8.rawValue) as String?
}

return nil
Expand Down
109 changes: 60 additions & 49 deletions src/Serializable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,42 @@ Supported objects:

import Foundation

public class Serializable: NSObject {
private class SortedDictionary: NSMutableDictionary {
var dictionary = [String: AnyObject]()
open class Serializable: NSObject {
fileprivate class SortedDictionary : NSMutableDictionary {
var _dictionary = [String: AnyObject]()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable Name Violation: Variable name should only contain alphanumeric characters: '_dictionary' (variable_name)


override var count: Int {
return dictionary.count
override var count : Int {
return _dictionary.count
}

override func keyEnumerator() -> NSEnumerator {
let sortedKeys: NSArray = dictionary.keys.sort()
let sortedKeys : NSArray = _dictionary.keys.sorted() as NSArray
return sortedKeys.objectEnumerator()
}

override func setValue(value: AnyObject?, forKey key: String) {
dictionary[key] = value
override func setValue(_ value: Any?, forKey key: String) {
_dictionary[key] = value as AnyObject?
}

override func objectForKey(aKey: AnyObject) -> AnyObject? {
override func object(forKey aKey: Any) -> Any? {
if let key = aKey as? String {
return dictionary[key]
return _dictionary[key]
}

return nil
}
}


public func formatKey(key: String) -> String {
open func formatKey(_ key: String) -> String {
return key
}

public func formatValue(value: AnyObject?, forKey: String) -> AnyObject? {
open func formatValue(_ value: AnyObject?, forKey: String) -> AnyObject? {
return value
}

func setValue(dictionary: NSDictionary, value: AnyObject?, forKey: String) {
func setValue(_ dictionary: NSDictionary, value: AnyObject?, forKey: String) {
dictionary.setValue(formatValue(value, forKey: forKey), forKey: formatKey(forKey))
}

Expand All @@ -56,44 +56,54 @@ public class Serializable: NSObject {

- returns: The class as an NSDictionary.
*/
public func toDictionary() -> NSDictionary {
open func toDictionary() -> NSDictionary {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cyclomatic Complexity Violation: Function should have complexity 10 or less: currently complexity equals 17 (cyclomatic_complexity)

let propertiesDictionary = SortedDictionary()
let mirror = Mirror(reflecting: self)
for (propName, propValue) in mirror.children {
if let propValue: AnyObject = self.unwrap(propValue) as? AnyObject, propName = propName {
if let propValue: AnyObject = self.unwrap(propValue) as AnyObject?, let propName = propName {
if let serializablePropValue = propValue as? Serializable {
setValue(propertiesDictionary, value: serializablePropValue.toDictionary(), forKey: propName)
} else if let arrayPropValue = propValue as? [Serializable] {
let subArray = arrayPropValue.toNSDictionaryArray()
setValue(propertiesDictionary, value: subArray, forKey: propName)
} else if propValue is Int || propValue is Double || propValue is Float || propValue is Bool {
var subArray = [NSDictionary]()
for item in arrayPropValue {
subArray.append(item.toDictionary())
}
setValue(propertiesDictionary, value: subArray as AnyObject?, forKey: propName)
} else if propValue is Int || propValue is Double || propValue is Float {
setValue(propertiesDictionary, value: propValue, forKey: propName)
} else if let dataPropValue = propValue as? NSData {
setValue(propertiesDictionary,
value: dataPropValue.base64EncodedStringWithOptions(.Encoding64CharacterLineLength), forKey: propName)
} else if let datePropValue = propValue as? NSDate {
setValue(propertiesDictionary, value: datePropValue.timeIntervalSince1970, forKey: propName)
} else if let dataPropValue = propValue as? Data {
setValue(propertiesDictionary, value: dataPropValue.base64EncodedString(options: .lineLength64Characters) as AnyObject?, forKey: propName)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line Length Violation: Line should be 150 characters or less: currently 158 characters (line_length)

} else if let datePropValue = propValue as? Date {
setValue(propertiesDictionary, value: datePropValue.timeIntervalSince1970 as AnyObject?, forKey: propName)
} else if let boolPropValue = propValue as? Bool {
setValue(propertiesDictionary, value: boolPropValue as AnyObject?, forKey: propName)
} else {
setValue(propertiesDictionary, value: propValue, forKey: propName)
}
} else if let propValue: Int8 = propValue as? Int8 {
setValue(propertiesDictionary, value: NSNumber(char: propValue), forKey: propName!)
} else if let propValue: Int16 = propValue as? Int16 {
setValue(propertiesDictionary, value: NSNumber(short: propValue), forKey: propName!)
} else if let propValue: Int32 = propValue as? Int32 {
setValue(propertiesDictionary, value: NSNumber(int: propValue), forKey: propName!)
} else if let propValue: Int64 = propValue as? Int64 {
setValue(propertiesDictionary, value: NSNumber(longLong: propValue), forKey: propName!)
} else if let propValue: UInt8 = propValue as? UInt8 {
setValue(propertiesDictionary, value: NSNumber(unsignedChar: propValue), forKey: propName!)
} else if let propValue: UInt16 = propValue as? UInt16 {
setValue(propertiesDictionary, value: NSNumber(unsignedShort: propValue), forKey: propName!)
} else if let propValue: UInt32 = propValue as? UInt32 {
setValue(propertiesDictionary, value: NSNumber(unsignedInt: propValue), forKey: propName!)
} else if let propValue: UInt64 = propValue as? UInt64 {
setValue(propertiesDictionary, value: NSNumber(unsignedLongLong: propValue), forKey: propName!)
} else if isEnum(propValue) {
setValue(propertiesDictionary, value: "\(propValue)", forKey: propName!)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Statement Position Violation: Else and catch should be on the same line, one space after the previous declaration. (statement_position)

else if let propValue:Int8 = propValue as? Int8 {
setValue(propertiesDictionary, value: NSNumber(value: propValue as Int8), forKey: propName!)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Statement Position Violation: Else and catch should be on the same line, one space after the previous declaration. (statement_position)

else if let propValue:Int16 = propValue as? Int16 {
setValue(propertiesDictionary, value: NSNumber(value: propValue as Int16), forKey: propName!)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Statement Position Violation: Else and catch should be on the same line, one space after the previous declaration. (statement_position)

else if let propValue:Int32 = propValue as? Int32 {
setValue(propertiesDictionary, value: NSNumber(value: propValue as Int32), forKey: propName!)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Statement Position Violation: Else and catch should be on the same line, one space after the previous declaration. (statement_position)

else if let propValue:Int64 = propValue as? Int64 {
setValue(propertiesDictionary, value: NSNumber(value: propValue as Int64), forKey: propName!)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Statement Position Violation: Else and catch should be on the same line, one space after the previous declaration. (statement_position)

else if let propValue:UInt8 = propValue as? UInt8 {
setValue(propertiesDictionary, value: NSNumber(value: propValue as UInt8), forKey: propName!)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Statement Position Violation: Else and catch should be on the same line, one space after the previous declaration. (statement_position)

else if let propValue:UInt16 = propValue as? UInt16 {
setValue(propertiesDictionary, value: NSNumber(value: propValue as UInt16), forKey: propName!)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Statement Position Violation: Else and catch should be on the same line, one space after the previous declaration. (statement_position)

else if let propValue:UInt32 = propValue as? UInt32 {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Colon Violation: Colons should be next to the identifier when specifying a type. (colon)

setValue(propertiesDictionary, value: NSNumber(value: propValue as UInt32), forKey: propName!)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Statement Position Violation: Else and catch should be on the same line, one space after the previous declaration. (statement_position)

else if let propValue:UInt64 = propValue as? UInt64 {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Colon Violation: Colons should be next to the identifier when specifying a type. (colon)

setValue(propertiesDictionary, value: NSNumber(value: propValue as UInt64), forKey: propName!)
}
}

Expand All @@ -105,12 +115,12 @@ public class Serializable: NSObject {

- returns: The class as JSON, wrapped in NSData.
*/
public func toJson(prettyPrinted: Bool = false) -> NSData? {
open func toJson(_ prettyPrinted : Bool = false) -> Data? {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Colon Violation: Colons should be next to the identifier when specifying a type. (colon)

let dictionary = self.toDictionary()

if NSJSONSerialization.isValidJSONObject(dictionary) {
if JSONSerialization.isValidJSONObject(dictionary) {
do {
let json = try NSJSONSerialization.dataWithJSONObject(dictionary, options: (prettyPrinted ? .PrettyPrinted: NSJSONWritingOptions()))
let json = try JSONSerialization.data(withJSONObject: dictionary, options: (prettyPrinted ? .prettyPrinted : JSONSerialization.WritingOptions()))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line Length Violation: Line should be 150 characters or less: currently 161 characters (line_length)

return json
} catch let error as NSError {
print("ERROR: Unable to serialize json, error: \(error)")
Expand All @@ -125,9 +135,9 @@ public class Serializable: NSObject {

- returns: The class as a JSON string.
*/
public func toJsonString(prettyPrinted: Bool = false) -> String? {
open func toJsonString(_ prettyPrinted : Bool = false) -> String? {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Colon Violation: Colons should be next to the identifier when specifying a type. (colon)

if let jsonData = self.toJson(prettyPrinted) {
return NSString(data: jsonData, encoding: NSUTF8StringEncoding) as String?
return NSString(data: jsonData, encoding: String.Encoding.utf8.rawValue) as String?
}

return nil
Expand All @@ -139,9 +149,10 @@ public class Serializable: NSObject {

- returns: The unwrapped object.
*/
func unwrap(any: Any) -> Any? {
func unwrap(_ any:Any) -> Any? {

let mi = Mirror(reflecting: any)
if mi.displayStyle != .Optional {
if mi.displayStyle != .optional {
return any
}

Expand All @@ -151,6 +162,6 @@ public class Serializable: NSObject {
}

func isEnum(any: Any) -> Bool {
return Mirror(reflecting: any).displayStyle == .Enum
return Mirror(reflecting: any).displayStyle == .enum
}
}
Loading