avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r1179361 - in /avro/trunk: CHANGES.txt lang/java/avro/src/main/java/org/apache/avro/io/JsonEncoder.java lang/java/avro/src/test/java/org/apache/avro/io/TestEncoders.java lang/java/tools/src/main/java/org/apache/avro/tool/DataFileReadTool.java
Date Wed, 05 Oct 2011 18:20:30 GMT
Author: cutting
Date: Wed Oct  5 18:20:30 2011
New Revision: 1179361

URL: http://svn.apache.org/viewvc?rev=1179361&view=rev
Log:
AVRO-905. Java: Change JsonEncoder to write objects on separate lines.

Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/io/JsonEncoder.java
    avro/trunk/lang/java/avro/src/test/java/org/apache/avro/io/TestEncoders.java
    avro/trunk/lang/java/tools/src/main/java/org/apache/avro/tool/DataFileReadTool.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1179361&r1=1179360&r2=1179361&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Wed Oct  5 18:20:30 2011
@@ -95,6 +95,9 @@ Avro 1.6.0 (unreleased)
     AVRO-898. Java: Extend NettyServer to support SSL.
     (Vadim Tsarik via cutting)
 
+    AVRO-905. Java: Change JsonEncoder to write objects on separate
+    lines.  (cutting)
+
   BUG FIXES
 
     AVRO-824. Java: Fix usage message of BinaryFragmentToJsonTool.

Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/io/JsonEncoder.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/io/JsonEncoder.java?rev=1179361&r1=1179360&r2=1179361&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/io/JsonEncoder.java (original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/io/JsonEncoder.java Wed Oct  5
18:20:30 2011
@@ -31,6 +31,7 @@ import org.apache.avro.util.Utf8;
 import org.codehaus.jackson.JsonEncoding;
 import org.codehaus.jackson.JsonFactory;
 import org.codehaus.jackson.JsonGenerator;
+import org.codehaus.jackson.util.MinimalPrettyPrinter;
 
 /** An {@link Encoder} for Avro's JSON data encoding. 
  * </p>
@@ -67,11 +68,17 @@ public class JsonEncoder extends Parsing
     }
   }
 
+  // by default, one object per line
   private static JsonGenerator getJsonGenerator(OutputStream out)
       throws IOException {
     if (null == out)
       throw new NullPointerException("OutputStream cannot be null"); 
-    return new JsonFactory().createJsonGenerator(out, JsonEncoding.UTF8);
+    JsonGenerator g
+      = new JsonFactory().createJsonGenerator(out, JsonEncoding.UTF8);
+    MinimalPrettyPrinter pp = new MinimalPrettyPrinter();
+    pp.setRootValueSeparator(System.getProperty("line.separator"));
+    g.setPrettyPrinter(pp);
+    return g;
   }
   
   /**

Modified: avro/trunk/lang/java/avro/src/test/java/org/apache/avro/io/TestEncoders.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/test/java/org/apache/avro/io/TestEncoders.java?rev=1179361&r1=1179360&r2=1179361&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/test/java/org/apache/avro/io/TestEncoders.java (original)
+++ avro/trunk/lang/java/avro/src/test/java/org/apache/avro/io/TestEncoders.java Wed Oct 
5 18:20:30 2011
@@ -25,6 +25,7 @@ import org.apache.avro.AvroTypeException
 import org.apache.avro.Schema;
 import org.apache.avro.Schema.Type;
 import org.apache.avro.generic.GenericDatumReader;
+import org.apache.avro.generic.GenericDatumWriter;
 import org.codehaus.jackson.JsonEncoding;
 import org.codehaus.jackson.JsonFactory;
 import org.codehaus.jackson.JsonGenerator;
@@ -93,6 +94,19 @@ public class TestEncoders {
   }
 
   @Test
+  public void testJsonEncoderNewlineDelimited() throws IOException {
+    OutputStream out = new ByteArrayOutputStream();
+    Schema ints = Schema.create(Type.INT);
+    Encoder e = factory.jsonEncoder(ints, out);
+    String separator = System.getProperty("line.separator");
+    GenericDatumWriter<Integer> writer = new GenericDatumWriter<Integer>(ints);
+    writer.write(1, e);
+    writer.write(2, e);
+    e.flush();
+    Assert.assertEquals("1"+separator+"2", out.toString());
+  }
+
+  @Test
   public void testValidatingEncoderInit() throws IOException {
     Schema s = Schema.parse("\"int\"");
     OutputStream out = new ByteArrayOutputStream();

Modified: avro/trunk/lang/java/tools/src/main/java/org/apache/avro/tool/DataFileReadTool.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/tools/src/main/java/org/apache/avro/tool/DataFileReadTool.java?rev=1179361&r1=1179360&r2=1179361&view=diff
==============================================================================
--- avro/trunk/lang/java/tools/src/main/java/org/apache/avro/tool/DataFileReadTool.java (original)
+++ avro/trunk/lang/java/tools/src/main/java/org/apache/avro/tool/DataFileReadTool.java Wed
Oct  5 18:20:30 2011
@@ -61,12 +61,10 @@ public class DataFileReadTool implements
       Schema schema = fileReader.getSchema();
       DatumWriter<Object> writer = new GenericDatumWriter<Object>(schema);
       JsonEncoder encoder = EncoderFactory.get().jsonEncoder(schema, out);
-      for (Object datum : fileReader) {
-        encoder.configure(out); //reinitializes state
+      for (Object datum : fileReader)
         writer.write(datum, encoder);
-        encoder.flush();
-        out.println();
-      }
+      encoder.flush();
+      out.println();
       out.flush();
     } finally {
       fileReader.close();



Mime
View raw message