Module arrow::json::writer [−][src]
Expand description
JSON Writer
This JSON writer converts Arrow RecordBatches into arrays of
JSON objects or JSON formatted byte streams.
Writing JSON Objects
To serialize RecordBatches into array of
JSON objects, use
record_batches_to_json_rows:
use std::sync::Arc; use arrow::array::Int32Array; use arrow::datatypes::{DataType, Field, Schema}; use arrow::json; use arrow::record_batch::RecordBatch; let schema = Schema::new(vec![Field::new("a", DataType::Int32, false)]); let a = Int32Array::from(vec![1, 2, 3]); let batch = RecordBatch::try_new(Arc::new(schema), vec![Arc::new(a)]).unwrap(); let json_rows = json::writer::record_batches_to_json_rows(&[batch]); assert_eq!( serde_json::Value::Object(json_rows[1].clone()), serde_json::json!({"a": 2}), );
Writing JSON formatted byte streams
To serialize RecordBatches into line-delimited JSON bytes, use
LineDelimitedWriter:
use std::sync::Arc; use arrow::array::Int32Array; use arrow::datatypes::{DataType, Field, Schema}; use arrow::json; use arrow::record_batch::RecordBatch; let schema = Schema::new(vec![Field::new("a", DataType::Int32, false)]); let a = Int32Array::from(vec![1, 2, 3]); let batch = RecordBatch::try_new(Arc::new(schema), vec![Arc::new(a)]).unwrap(); // Write the record batch out as JSON let buf = Vec::new(); let mut writer = json::LineDelimitedWriter::new(buf); writer.write_batches(&vec![batch]).unwrap(); writer.finish().unwrap(); // Get the underlying buffer back, let buf = writer.into_inner(); assert_eq!(r#"{"a":1} {"a":2} {"a":3} "#, String::from_utf8(buf).unwrap())
To serialize RecordBatches into a well formed JSON array, use
ArrayWriter:
use std::sync::Arc; use arrow::array::Int32Array; use arrow::datatypes::{DataType, Field, Schema}; use arrow::json; use arrow::record_batch::RecordBatch; let schema = Schema::new(vec![Field::new("a", DataType::Int32, false)]); let a = Int32Array::from(vec![1, 2, 3]); let batch = RecordBatch::try_new(Arc::new(schema), vec![Arc::new(a)]).unwrap(); // Write the record batch out as a JSON array let buf = Vec::new(); let mut writer = json::ArrayWriter::new(buf); writer.write_batches(&vec![batch]).unwrap(); writer.finish().unwrap(); // Get the underlying buffer back, let buf = writer.into_inner(); assert_eq!(r#"[{"a":1},{"a":2},{"a":3}]"#, String::from_utf8(buf).unwrap())
Structs
| JsonArray | Produces JSON output as a single JSON array. For example |
| LineDelimited | Produces JSON output with one record per line. For example |
| Writer | A JSON writer which serializes |
Traits
| JsonFormat | This trait defines how to format a sequence of JSON objects to a byte stream. |
Functions
| array_to_json_array | Converts an arrow |
| record_batches_to_json_rows | Converts an arrow |
Type Definitions
| ArrayWriter | A JSON writer which serializes |
| LineDelimitedWriter | A JSON writer which serializes |