You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, thanks for axum-streams-rs. I noticed that the newline-delimited JSON format emits the newline separator before each item (except for the very first). This makes it harder to use than if every object were immediately followed by a newline.
On the consumer side, the natural, easy way to handle newline-delimited JSON (in general) is to buffer the response until you see a newline, then pass that off to JSON.parse. If you do that with the current implementation of this library, you'll always be delayed by the latency of the next message, and you'd potentially miss the very last message (unless you handle the edge case of a valid JSON object with no newline on stream close). So you'd need a more sophisticated response, such as trying to parse the buffer as JSON each time you get more content on the TCP stream, which is more expensive and complicated.
Interesting finding. So the problem actually happens only if we have one element to emit, so in this case it will be emitted without \n. Let me look into this.
Hi, thanks for axum-streams-rs. I noticed that the newline-delimited JSON format emits the newline separator before each item (except for the very first). This makes it harder to use than if every object were immediately followed by a newline.
On the consumer side, the natural, easy way to handle newline-delimited JSON (in general) is to buffer the response until you see a newline, then pass that off to
JSON.parse
. If you do that with the current implementation of this library, you'll always be delayed by the latency of the next message, and you'd potentially miss the very last message (unless you handle the edge case of a valid JSON object with no newline on stream close). So you'd need a more sophisticated response, such as trying to parse the buffer as JSON each time you get more content on the TCP stream, which is more expensive and complicated.Relatedly, https://github.com/ndjson/ndjson-spec#31-serialization states "Each JSON text MUST […] be written to the stream followed by the newline character
\n
".Please consider changing this code to emit the newline immediately after each object, unconditionally.
axum-streams-rs/src/json_formats.rs
Lines 81 to 87 in f48d988
The text was updated successfully, but these errors were encountered: