Revisions
-
turowicz revised this gist
Aug 29, 2014 . 1 changed file with 9 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -35,7 +35,15 @@ class Serializable : NSObject{ } propertiesDictionary.setValue(subArray, forKey: propName) } else if propValue is NSData { propertiesDictionary.setValue((propValue as NSData).base64EncodedStringWithOptions(nil), forKey: propName) } else if propValue is Bool { propertiesDictionary.setValue((propValue as Bool).boolValue, forKey: propName) } else if propValue is NSDate { var date = propValue as NSDate let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "Z" var dateString = NSString(format: "/Date(%.0f000%@)/", date.timeIntervalSince1970, dateFormatter.stringFromDate(date)) propertiesDictionary.setValue(dateString, forKey: propName) } else { propertiesDictionary.setValue(propValue, forKey: propName) } -
turowicz revised this gist
Jul 7, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -23,7 +23,7 @@ class Animal:Serializable { } class SerializationTests: XCTestCase { func test_serialization_works() { var john = Person(Name: "John", Surname: "Doe") john.Animals.append(Animal(Nickname: "Fluffy", Kind: "Dog")) -
turowicz revised this gist
Jul 7, 2014 . 1 changed file with 4 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -33,5 +33,9 @@ class SerializationTests: XCTestCase { println(john.toJsonString()) //will give the exact string in JSON //{"Surname":"Doe","Name":"John","Animals":[{"Kind":"Dog","Nickname":"Fluffy"},{"Kind":"Cat","Nickname":"Purry"}]} var expected = "{\"Surname\":\"Doe\",\"Name\":\"John\",\"Animals\":[{\"Kind\":\"Dog\",\"Nickname\":\"Fluffy\"},{\"Kind\":\"Cat\",\"Nickname\":\"Purry\"}]}"; XCTAssertEqual(john.toJsonString(), expected,"") } } -
turowicz revised this gist
Jul 7, 2014 . 1 changed file with 12 additions and 20 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,6 @@ /* Converts A class to a dictionary, used for serializing dictionaries to JSON Supported objects: - Serializable derived classes @@ -11,32 +9,30 @@ import Foundation - String, Numeric, and all other NSJSONSerialization supported objects */ import Foundation class Serializable : NSObject{ func toDictionary() -> NSDictionary { var aClass : AnyClass? = self.dynamicType var propertiesCount : CUnsignedInt = 0 let propertiesInAClass : UnsafePointer<objc_property_t> = class_copyPropertyList(aClass, &propertiesCount) var propertiesDictionary : NSMutableDictionary = NSMutableDictionary() for var i = 0; i < Int(propertiesCount); i++ { var property = propertiesInAClass[i] var propName = NSString(CString: property_getName(property), encoding: NSUTF8StringEncoding) var propType = property_getAttributes(property) var propValue : AnyObject! = self.valueForKey(propName); if propValue is Serializable { propertiesDictionary.setValue((propValue as Serializable).toDictionary(), forKey: propName) } else if propValue is Array<Serializable> { var subArray = Array<NSDictionary>() for item in (propValue as Array<Serializable>) { subArray.append(item.toDictionary()) } propertiesDictionary.setValue(subArray, forKey: propName) } else if propValue is NSData { propertiesDictionary.setValue(propValue.base64Encoding(), forKey: propName) @@ -50,17 +46,13 @@ class Serializable:NSObject{ func toJson() -> NSData! { var dictionary = self.toDictionary() var err: NSError? return NSJSONSerialization.dataWithJSONObject(dictionary, options:NSJSONWritingOptions(0), error: &err) } func toJsonString() -> NSString! { return NSString(data: self.toJson(), encoding: NSUTF8StringEncoding) } init() { } } -
turowicz revised this gist
Jul 7, 2014 . 1 changed file with 0 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +0,0 @@ -
turowicz revised this gist
Jul 7, 2014 . 1 changed file with 5 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,5 @@ <div> </div> -
turowicz revised this gist
Jul 7, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ import Foundation /* Converts A class to a Dictionary, JSON object or JSON String Supported objects: - Serializable derived classes -
turowicz revised this gist
Jul 7, 2014 . 1 changed file with 11 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,16 @@ import Foundation /* Converts A class to a dictionary, used for serializing dictionaries to JSON Supported objects: - Serializable derived classes - Arrays of Serializable - NSData - String, Numeric, and all other NSJSONSerialization supported objects */ class Serializable:NSObject{ func toDictionary() -> NSDictionary { -
turowicz revised this gist
Jul 7, 2014 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -31,5 +31,7 @@ class SerializationTests: XCTestCase { println(john.toJson()) //will give binary data to include in HTTP Body println(john.toJsonString()) //will give the exact string in JSON //{"Surname":"Doe","Name":"John","Animals":[{"Kind":"Dog","Nickname":"Fluffy"},{"Kind":"Cat","Nickname":"Purry"}]} } } -
turowicz renamed this gist
Jul 7, 2014 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
turowicz created this gist
Jul 7, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,35 @@ import XCTest class Person:Serializable{ var Name : String var Surname : String var Animals : Array<Animal> init(Name:String, Surname:String) { self.Name = Name self.Surname = Surname self.Animals = Array<Animal>() } } class Animal:Serializable { var Nickname : String var Kind : String init(Nickname : String, Kind : String) { self.Nickname = Nickname self.Kind = Kind } } class SerializationTests: XCTestCase { func test_email_serialization_works() { var john = Person(Name: "John", Surname: "Doe") john.Animals.append(Animal(Nickname: "Fluffy", Kind: "Dog")) john.Animals.append(Animal(Nickname: "Purry", Kind: "Cat")) println(john.toJson()) //will give binary data to include in HTTP Body println(john.toJsonString()) //will give the exact string in JSON } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,56 @@ import Foundation /*Converts A class to a dictionary, used for serializing dictionaries to JSON*/ class Serializable:NSObject{ func toDictionary() -> NSDictionary { var aClass : AnyClass? = self.dynamicType var propertiesCount : CUnsignedInt = 0 let propertiesInAClass : UnsafePointer<objc_property_t> = class_copyPropertyList(aClass, &propertiesCount) var propertiesDictionary : NSMutableDictionary = NSMutableDictionary() for var i = 0; i < Int(propertiesCount); i++ { var propName : NSString? = NSString(CString: property_getName(propertiesInAClass[i]), encoding: NSUTF8StringEncoding) var propType = property_getAttributes(propertiesInAClass[i]) var propValue : AnyObject! = self.valueForKey(propName); if propValue is Serializable { propertiesDictionary.setValue((propValue as Serializable).toDictionary(), forKey: propName) } else if propValue is Array<Serializable> { var subArray = Array<NSDictionary>() for item in (propValue as Array<Serializable>) { subArray.append(item.toDictionary()) } propertiesDictionary.setValue(subArray, forKey: propName) } else if propValue is NSData { propertiesDictionary.setValue(propValue.base64Encoding(), forKey: propName) } else { propertiesDictionary.setValue(propValue, forKey: propName) } } return propertiesDictionary } func toJson() -> NSData! { var dictionary = self.toDictionary() var err: NSError? var jsonData = NSJSONSerialization.dataWithJSONObject(dictionary, options:NSJSONWritingOptions(0), error: &err) return jsonData } func toJsonString() -> NSString! { return NSString(data: self.toJson(), encoding: NSUTF8StringEncoding) } init(){ } }