Skip to content

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

Merged
merged 1 commit into from
Mar 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions Foundation/NSJSONSerialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,22 @@ open class JSONSerialization : NSObject {

/* Create a JSON object from JSON data stream. The stream should be opened and configured. All other behavior of this method is the same as the JSONObjectWithData:options:error: method.
*/
open class func jsonObject(with stream: InputStream, options opt: ReadingOptions = []) throws -> AnyObject {
NSUnimplemented()
open class func jsonObject(with stream: InputStream, options opt: ReadingOptions = []) throws -> Any {
var data = Data()
guard stream.streamStatus == .open || stream.streamStatus == .reading else {
fatalError("Stream is not available for reading")
}
repeat {
var buffer = [UInt8](repeating: 0, count: 1024)
var bytesRead: Int = 0
bytesRead = stream.read(&buffer, maxLength: buffer.count)
if bytesRead < 0 {
throw stream.streamError!
} else {
data.append(&buffer, count: bytesRead)
}
} while stream.hasBytesAvailable
return try jsonObject(with: data, options: opt)
}
}

Expand Down
Loading