Skip to content
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
12 changes: 6 additions & 6 deletions Podfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Podfile

link_with 'Tests'

use_frameworks!
pod 'Quick', '~> 0.9.2'
pod 'Nimble', '~> 4.0.0'
platform :ios, '9.0'

target 'Tests' do
pod 'Quick', '~> 0.10.0'
pod 'Nimble', '~> 5.0.0'
end
16 changes: 9 additions & 7 deletions Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
PODS:
- Nimble (4.0.1)
- Quick (0.9.2)
- Nimble (5.0.0)
- Quick (0.10.0)

DEPENDENCIES:
- Nimble (~> 4.0.0)
- Quick (~> 0.9.2)
- Nimble (~> 5.0.0)
- Quick (~> 0.10.0)

SPEC CHECKSUMS:
Nimble: 0f3c8b8b084cda391209c3c5efbb48bedeeb920a
Quick: 18d057bc66451eedd5d1c8dc99ba2a5db6e60226
Nimble: 56fc9f5020effa2206de22c3dd910f4fb011b92f
Quick: 5d290df1c69d5ee2f0729956dcf0fd9a30447eaa

COCOAPODS: 0.39.0
PODFILE CHECKSUM: 03bad95c0634d3035d5f61527505d5b2aa02895f

COCOAPODS: 1.1.1
11 changes: 7 additions & 4 deletions SwiftSerializer.podspec
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
Pod::Spec.new do |s|
s.name = "SwiftSerializer"
s.version = "0.6"
s.version = "0.7"
s.summary = "Swift Strong Type Object Serialization to JSON"
s.homepage = "https://github.com/Mailcloud/swift-serializer"
s.license = { :type => "MIT", :file => "LICENSE"}
s.source = { :git => "https://github.com/Mailcloud/swift-serializer.git", :tag => "#{s.version}" }
s.authors = {'Mailcloud' => "contact@mailcloud.com"}
s.social_media_url = "https://twitter.com/mailcloud"
s.ios.platform = :ios, '8.0'
s.ios.deployment_target = "8.0"
s.osx.platform = :osx, '10.9'
s.osx.deployment_target = "10.9"
s.ios.deployment_target = "8.0"
s.watchos.deployment_target = "2.0"
s.tvos.deployment_target = "9.0"
s.source_files = "src/*"
s.requires_arc = true
s.pod_target_xcconfig = {
'SWIFT_VERSION' => '3.0',
}
end
16 changes: 7 additions & 9 deletions src/Array+Serializable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,17 @@ extension Array where Element: Serializable {

:returns: The array as JSON, wrapped in NSData.
*/
public func toJson(prettyPrinted: Bool = false) -> NSData? {
public 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.

Valid Docs Violation: Documented declarations should be valid. (valid_docs)

let subArray = self.toNSDictionaryArray()
if NSJSONSerialization.isValidJSONObject(subArray) {

if JSONSerialization.isValidJSONObject(subArray) {
do {
let json = try NSJSONSerialization.dataWithJSONObject(subArray, options: (prettyPrinted ? .PrettyPrinted: NSJSONWritingOptions()))
return json
let options = prettyPrinted ? .prettyPrinted: JSONSerialization.WritingOptions()
return try JSONSerialization.data(withJSONObject: subArray, options: options)
} catch let error as NSError {
print("ERROR: Unable to serialize json, error: \(error)")
}
}

return nil
}

Expand All @@ -41,11 +40,10 @@ 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? {

Choose a reason for hiding this comment

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

Valid Docs Violation: Documented declarations should be valid. (valid_docs)

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
}
}
105 changes: 54 additions & 51 deletions src/Serializable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,41 @@ 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 sortedDictionary = [String: AnyObject]()

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

override func keyEnumerator() -> NSEnumerator {
let sortedKeys: NSArray = dictionary.keys.sort()
let sortedKeys: NSArray = sortedDictionary.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) {
sortedDictionary[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 sortedDictionary[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 @@ -60,43 +59,48 @@ public class Serializable: NSObject {
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 {
let propValue = self.unwrap(propValue) as AnyObject
if 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)
setValue(propertiesDictionary, value: subArray as AnyObject?, forKey: propName)
} else if propValue is Int || propValue is Double || propValue is Float || propValue is Bool {
setValue(propertiesDictionary, value: propValue, forKey: propName)
} else if let dataPropValue = propValue as? NSData {
} else if let dataPropValue = propValue as? Data {
setValue(propertiesDictionary,
value: dataPropValue.base64EncodedStringWithOptions(.Encoding64CharacterLineLength), forKey: propName)
} else if let datePropValue = propValue as? NSDate {
setValue(propertiesDictionary, value: datePropValue.timeIntervalSince1970, forKey: propName)
value: dataPropValue.base64EncodedString(options: .lineLength64Characters) as AnyObject?, forKey: propName)
} else if let datePropValue = propValue as? Date {
setValue(propertiesDictionary, value: datePropValue.timeIntervalSince1970 as AnyObject?, forKey: propName)
} else {
setValue(propertiesDictionary, value: propValue, forKey: propName)
switch propValue {
case let rawEnum as RawRepresentable:
setValue(propertiesDictionary, value: "\(rawEnum)" as AnyObject?, forKey: propName)
default:
setValue(propertiesDictionary, value: propValue, forKey: propName)
}
}
} else if let propValue: Int8 = propValue as? Int8 {
setValue(propertiesDictionary, value: NSNumber(char: propValue), forKey: propName!)
setValue(propertiesDictionary, value: NSNumber(value: propValue as Int8), forKey: propName!)
} else if let propValue: Int16 = propValue as? Int16 {
setValue(propertiesDictionary, value: NSNumber(short: propValue), forKey: propName!)
setValue(propertiesDictionary, value: NSNumber(value: propValue as Int16), forKey: propName!)
} else if let propValue: Int32 = propValue as? Int32 {
setValue(propertiesDictionary, value: NSNumber(int: propValue), forKey: propName!)
setValue(propertiesDictionary, value: NSNumber(value: propValue as Int32), forKey: propName!)
} else if let propValue: Int64 = propValue as? Int64 {
setValue(propertiesDictionary, value: NSNumber(longLong: propValue), forKey: propName!)
setValue(propertiesDictionary, value: NSNumber(value: propValue as Int64), forKey: propName!)
} else if let propValue: UInt8 = propValue as? UInt8 {
setValue(propertiesDictionary, value: NSNumber(unsignedChar: propValue), forKey: propName!)
setValue(propertiesDictionary, value: NSNumber(value: propValue as UInt8), forKey: propName!)
} else if let propValue: UInt16 = propValue as? UInt16 {
setValue(propertiesDictionary, value: NSNumber(unsignedShort: propValue), forKey: propName!)
setValue(propertiesDictionary, value: NSNumber(value: propValue as UInt16), forKey: propName!)
} else if let propValue: UInt32 = propValue as? UInt32 {
setValue(propertiesDictionary, value: NSNumber(unsignedInt: propValue), forKey: propName!)
setValue(propertiesDictionary, value: NSNumber(value: propValue as UInt32), forKey: propName!)
} else if let propValue: UInt64 = propValue as? UInt64 {
setValue(propertiesDictionary, value: NSNumber(unsignedLongLong: propValue), forKey: propName!)
setValue(propertiesDictionary, value: NSNumber(value: propValue as UInt64), forKey: propName!)
} else if isEnum(propValue) {
setValue(propertiesDictionary, value: "\(propValue)", forKey: propName!)
setValue(propertiesDictionary, value: "\(propValue)" as AnyObject?, forKey: propName!)
}
}

return propertiesDictionary
}

Expand All @@ -105,18 +109,16 @@ public class Serializable: NSObject {

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

if NSJSONSerialization.isValidJSONObject(dictionary) {
if JSONSerialization.isValidJSONObject(dictionary) {
do {
let json = try NSJSONSerialization.dataWithJSONObject(dictionary, options: (prettyPrinted ? .PrettyPrinted: NSJSONWritingOptions()))
return json
let options = prettyPrinted ? .prettyPrinted: JSONSerialization.WritingOptions()
return try JSONSerialization.data(withJSONObject: dictionary, options: options)
} catch let error as NSError {
print("ERROR: Unable to serialize json, error: \(error)")
}
}

return nil
}

Expand All @@ -125,11 +127,11 @@ public class Serializable: NSObject {

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

return nil
}

Expand All @@ -139,18 +141,19 @@ 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
}

if mi.children.count == 0 { return nil }
let (_, some) = mi.children.first!
return some
}

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

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