Comments (16)
I notice you conspicuously left out "Role" in your translation 😉
For the model you have, that looks about right. But when you're dealing with types not handled by NSJSONSerialization
, including arrays of those types, you get a lot more help from Argo.
from argo.
Totally valid concern. In addition to what you posted, you'd also need to add the create
function to your user models.
Also, keep in mind that pulling Argo in as a dependency of your framework would require users to link Argo (and Runes, and soon Box as well) to your user's apps (assuming they are using frameworks. CocoaPods will do this for them). That might be a deal breaker.
from argo.
I notice you conspicuously left out "Role" in your translation
🙈 - updated to add that and fix one other issue.
from argo.
In addition to what you posted, you'd also need to add the create function to your user models.
Aware of that if using Argo, thanks. I've written too many curried create
functions to be able to forget!
from argo.
when you're dealing with types not handled by
NSJSONSerialization
, including arrays of those types, you get a lot more help from Argo
@michaelmcguire could you elaborate on that?
from argo.
Sure, the line:
role = j["role"] as Role,
probably won't do what you want it to do. It is probably another NSDictionary
that contains the key/values for the Role
that will need to be properly mapped, so you would need code to do that also. Of course, you don't get that "for free" from Argo, as you would need to implement JSONEncodable
for that also. But if you do that, then you get handling an array of them.
from argo.
yep. @michaelmcguire is exactly right. You'd have to do something like:
role = Role.decode(j["role"])
from argo.
Yeah, I realized that as soon as I'd asked. That seems totally reasonable though.
Any other concerns or gotchas?
from argo.
As for arrays?
friends = j["friends"].map { User.decode($0) }
from argo.
Yes, but you'd also need to bring in sequence
to get the same behavior of Argo (when one decode fails, the entire array fails)
from argo.
You example also probably won't handle the case of actual optional values. The if let
will fail if it's .None
, where as you'd want to let that succeed. You might want to confirm this.
from argo.
when one decode fails, the entire array fails
TIL.
It looks like map
succeeds until the first decode fails rather than all or nothing. In some cases it might actually be desirable to do:
friends = j["friends"].reduce([], combine: { accum, elem in
if let user = User.decode(elem) {
return accum + [user]
} else {
return accum
}
})
(specialized for User
, could be made generic)
from argo.
You example also probably won't handle the case of actual optional values. The if let will fail if it's .None, where as you'd want to let that succeed. You might want to confirm this.
@gfontenot do you mean if a key is missing from the JSON dictionary? If so, this works:
let j: NSDictionary = [
"id": 123
]
// prints "success"
if let name = j["name"] as? String?, id = j["id"] as? Int {
println("success")
} else {
println("failure")
}
Sorry for the detour. This all helps in weighing things up.
from argo.
Good to know that using as? Foo?
works as I'd hope. I hadn't ever tried it, so was taking the more pessimistic route.
re: your map
/reduce
question, map
should return [User?]
and your reduce
version should return [User]
. We chose to go with [User]?
(using map
/sequence
) because it fits in line with the behavior of the rest of the lib (we never fail silently). I think it's probably fine for you to use reduce
to get [User]
, but if you were trying to replicate Argo's behavior exactly, you'd want to use map
/sequence
.
from argo.
In addition to all of this, Argo also does JSON parsing which I'd need to handle.
I think there's a pretty compelling case for me to continue to use it.
Thanks @michaelmcguire @gfontenot!
from argo.
Argo doesn't actually parse JSON itself, to be clear. We rely on NSJSONSerialization
to do the NSData -> AnyObject
transformation. We just then turn it into an enum for performance/type safety reasons.
from argo.
Related Issues (20)
- error: no such module 'Runes' HOT 7
- Problem with Decodable on xcode 9 HOT 3
- RFC: Renaming the Decodable protocol
- How to decode a "Dictionary" to "[Model]"? HOT 3
- Parse nested array failed HOT 5
- Parse nested model fails when adding a specific key HOT 2
- How to decode dynamic keys HOT 2
- RFC: Renaming (or removing) the `<|` family of operators HOT 5
- Decode type 'Any' HOT 2
- RFC: Make Swift.Decodable conform to Argo.Decodable in Swift 4.1 HOT 4
- Convert all wrong type return JSON to specific type HOT 6
- Decoding the same object with different keys HOT 6
- How to parse an empty dictionary object in Argo HOT 1
- [Question] Array decoding HOT 4
- Using pure can't compile using Swift 3 HOT 2
- Decodable' is ambiguous for type lookup in this context (Swift4) HOT 1
- Parsing a json array with strings and objects intermixed HOT 2
- Xcode 11.0 compatibility issue HOT 3
- Xcode 11.2.1 Binary operator '<^>' cannot be applied to operands of type HOT 7
- Upgrade to version 5.0.0 blocked due to Ogra library HOT 6
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 argo.