lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From yo...@apache.org
Subject svn commit: r950723 - in /lucene/dev/trunk/solr: CHANGES.txt src/java/org/apache/solr/response/TextResponseWriter.java src/test/org/apache/solr/request/JSONWriterTest.java
Date Wed, 02 Jun 2010 19:05:55 GMT
Author: yonik
Date: Wed Jun  2 19:05:54 2010
New Revision: 950723

URL: http://svn.apache.org/viewvc?rev=950723&view=rev
Log:
SOLR-1914: write NaN/+-Inf as strings by default

Modified:
    lucene/dev/trunk/solr/CHANGES.txt
    lucene/dev/trunk/solr/src/java/org/apache/solr/response/TextResponseWriter.java
    lucene/dev/trunk/solr/src/test/org/apache/solr/request/JSONWriterTest.java

Modified: lucene/dev/trunk/solr/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/CHANGES.txt?rev=950723&r1=950722&r2=950723&view=diff
==============================================================================
--- lucene/dev/trunk/solr/CHANGES.txt (original)
+++ lucene/dev/trunk/solr/CHANGES.txt Wed Jun  2 19:05:54 2010
@@ -325,6 +325,9 @@ Bug Fixes
 * SOLR-1936: The JSON response format needed to escape unicode code point
   U+2028 - 'LINE SEPARATOR' (Robert Hofstra, yonik)
 
+* SOLR-1914: Change the JSON response format to output float/double
+  values of NaN,Infinity,-Infinity as strings. (yonik)
+
 
 Other Changes
 ----------------------

Modified: lucene/dev/trunk/solr/src/java/org/apache/solr/response/TextResponseWriter.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/src/java/org/apache/solr/response/TextResponseWriter.java?rev=950723&r1=950722&r2=950723&view=diff
==============================================================================
--- lucene/dev/trunk/solr/src/java/org/apache/solr/response/TextResponseWriter.java (original)
+++ lucene/dev/trunk/solr/src/java/org/apache/solr/response/TextResponseWriter.java Wed Jun
 2 19:05:54 2010
@@ -204,14 +204,28 @@ public abstract class TextResponseWriter
   public abstract void writeFloat(String name, String val) throws IOException;
 
   public void writeFloat(String name, float val) throws IOException {
-    writeFloat(name,Float.toString(val));
+    String s = Float.toString(val);
+    // If it's not a normal number, write the value as a string instead.
+    // The following test also handles NaN since comparisons are always false.
+    if (val > Float.NEGATIVE_INFINITY && val < Float.POSITIVE_INFINITY) {
+      writeFloat(name,s);
+    } else {
+      writeStr(name,s,false);
+    }
   }
 
   /** if this form of the method is called, val is the Java string form of a double */
   public abstract void writeDouble(String name, String val) throws IOException;
 
   public void writeDouble(String name, double val) throws IOException {
-    writeDouble(name,Double.toString(val));
+    String s = Double.toString(val);
+    // If it's not a normal number, write the value as a string instead.
+    // The following test also handles NaN since comparisons are always false.
+    if (val > Double.NEGATIVE_INFINITY && val < Double.POSITIVE_INFINITY) {
+      writeDouble(name,s);
+    } else {
+      writeStr(name,s,false);
+    }
   }
 
   public abstract void writeDate(String name, Date val) throws IOException;

Modified: lucene/dev/trunk/solr/src/test/org/apache/solr/request/JSONWriterTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/src/test/org/apache/solr/request/JSONWriterTest.java?rev=950723&r1=950722&r2=950723&view=diff
==============================================================================
--- lucene/dev/trunk/solr/src/test/org/apache/solr/request/JSONWriterTest.java (original)
+++ lucene/dev/trunk/solr/src/test/org/apache/solr/request/JSONWriterTest.java Wed Jun  2
19:05:54 2010
@@ -61,6 +61,11 @@ public class JSONWriterTest extends Solr
     w.write(buf, req, rsp);
     assertEquals(buf.toString(), "{'data1'=>(0.0/0.0),'data2'=>-(1.0/0.0),'data3'=>(1.0/0.0)}");
 
+    w = new JSONResponseWriter();
+    buf = new StringWriter();
+    w.write(buf, req, rsp);
+    assertEquals(buf.toString(), "{\"data1\":\"NaN\",\"data2\":\"-Infinity\",\"data3\":\"Infinity\"}");
+
   }
 
   @Test



Mime
View raw message