Library should not be tied to specific data reading protocols
Bilge opened this issue · comments
This library should not have either of the methods: fromFile()
nor fromStream()
. Perhaps these could be kept around as a convenience, but a pure implementation should not have methods that deal with reading files or streams, because insodoing, the library only supports files and streams. If I wanted to use this library with a general purpose async framework like Amp or React, I can't, because it doesn't explicitly support them. A well designed implementation would not tie itself to particular data protocols, instead just accepting incomplete JSON fragments from a string buffer, like Duct.
Luckily Json machine is not tied to any data protocol. Those methods are just convenient methods. If you look inside any of the from*
methods, you'll see how it's done. Core implementation is based on an iterable which is internally consumed by foreach. You can pass it any iterable that produces chunks of json document. Example:
$jsonDocumentChunks = ['{', '"one": "two"', '}']; // or any iterable, be it array, Iterator or Generator
$iterator = new JsonMachine($jsonDocumentChunks);
foreach ($iterator as $item) {
// ...
}
@Bilge, if you want to contribute to README with an example of usage with Amp or React, feel free to do so. It will be appreciated.