alamb commented on a change in pull request #9575:
URL: https://github.com/apache/arrow/pull/9575#discussion_r583147570



##########
File path: rust/arrow/src/json/writer.rs
##########
@@ -309,40 +353,167 @@ pub fn record_batches_to_json_rows(
     rows
 }
 
-/// A JSON writer
-#[derive(Debug)]
-pub struct Writer<W: Write> {
-    writer: BufWriter<W>,
+/// This trait defines how to format a sequence of JSON objects to a
+/// byte stream.
+pub trait JsonFormat: Debug + Default {
+    #[inline]
+    /// write any bytes needed at the start of the file to the writer
+    fn start_stream<W: Write>(&self, _writer: &mut W) -> Result<()> {
+        Ok(())
+    }
+
+    #[inline]
+    /// write any bytes needed for the start of each row
+    fn start_row<W: Write>(&self, _writer: &mut W, _is_first_row: bool) -> 
Result<()> {
+        Ok(())
+    }
+
+    #[inline]
+    /// write any bytes needed for the end of each row
+    fn end_row<W: Write>(&self, _writer: &mut W) -> Result<()> {
+        Ok(())
+    }
+
+    /// write any bytes needed for the start of each row
+    fn end_stream<W: Write>(&self, _writer: &mut W) -> Result<()> {
+        Ok(())
+    }
 }
 
-impl<W: Write> Writer<W> {
-    pub fn new(writer: W) -> Self {
-        Self::from_buf_writer(BufWriter::new(writer))
+/// Produces JSON output with one record per line. For example
+///
+/// ```json
+/// {"foo":1}
+/// {"bar":1}
+///
+/// ```
+#[derive(Debug, Default)]
+pub struct LineDelimited {}
+
+impl JsonFormat for LineDelimited {
+    fn end_row<W: Write>(&self, writer: &mut W) -> Result<()> {
+        writer.write_all(b"\n")?;
+        Ok(())
+    }
+}
+
+/// Produces JSON output as a single JSON array. For example
+///
+/// ```json
+/// [{"foo":1},{"bar":1}]
+/// ```
+#[derive(Debug, Default)]
+pub struct JsonArray {}
+
+impl JsonFormat for JsonArray {
+    fn start_stream<W: Write>(&self, writer: &mut W) -> Result<()> {
+        writer.write_all(b"[")?;
+        Ok(())
+    }
+
+    fn start_row<W: Write>(&self, writer: &mut W, is_first_row: bool) -> 
Result<()> {
+        if !is_first_row {
+            writer.write_all(b",")?;
+        }
+        Ok(())
     }
 
-    pub fn from_buf_writer(writer: BufWriter<W>) -> Self {
-        Self { writer }
+    fn end_stream<W: Write>(&self, writer: &mut W) -> Result<()> {
+        writer.write_all(b"]")?;
+        Ok(())
     }
+}
+
+/// A JSON writer which serializes [`RecordBatch`]es to newline delimited JSON 
objects
+pub type LineDelimitedWriter<W> = Writer<W, LineDelimited>;
 
+/// A JSON writer which serializes [`RecordBatch`]es to JSON arrays
+pub type ArrayWriter<W> = Writer<W, JsonArray>;
+
+/// A JSON writer which serializes [`RecordBatch`]es to a stream of
+/// `u8` encoded JSON objects. See the module level documentation for
+/// detailed usage and examples. The specific format of the stream is
+/// controlled by the [`JsonFormat`] type parameter.
+#[derive(Debug)]
+pub struct Writer<W, F>
+where
+    W: Write,
+    F: JsonFormat,
+{
+    /// Underlying writer to use to write bytes
+    writer: W,
+
+    /// Has the writer output any records yet?
+    started: bool,
+
+    /// Is the writer finished?
+    finished: bool,
+
+    /// Determines how the byte stream is formatted
+    format: F,
+}
+
+impl<W, F> Writer<W, F>
+where
+    W: Write,
+    F: JsonFormat,
+{
+    /// Construct a new writer
+    pub fn new(writer: W) -> Self {
+        Self {
+            writer,
+            started: false,
+            finished: false,
+            format: F::default(),

Review comment:
       There isn't a default format for `Writer` (I couldn't figure out how to 
make one).
   
   This line makes an instance of the formatter. Though come to think of it, 
none of the formatters  actually have state now 🤔  I could move some of the 
state into `JsonArray` maybe to make that clearer




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to