Giter Club home page Giter Club logo

Comments (3)

matthewcheok avatar matthewcheok commented on September 27, 2024

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 mapPropertyName if we don't need it?

from jsoncodable.

lucatorella avatar lucatorella commented on September 27, 2024

true, there is no optional methods, but you can define a default implementation via protocol extension, achieving the same result. What do you think?

from jsoncodable.

tomlokhorst avatar tomlokhorst commented on September 27, 2024

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 toJSON, you write: let label = mapPropertyName(defaultLabel) ?? defaultLabel
Users can optionally override the default implementation of mapPropertyName.

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.

from jsoncodable.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.