-
-
Notifications
You must be signed in to change notification settings - Fork 198
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
Add JSONDeserializer for transforming NSData to JSON #70
Conversation
I can see an argument for it not living here because technically Argo could decode anything that comes in a Swift/Objc object form. In the future, if we look into changing the JSON enum name I would probably change the init to something like I am ok with this though. |
That's a good point. What about throwing this on a simple |
Ya I think I like the idea of it being separate from this JSON enum ... I would be more into that |
91cbd79
to
c7fc4e2
Compare
@tonyd256 Want to take another look? Also ping @thoughtbot/ios for thoughts on this? |
I think this is better |
import Runes | ||
|
||
public struct JSONDeserializer { | ||
public static func deserialize(data: NSData) -> JSON? { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Too early to yank this up to a Deserializer
protocol?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably. I think that's an easy enough refactor later on if we actually have real-world usage for parsing other sources of NSData
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair. My thinking here is that it would at least indicate to consumers of this framework that they can implement additional deserializable types if they want to.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair point.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just realized that we shouldn't be obscuring the ability to set reading options here. Consider a response that doesn't begin with an array or object. For instance...
response_date: 2015-03-04T12:43:20Z,
post: {
id: 1
title: "JSON Fragments Considered Harmful"
}
Attempting to parse this with NSJSONSerialization
without specifying the NSJSONReadingOptionsAllowFragments
option would return an error: The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.)
To work around this, I suggest we overload deserialize(data:) -> JSON?
with deserialize(data:allowFragments:) -> JSON?
.
I'm pretty sure we can disregard the other reading options, .MutableContainers
and .MutableLeaves
but I could be convinced that we should add more overloads or a function that just takes an NSJSONReadingOptions
if there's a really good use case for it.
@tonyd256 What do we think about this idea now that we have |
This adds a failable initializer that wraps up the `NSData -> AnyObject -> JSON` transformation into a self contained package. Now all you need to do is pass the `NSData` directly to `JSON` and you'll get an optional `JSON` value back. That value can then be (conditionally) passed into a decoder for parsing.
This encapsulates the transformation of `NSData` through `NSJSONSerialization` into a `JSON` object in a `JSONDeserializer` struct. The only responsibility of this object is this transformation of data.
c7fc4e2
to
0588ffa
Compare
After discussing with @tonyd256 in meat space, we don't think this is super useful. Micromanaging the I've taken a stab at this in #96 and am closing this PR in favor of that one. |
This adds a failable initializer that wraps up the
NSData -> AnyObject -> JSON
transformation into a self contained package. Now all you needto do is pass the
NSData
directly toJSON
and you'll get an optionalJSON
value back. That value can then be (conditionally) passed into adecoder for parsing.
Open to comments here. Should this live somewhere other than this?
Fixes #68