-
Notifications
You must be signed in to change notification settings - Fork 31
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
Added dataFormatter parameter so users can use something other than unix epoch time in their JSON output. #46
base: master
Are you sure you want to change the base?
Changes from all commits
d87c4f3
c6ab3f3
49b5f47
61ea387
6bee0cb
783dca0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
# Podfile | ||
|
||
link_with 'Tests' | ||
|
||
use_frameworks! | ||
pod 'Quick', '~> 0.9.2' | ||
pod 'Nimble', '~> 4.0.0' | ||
|
||
abstract_target 'BasePods' do | ||
|
||
pod 'Quick', '~> 0.9.2' | ||
pod 'Nimble', '~> 4.0.0' | ||
|
||
target 'Tests' do | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,7 @@ Pod::Spec.new do |s| | |
s.source = { :git => "https://github.com/Mailcloud/swift-serializer.git", :tag => "#{s.version}" } | ||
s.authors = {'Mailcloud' => "[email protected]"} | ||
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.source_files = "src/*" | ||
s.requires_arc = true | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,10 +8,10 @@ import Foundation | |
|
||
extension Array where Element: Serializable { | ||
|
||
public func toNSDictionaryArray() -> [NSDictionary] { | ||
public func toNSDictionaryArray(dateFormatter: NSDateFormatter?) -> [NSDictionary] { | ||
var subArray = [NSDictionary]() | ||
for item in self { | ||
subArray.append(item.toDictionary()) | ||
subArray.append(item.toDictionary(dateFormatter)) | ||
} | ||
return subArray | ||
} | ||
|
@@ -21,8 +21,8 @@ extension Array where Element: Serializable { | |
|
||
:returns: The array as JSON, wrapped in NSData. | ||
*/ | ||
public func toJson(prettyPrinted: Bool = false) -> NSData? { | ||
let subArray = self.toNSDictionaryArray() | ||
public func toJson(prettyPrinted: Bool = false, dateFormatter: NSDateFormatter? = nil) -> NSData? { | ||
let subArray = self.toNSDictionaryArray(dateFormatter) | ||
|
||
if NSJSONSerialization.isValidJSONObject(subArray) { | ||
do { | ||
|
@@ -41,8 +41,8 @@ extension Array where Element: Serializable { | |
|
||
:returns: The array as a JSON string. | ||
*/ | ||
public func toJsonString(prettyPrinted: Bool = false) -> String? { | ||
if let jsonData = toJson(prettyPrinted) { | ||
public func toJsonString(prettyPrinted: Bool = false, dateFormatter: NSDateFormatter? = nil) -> String? { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Valid Docs Violation: Documented declarations should be valid. (valid_docs) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, dateFormatter: dateFormatter) { | ||
return NSString(data: jsonData, encoding: NSUTF8StringEncoding) as String? | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,23 +56,27 @@ public class Serializable: NSObject { | |
|
||
- returns: The class as an NSDictionary. | ||
*/ | ||
public func toDictionary() -> NSDictionary { | ||
public func toDictionary(dateFormatter: NSDateFormatter?) -> NSDictionary { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 serializablePropValue = propValue as? Serializable { | ||
setValue(propertiesDictionary, value: serializablePropValue.toDictionary(), forKey: propName) | ||
setValue(propertiesDictionary, value: serializablePropValue.toDictionary(dateFormatter), forKey: propName) | ||
} else if let arrayPropValue = propValue as? [Serializable] { | ||
let subArray = arrayPropValue.toNSDictionaryArray() | ||
let subArray = arrayPropValue.toNSDictionaryArray(dateFormatter) | ||
setValue(propertiesDictionary, value: subArray, 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 { | ||
setValue(propertiesDictionary, | ||
value: dataPropValue.base64EncodedStringWithOptions(.Encoding64CharacterLineLength), forKey: propName) | ||
} else if let datePropValue = propValue as? NSDate { | ||
setValue(propertiesDictionary, value: datePropValue.timeIntervalSince1970, forKey: propName) | ||
if let dateFormatter = dateFormatter { | ||
setValue(propertiesDictionary, value: dateFormatter.stringFromDate(datePropValue), forKey: propName) | ||
} else { | ||
setValue(propertiesDictionary, value: datePropValue.timeIntervalSince1970, forKey: propName) | ||
} | ||
} else { | ||
setValue(propertiesDictionary, value: propValue, forKey: propName) | ||
} | ||
|
@@ -105,8 +109,8 @@ public class Serializable: NSObject { | |
|
||
- returns: The class as JSON, wrapped in NSData. | ||
*/ | ||
public func toJson(prettyPrinted: Bool = false) -> NSData? { | ||
let dictionary = self.toDictionary() | ||
public func toJson(prettyPrinted: Bool = false, dateFormatter: NSDateFormatter? = nil) -> NSData? { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Valid Docs Violation: Documented declarations should be valid. (valid_docs) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Valid Docs Violation: Documented declarations should be valid. (valid_docs) |
||
let dictionary = self.toDictionary(dateFormatter) | ||
|
||
if NSJSONSerialization.isValidJSONObject(dictionary) { | ||
do { | ||
|
@@ -125,8 +129,8 @@ public class Serializable: NSObject { | |
|
||
- returns: The class as a JSON string. | ||
*/ | ||
public func toJsonString(prettyPrinted: Bool = false) -> String? { | ||
if let jsonData = self.toJson(prettyPrinted) { | ||
public func toJsonString(prettyPrinted: Bool = false, dateFormatter: NSDateFormatter? = nil) -> String? { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Valid Docs Violation: Documented declarations should be valid. (valid_docs) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = self.toJson(prettyPrinted, dateFormatter: dateFormatter) { | ||
return NSString(data: jsonData, encoding: NSUTF8StringEncoding) as String? | ||
} | ||
|
||
|
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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)