avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b...@apache.org
Subject svn commit: r1720055 - in /avro/trunk: CHANGES.txt lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java
Date Tue, 15 Dec 2015 00:50:12 GMT
Author: blue
Date: Tue Dec 15 00:50:12 2015
New Revision: 1720055

URL: http://svn.apache.org/viewvc?rev=1720055&view=rev
Log:
AVRO-1584: Java: Escape characters not allowed in JSON in toString.

>From the JSON spec: "All Unicode characters may be placed within the
quotation marks except for the characters that must be escaped:
quotation mark, reverse solidus, and the control characters (U+0000
through U+001F)."

This uses the existing string escape function.

Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java
    avro/trunk/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1720055&r1=1720054&r2=1720055&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Tue Dec 15 00:50:12 2015
@@ -225,6 +225,8 @@ Avro 1.8.0 (10 August 2014)
 
     AVRO-1722: Update root LICENSE.txt and NOTICE.txt. (blue)
 
+    AVRO-1584: Java: Escape characters not allowed in JSON in toString. (blue)
+
 Avro 1.7.7 (23 July 2014)
 
   NEW FEATURES

Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java?rev=1720055&r1=1720054&r2=1720055&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java (original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java Tue Dec
15 00:50:12 2015
@@ -20,6 +20,7 @@ package org.apache.avro.generic;
 import java.nio.ByteBuffer;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.nio.charset.StandardCharsets;
 import java.util.AbstractList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -547,8 +548,7 @@ public class GenericData {
     } else if (isBytes(datum)) {
       buffer.append("{\"bytes\": \"");
       ByteBuffer bytes = (ByteBuffer)datum;
-      for (int i = bytes.position(); i < bytes.limit(); i++)
-        buffer.append((char)bytes.get(i));
+      writeEscapedString(StandardCharsets.ISO_8859_1.decode(bytes), buffer);
       buffer.append("\"}");
     } else if (((datum instanceof Float) &&       // quote Nan & Infinity
                 (((Float)datum).isInfinite() || ((Float)datum).isNaN()))
@@ -563,7 +563,7 @@ public class GenericData {
   }
   
   /* Adapted from http://code.google.com/p/json-simple */
-  private void writeEscapedString(String string, StringBuilder builder) {
+  private void writeEscapedString(CharSequence string, StringBuilder builder) {
     for(int i = 0; i < string.length(); i++){
       char ch = string.charAt(i);
       switch(ch){

Modified: avro/trunk/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java?rev=1720055&r1=1720054&r2=1720055&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java (original)
+++ avro/trunk/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java Tue
Dec 15 00:50:12 2015
@@ -319,6 +319,18 @@ public class TestGenericData {
     mapper.readTree(parser);
   }
 
+  @Test public void testToStringEscapesControlCharsInBytes() throws Exception {
+    GenericData data = GenericData.get();
+    assertEquals("{\"bytes\": \"a\\nb\"}", data.toString(ByteBuffer.wrap(new byte[] {'a',
'\n', 'b'})));
+  }
+
+  @Test public void testToStringFixed() throws Exception {
+    GenericData data = GenericData.get();
+    assertEquals("[97, 10, 98]", data.toString(new GenericData.Fixed(
+        Schema.createFixed("test", null, null, 3),
+        new byte[] {'a', '\n', 'b'})));
+  }
+
   @Test public void testToStringDoesNotEscapeForwardSlash() throws Exception {
     GenericData data = GenericData.get();
     assertEquals("\"/\"", data.toString("/"));



Mime
View raw message