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
toJSON() improvement #14
Comments
What do you think about calling super and then making the change for one property? Debatably it's less efficient but Swift doesn't have optional protocol methods so we would have to implement |
true, there is no optional methods, but you can define a default implementation via protocol extension, achieving the same result. What do you think? |
Swift sort of has optional protocol methods by creating default implementations in an extension. For this example, you could change the protocol to: public protocol JSONEncodable {
func toJSON() throws -> AnyObject
func mapPropertyName(name: String) -> String?
} And add a default implementation: public extension JSONEncodable {
func mapPropertyName(name: String) -> String? {
return nil
}
} Then in Although to be honest, I'm not a fan of writing writing logic in strings. I much prefer creating to separate models, a JSON model and a domain model, where the actual mapping is done in compile-time checked Swift code. So I don't think I would ever use "mapPropertyName", but it is something you could provide using extensions on protocols. |
Most of the times the default implementation of toJSON() is great, so you don't need to implement that function. Anyway if you want to change the name of just one property, you need to implement toJSON() for all the properties. It would be nice to add a method that it will ask for mapping some properties name, otherwise will use the default behavior.
example:
My object has a
oneProperty
property. And the server wants aone_property
property. I'd like a method like:The text was updated successfully, but these errors were encountered: