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

Added dataFormatter parameter so users can use something other than unix epoch time in their JSON output. #46

Open
wants to merge 6 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
13 changes: 9 additions & 4 deletions Podfile
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
2 changes: 0 additions & 2 deletions SwiftSerializer.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions src/Array+Serializable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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? {

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)

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(dateFormatter)

if NSJSONSerialization.isValidJSONObject(subArray) {
do {
Expand All @@ -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? {

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)

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, dateFormatter: dateFormatter) {
return NSString(data: jsonData, encoding: NSUTF8StringEncoding) as String?
}

Expand Down
20 changes: 12 additions & 8 deletions src/Serializable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,27 @@ public class Serializable: NSObject {

- returns: The class as an NSDictionary.
*/
public func toDictionary() -> NSDictionary {
public func toDictionary(dateFormatter: NSDateFormatter?) -> 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)
Valid Docs Violation: Documented declarations should be valid. (valid_docs)

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)
Valid Docs Violation: Documented declarations should be valid. (valid_docs)

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)
}
Expand Down Expand Up @@ -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? {

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)

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 dictionary = self.toDictionary(dateFormatter)

if NSJSONSerialization.isValidJSONObject(dictionary) {
do {
Expand All @@ -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? {

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)

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 = self.toJson(prettyPrinted, dateFormatter: dateFormatter) {
return NSString(data: jsonData, encoding: NSUTF8StringEncoding) as String?
}

Expand Down
Loading