CommandLineToolkit
34 строки · 2.0 Кб
1import Foundation
2
3public final class JSONStreamFactory {
4
5public struct JSONStreamParts {
6/// Write your incoming bytes from your source into this appendable stream.
7/// Your source could be a socket, a file, etc.
8/// You can append to this object from a background thread or queue.
9/// You should not read from this stream, you should only write to it.
10/// When your source of bytes ends (e.g. on EOF, or when socket is closed), call `close()` on this appendable stream.
11/// By closing this stream, you are stating that there no more bytes will be appended to this stream,
12/// allowing `JSONReader` to finish its continous parse operation.
13public let appendableStream: AppendableJSONStream
14
15/// Call `start()` on this reader to initiate recursive, blocking, and continous parse of your byte stream.
16/// As a result, `start()` method will either throw an error if stream error occur (e.g. broken data, unexpected values, etc.),
17/// or it will return without throwing errors.
18/// Since `start()` call is blocking, you can call it on your own queue/thread.
19public let jsonReader: JSONReader
20}
21
22/// Creates a structure of objects to allow running your JSON stream.
23/// - Parameter eventStream: This object is responsible for processing all parsed JSON objects.
24/// Consider it as a delegate of a JSON stream. This is where you get events about parsed objects.
25/// - Returns: `JSONStreamParts` object, with `appendableStream` object (you feed it with your incoming bytes),
26/// and `jsonReader` object (actual parser) configured to work together.
27public static func create(
28eventStream: JSONReaderEventStream
29) -> JSONStreamParts {
30let jsonStream: AppendableJSONStream = BlockingArrayBasedJSONStream()
31let jsonReader = JSONReader(inputStream: jsonStream, eventStream: eventStream)
32return JSONStreamParts(appendableStream: jsonStream, jsonReader: jsonReader)
33}
34}
35