Skip to content

Commit 7dbef6d

Browse files
authored
Merge pull request #466 from mamabusi/nsjsonser-branch
2 parents 4eac74e + 6d7b27b commit 7dbef6d

File tree

2 files changed

+402
-115
lines changed

2 files changed

+402
-115
lines changed

Foundation/NSJSONSerialization.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,22 @@ open class JSONSerialization : NSObject {
202202

203203
/* 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.
204204
*/
205-
open class func jsonObject(with stream: InputStream, options opt: ReadingOptions = []) throws -> AnyObject {
206-
NSUnimplemented()
205+
open class func jsonObject(with stream: InputStream, options opt: ReadingOptions = []) throws -> Any {
206+
var data = Data()
207+
guard stream.streamStatus == .open || stream.streamStatus == .reading else {
208+
fatalError("Stream is not available for reading")
209+
}
210+
repeat {
211+
var buffer = [UInt8](repeating: 0, count: 1024)
212+
var bytesRead: Int = 0
213+
bytesRead = stream.read(&buffer, maxLength: buffer.count)
214+
if bytesRead < 0 {
215+
throw stream.streamError!
216+
} else {
217+
data.append(&buffer, count: bytesRead)
218+
}
219+
} while stream.hasBytesAvailable
220+
return try jsonObject(with: data, options: opt)
207221
}
208222
}
209223

0 commit comments

Comments
 (0)