avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r1573670 - in /avro/trunk: ./ lang/java/avro/src/main/java/org/apache/avro/generic/ lang/java/avro/src/test/java/org/apache/avro/generic/ lang/java/mapred/src/main/java/org/apache/avro/mapred/
Date Mon, 03 Mar 2014 18:40:19 GMT
Author: cutting
Date: Mon Mar  3 18:40:19 2014
New Revision: 1573670

URL: http://svn.apache.org/r1573670
Log:
AVRO-1454. Java: Fix GenericData#toString and AvroAsTextRecordReader to generate valid Json
for NaN and infinities.

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
    avro/trunk/lang/java/mapred/src/main/java/org/apache/avro/mapred/AvroAsTextRecordReader.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1573670&r1=1573669&r2=1573670&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Mon Mar  3 18:40:19 2014
@@ -37,6 +37,9 @@ Trunk (not yet released)
     AVRO-1458. Java: Setting char record field via reflection affects other
     fields. (tomwhite)
 
+    AVRO-1454. Java: Fix GenericData#toString and AvroAsTextRecordReader
+    to generate valid Json for NaN and infinities.  (cutting)
+
 Avro 1.7.6 (15 January 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=1573670&r1=1573669&r2=1573670&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 Mon Mar
 3 18:40:19 2014
@@ -464,6 +464,13 @@ public class GenericData {
       for (int i = bytes.position(); i < bytes.limit(); i++)
         buffer.append((char)bytes.get(i));
       buffer.append("\"}");
+    } else if (((datum instanceof Float) &&       // quote Nan & Infinity
+                (((Float)datum).isInfinite() || ((Float)datum).isNaN()))
+               || ((datum instanceof Double) &&
+                   (((Double)datum).isInfinite() || ((Double)datum).isNaN()))) {
+      buffer.append("\"");
+      buffer.append(datum);
+      buffer.append("\"");
     } else {
       buffer.append(datum);
     }

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=1573670&r1=1573669&r2=1573670&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 Mon
Mar  3 18:40:19 2014
@@ -318,6 +318,16 @@ public class TestGenericData {
     mapper.readTree(parser);
   }
 
+  @Test public void testToStringNanInfinity() throws Exception {
+    GenericData data = GenericData.get();
+    assertEquals("\"Infinity\"",data.toString(Float.POSITIVE_INFINITY));
+    assertEquals("\"-Infinity\"",data.toString(Float.NEGATIVE_INFINITY));
+    assertEquals("\"NaN\"", data.toString(Float.NaN));
+    assertEquals("\"Infinity\"",data.toString(Double.POSITIVE_INFINITY));
+    assertEquals("\"-Infinity\"",data.toString(Double.NEGATIVE_INFINITY));
+    assertEquals("\"NaN\"", data.toString(Double.NaN));
+  }
+
   @Test
   public void testCompare() {
     // Prepare a schema for testing.

Modified: avro/trunk/lang/java/mapred/src/main/java/org/apache/avro/mapred/AvroAsTextRecordReader.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/mapred/src/main/java/org/apache/avro/mapred/AvroAsTextRecordReader.java?rev=1573670&r1=1573669&r2=1573670&view=diff
==============================================================================
--- avro/trunk/lang/java/mapred/src/main/java/org/apache/avro/mapred/AvroAsTextRecordReader.java
(original)
+++ avro/trunk/lang/java/mapred/src/main/java/org/apache/avro/mapred/AvroAsTextRecordReader.java
Mon Mar  3 18:40:19 2014
@@ -23,6 +23,7 @@ import java.nio.ByteBuffer;
 
 import org.apache.avro.file.DataFileReader;
 import org.apache.avro.file.FileReader;
+import org.apache.avro.generic.GenericData;
 import org.apache.avro.generic.GenericDatumReader;
 import org.apache.hadoop.io.Text;
 import org.apache.hadoop.mapred.FileSplit;
@@ -75,7 +76,7 @@ class AvroAsTextRecordReader<T> implemen
         key.set(bytes);
       }
     } else {
-      key.set(datum.toString());
+      key.set(GenericData.get().toString(datum));
     }
     return true;
   }



Mime
View raw message