Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add write method to Json Writer #1383

Merged
merged 2 commits into from
Mar 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions arrow/src/json/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,14 @@ where
Ok(())
}

/// Convert the `RecordBatch` into JSON rows, and write them to the output
pub fn write(&mut self, batch: RecordBatch) -> Result<()> {
for row in record_batches_to_json_rows(&[batch])? {
self.write_row(&Value::Object(row))?;
}
Ok(())
}

/// Convert the [`RecordBatch`] into JSON rows, and write them to the output
pub fn write_batches(&mut self, batches: &[RecordBatch]) -> Result<()> {
for row in record_batches_to_json_rows(batches)? {
Expand Down Expand Up @@ -1458,4 +1466,35 @@ mod tests {
"#
);
}

#[test]
fn test_write_single_batch() {
let test_file = "test/data/basic.json";
let builder = ReaderBuilder::new()
.infer_schema(None)
.with_batch_size(1024);
let mut reader: Reader<File> = builder
.build::<File>(File::open(test_file).unwrap())
.unwrap();
let batch = reader.next().unwrap().unwrap();

let mut buf = Vec::new();
{
let mut writer = LineDelimitedWriter::new(&mut buf);
writer.write(batch).unwrap();
}

let result = String::from_utf8(buf).unwrap();
let expected = read_to_string(test_file).unwrap();
for (r, e) in result.lines().zip(expected.lines()) {
let mut expected_json = serde_json::from_str::<Value>(e).unwrap();
// remove null value from object to make comparision consistent:
if let Value::Object(obj) = expected_json {
expected_json = Value::Object(
obj.into_iter().filter(|(_, v)| *v != Value::Null).collect(),
);
}
assert_eq!(serde_json::from_str::<Value>(r).unwrap(), expected_json,);
}
}
}