-
Notifications
You must be signed in to change notification settings - Fork 924
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JSON Lines (NDJSON) message serialization (#5048)
## Summary This adds `json-lines` (https://jsonlines.org/ or http://ndjson.org/) as an output format. I'm sure you already know, but * JSONL is more greppable (each record is a single line) than the pretty JSON * JSONL is faster to ingest piecewise (and/or in parallel) than JSON ## Test Plan Snapshot test in the new module :)
- Loading branch information
Showing
8 changed files
with
84 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use std::io::Write; | ||
|
||
use crate::message::json::message_to_json_value; | ||
use crate::message::{Emitter, EmitterContext, Message}; | ||
|
||
#[derive(Default)] | ||
pub struct JsonLinesEmitter; | ||
|
||
impl Emitter for JsonLinesEmitter { | ||
fn emit( | ||
&mut self, | ||
writer: &mut dyn Write, | ||
messages: &[Message], | ||
_context: &EmitterContext, | ||
) -> anyhow::Result<()> { | ||
let mut w = writer; | ||
for message in messages { | ||
serde_json::to_writer(&mut w, &message_to_json_value(message))?; | ||
w.write_all(b"\n")?; | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::message::json_lines::JsonLinesEmitter; | ||
use insta::assert_snapshot; | ||
|
||
use crate::message::tests::{capture_emitter_output, create_messages}; | ||
|
||
#[test] | ||
fn output() { | ||
let mut emitter = JsonLinesEmitter::default(); | ||
let content = capture_emitter_output(&mut emitter, &create_messages()); | ||
|
||
assert_snapshot!(content); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
crates/ruff/src/message/snapshots/ruff__message__json_lines__tests__output.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
source: crates/ruff/src/message/jsonlines.rs | ||
expression: content | ||
--- | ||
{"code":"F401","message":"`os` imported but unused","fix":{"applicability":"Suggested","message":"Remove unused import: `os`","edits":[{"content":"","location":{"row":1,"column":1},"end_location":{"row":2,"column":1}}]},"location":{"row":1,"column":8},"end_location":{"row":1,"column":10},"filename":"fib.py","noqa_row":1} | ||
{"code":"F841","message":"Local variable `x` is assigned to but never used","fix":{"applicability":"Suggested","message":"Remove assignment to unused variable `x`","edits":[{"content":"","location":{"row":6,"column":5},"end_location":{"row":6,"column":10}}]},"location":{"row":6,"column":5},"end_location":{"row":6,"column":6},"filename":"fib.py","noqa_row":6} | ||
{"code":"F821","message":"Undefined name `a`","fix":null,"location":{"row":1,"column":4},"end_location":{"row":1,"column":5},"filename":"undef.py","noqa_row":1} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.