-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Implementing jsonObject with streams api of NSJSONSerialization. #466
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
Conversation
Foundation/NSJSONSerialization.swift
Outdated
@@ -125,8 +125,8 @@ public class JSONSerialization : NSObject { | |||
The data must be in one of the 5 supported encodings listed in the JSON specification: UTF-8, UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE. The data may or may not have a BOM. The most efficient encoding to use for parsing is UTF-8, so if you have a choice in encoding the data passed to this method, use UTF-8. | |||
*/ | |||
/// - Experiment: Note that the return type of this function is different than on Darwin Foundation (Any instead of AnyObject). This is likely to change once we have a more complete story for bridging in place. | |||
public class func jsonObject(with data: Data, options opt: ReadingOptions = []) throws -> Any { |
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.
Implementation NSJSONSerialization.jsonObject(with: stream) internally needs to call the api NSJSONSerialization.jsonObject(with: data). Currently, return type of NSJSONSerialization.jsonObject(with: data) returns 'Any' instead of 'AnyObject' and it is not possible to cast 'Any' to 'AnyObject'.
Wrote a query to the mailing list regarding the above hiccup. Following is the response from Philippe Hausler:
"We had changed the return type of JSONSerialization to be Any such that it avoided the caller needing to bridge types (which works poorly on linux). Even though this is an API change technically, just change the return type to match the other JSONSerialization methods."
@phausler: In accordance with the above suggestion, modified the return type and the related code (including the existing test cases) to resolve errors caused by the change.
I think we wound up with two implementations of the JSON stream methods. @mamabusi , I like the test cases from this - can you rebase vs master so we can merge this? |
06d0314
to
4b857b9
Compare
@parkera , I see NSJSONSerialization.writeObject(with streams) implementation is merged but not NSJSONSerialization.jsonObject(with streams). This PR implements the latter. I have resolved the conflicts. Please review. |
4b857b9
to
903e12f
Compare
Rebased to resolve the conflicts. |
9d0e640
to
5fc7041
Compare
@parkera This PR implements an unimplemented method in NSJSONSerialization. Could you please review. |
Foundation/NSJSONSerialization.swift
Outdated
data.append(&buffer, count: bytesRead!) | ||
} | ||
} | ||
} while bytesRead == buffer.count |
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.
shouldn't this condition be while stream.status == .open || stream.status == .reading
?
@swift-ci please test |
5fc7041
to
35ab1e1
Compare
streamError implementation is now available. Will work on the review comments and resolution of conflicts. |
116270c
to
54a6a33
Compare
Foundation/NSJSONSerialization.swift
Outdated
open class func jsonObject(with stream: InputStream, options opt: ReadingOptions = []) throws -> Any { | ||
var data = Data() | ||
guard stream.streamStatus == .open || stream.streamStatus == .reading else { | ||
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: [ |
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.
This should actually be fatalError
to match the Darwin behavior.
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.
Ok..
Foundation/NSJSONSerialization.swift
Outdated
]) | ||
} | ||
repeat { | ||
var buffer = [UInt8](repeating: 0, count: 1024) |
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.
Is it possible to use a Data
here instead?
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 tried to use Data instead of the buffer here, but got stuck at the point where I am supposed to send in the UnsafeMutablePointer and an Int of maxLength to read the contents of the stream:
var dataChunk = Data()
try dataChunk.withUnsafeMutableBytes {(bytes: UnsafeMutablePointer)->Void in
bytesRead = stream.read(bytes, maxLength: ??)
Is there a way I could read the InputStream to Data without the buffer?
do { | ||
try FileManager.default.removeItem(atPath: location) | ||
} catch _ { | ||
|
||
} | ||
} | ||
} | ||
|
||
//MARK: - JSONObjectWithStreams |
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.
Ok, I appreciate the extended test coverage here - but there has got to be a better way than copy/pasting all the existing test cases and just passing a stream with the same content.
Can we perhaps make a helper function which tests both data and stream versions and modify the existing tests?
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.
Sure, will try and reduce the redundancy.
0878f80
to
251e7d2
Compare
251e7d2
to
6d7b27b
Compare
@parkera Have addressed the changes requested. |
Thanks! |
@swift-ci test and merge |
No description provided.