Skip to content

Commit 5fc7041

Browse files
committed
Implementing jsonObject with streams api of NSJSONSerialization.
1 parent 099f97a commit 5fc7041

File tree

2 files changed

+531
-4
lines changed

2 files changed

+531
-4
lines changed

Foundation/NSJSONSerialization.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,20 @@ open class JSONSerialization : NSObject {
184184

185185
/* 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.
186186
*/
187-
open class func jsonObject(with stream: InputStream, options opt: ReadingOptions = []) throws -> AnyObject {
188-
NSUnimplemented()
187+
open class func jsonObject(with stream: InputStream, options opt: ReadingOptions = []) throws -> Any {
188+
var buffer = [UInt8](repeating: 0, count: 1024)
189+
var data = Data()
190+
var bytesRead: Int?
191+
repeat {
192+
bytesRead = 0
193+
if stream.hasBytesAvailable {
194+
bytesRead = stream.read(&buffer, maxLength: buffer.count)
195+
if bytesRead! > 0 {
196+
data.append(&buffer, count: bytesRead!)
197+
}
198+
}
199+
} while bytesRead == buffer.count
200+
return try jsonObject(with: data, options: opt)
189201
}
190202
}
191203

0 commit comments

Comments
 (0)