Skip to content

Instantly share code, notes, and snippets.

@rkeniger
Forked from turowicz/swift-json-class.md
Last active August 29, 2015 14:05

Revisions

  1. @turowicz turowicz revised this gist Aug 29, 2014. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion swift-json-class.swift
    Original 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.base64Encoding(), forKey: propName)
    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)
    }
  2. @turowicz turowicz revised this gist Jul 7, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion swift-json-usage.swift
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,7 @@ class Animal:Serializable {
    }

    class SerializationTests: XCTestCase {
    func test_email_serialization_works() {
    func test_serialization_works() {
    var john = Person(Name: "John", Surname: "Doe")

    john.Animals.append(Animal(Nickname: "Fluffy", Kind: "Dog"))
  3. @turowicz turowicz revised this gist Jul 7, 2014. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions swift-json-usage.swift
    Original 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,"")
    }
    }
  4. @turowicz turowicz revised this gist Jul 7, 2014. 1 changed file with 12 additions and 20 deletions.
    32 changes: 12 additions & 20 deletions swift-json-class.swift
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,6 @@
    import Foundation

    /*
    /*

    Converts A class to a Dictionary, JSON object or JSON String
    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

    */
    class Serializable:NSObject{

    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 propName : NSString? = NSString(CString: property_getName(propertiesInAClass[i]), encoding: NSUTF8StringEncoding)
    var propType = property_getAttributes(propertiesInAClass[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?
    var jsonData = NSJSONSerialization.dataWithJSONObject(dictionary, options:NSJSONWritingOptions(0), error: &err)

    return jsonData
    return NSJSONSerialization.dataWithJSONObject(dictionary, options:NSJSONWritingOptions(0), error: &err)
    }

    func toJsonString() -> NSString! {
    return NSString(data: self.toJson(), encoding: NSUTF8StringEncoding)
    }

    init(){
    }
    }
    init() { }
    }
  5. @turowicz turowicz revised this gist Jul 7, 2014. 1 changed file with 0 additions and 5 deletions.
    5 changes: 0 additions & 5 deletions blog-post.html
    Original file line number Diff line number Diff line change
    @@ -1,5 +0,0 @@
    <div>



    </div>
  6. @turowicz turowicz revised this gist Jul 7, 2014. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions blog-post.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    <div>



    </div>
  7. @turowicz turowicz revised this gist Jul 7, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion swift-json-class.swift
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@ import Foundation

    /*

    Converts A class to a dictionary, used for serializing dictionaries to JSON
    Converts A class to a Dictionary, JSON object or JSON String

    Supported objects:
    - Serializable derived classes
  8. @turowicz turowicz revised this gist Jul 7, 2014. 1 changed file with 11 additions and 1 deletion.
    12 changes: 11 additions & 1 deletion swift-json-class.swift
    Original 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*/
    /*

    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 {
  9. @turowicz turowicz revised this gist Jul 7, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions swift-json-usage.swift
    Original 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"}]}
    }
    }
  10. @turowicz turowicz renamed this gist Jul 7, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  11. @turowicz turowicz created this gist Jul 7, 2014.
    35 changes: 35 additions & 0 deletions swift-json-usage.swift
    Original 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
    }
    }
    56 changes: 56 additions & 0 deletions swift-json.swift
    Original 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(){
    }
    }