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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request: Object serialization to JSON string #24

Open
OmarKan opened this issue Feb 2, 2015 · 3 comments
Open

Request: Object serialization to JSON string #24

OmarKan opened this issue Feb 2, 2015 · 3 comments
Assignees

Comments

@OmarKan
Copy link

OmarKan commented Feb 2, 2015

It would be awesome to implement serialization of swift objects to JSON strings 馃憤
Is it possible to do so using this excellent library ?
Thank you...

鈹咺ssue is synchronized with this Asana task

@isair
Copy link
Owner

isair commented Feb 16, 2015

Serialization of any kind is sadly not supported, and this library is deserialization focused right now. Not making any promises but it may be supported in the future after I turn this into a dynamic library and give it a proper rewrite (while maintaining backwards compatibility, of course).

@lancy
Copy link

lancy commented Apr 28, 2015

I'm building something to do the serialization, and it's design to do it in a similar way like deserialization.
it's not finished yet. but if someone need this asap, try my code below:

class IdName: NSObject, Deserializable, Serializable {
    var id: Int?
    var name: String?

    func toJSONDictionary() -> JSONDictionary {
        var data = JSONDictionary()
        id --> data["id"]
        name --> data["name"]
        return data
    }

    required init(data: JSONDictionary) {
        super.init()
        id <-- data["id"]
        name <-- data["name"]
    }
}
protocol Serializable {
    func toJSONDictionary() -> JSONDictionary
}

infix operator --> { associativity right precedence 150 }

func --> <T>(property: T?, inout value: AnyObject?) -> AnyObject? {
    if let int = property as? Int {
        value = int
    } else if let string = property as? String {
        value = string
    } else if let float = property as? Float {
        value = float
    } else if let double = property as? Double {
        value = double
    } else if let instance = property as? Serializable {
        value = instance.toJSONDictionary()
    }
    return value
}

func --> (properties: [Any]?, inout value: AnyObject?) -> AnyObject? {
    if let intArray = properties as? [Int] {
        value = intArray
    } else if let stringArray = properties as? [String] {
        value = stringArray
    } else if let floatArray = properties as? [Float] {
        value = floatArray
    } else if let doubleArray = properties as? [Double] {
        value = doubleArray
    } else if let instances = properties as? [Serializable] {
        value = instances.map { ins in ins.toJSONDictionary() }
    }
    return value
}

@isair I'd like to contribute this project. any suggestion?

@isair
Copy link
Owner

isair commented May 2, 2015

@lancy I was planning something similar. The difference is I am thinking of wrapping all conversions in a function like convertAndAssign(a, to: b) and then use that in defining <-- and --> operators.

@isair isair mentioned this issue Jun 15, 2015
@isair isair added this to the 2.0.0 milestone Jun 15, 2015
@isair isair added ready and removed in progress labels Aug 31, 2015
@isair isair removed this from the 2.0.0 milestone Oct 2, 2015
@isair isair removed the ready label Nov 23, 2015
@isair isair self-assigned this Dec 11, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants