SOLR-1914: write NaN/+-Inf as strings by default

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@950723 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2010-06-02 19:05:54 +00:00
parent 7000b05a05
commit d9f5941e76
3 changed files with 24 additions and 2 deletions

View File

@ -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
----------------------

View File

@ -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;

View File

@ -61,6 +61,11 @@ public class JSONWriterTest extends SolrTestCaseJ4 {
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