Comments (3)
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.
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.
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)
- new release please? :) HOT 8
- Empty array instead of MissingTypeError HOT 1
- Empty in an JSONEncodable object result in missing key in toJSON
- JSONEncoder.encode(_:,key:) fails to compile with value argument cast to JSONEncodable
- JSONString: line 19 tries casting to AnyObject while return type of the toJSON method is Any HOT 2
- Use proper SemVer in release version tags HOT 2
- Cocoapods repo update? HOT 3
- Working with keys that have '.' (dot) in it HOT 1
- Missing travis. HOT 1
- `JSONDecodable` with static `parse` method? HOT 4
- Interface for extending `JSONDecoder`?
- Issues with Arrays HOT 1
- Swift Package Manager Support HOT 1
- Warning when building with Carthage HOT 1
- Problem JSON encoding and decoding in Swift with JSONCodable HOT 3
- Unable to call toJSON on multi-dimensional arrays
- JSONDecoder is ambiguous for type lookup HOT 2
- Deprecated Method
- Is it possible to decode an Array? HOT 1
- Swift 5 support.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsoncodable.