CommandLineToolkit

Форк
0
/
JSONStreamFactory.swift 
34 строки · 2.0 Кб
1
import Foundation
2

3
public final class JSONStreamFactory {
4
    
5
    public 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.
13
        public 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.
19
        public 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.
27
    public static func create(
28
        eventStream: JSONReaderEventStream
29
    ) -> JSONStreamParts {
30
        let jsonStream: AppendableJSONStream = BlockingArrayBasedJSONStream()
31
        let jsonReader = JSONReader(inputStream: jsonStream, eventStream: eventStream)
32
        return JSONStreamParts(appendableStream: jsonStream, jsonReader: jsonReader)
33
    }
34
}
35

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.