From d9f5941e76f5220a6b76eccfed557579bfc8e0ae Mon Sep 17 00:00:00 2001 From: Yonik Seeley Date: Wed, 2 Jun 2010 19:05:54 +0000 Subject: [PATCH] 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 --- solr/CHANGES.txt | 3 +++ .../solr/response/TextResponseWriter.java | 18 ++++++++++++++++-- .../apache/solr/request/JSONWriterTest.java | 5 +++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index eb27bf049be..9f1c8bc6db8 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -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 ---------------------- diff --git a/solr/src/java/org/apache/solr/response/TextResponseWriter.java b/solr/src/java/org/apache/solr/response/TextResponseWriter.java index 8eb282d0c09..8c7209a7ccb 100644 --- a/solr/src/java/org/apache/solr/response/TextResponseWriter.java +++ b/solr/src/java/org/apache/solr/response/TextResponseWriter.java @@ -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; diff --git a/solr/src/test/org/apache/solr/request/JSONWriterTest.java b/solr/src/test/org/apache/solr/request/JSONWriterTest.java index e108d354fc7..13389ea54a5 100644 --- a/solr/src/test/org/apache/solr/request/JSONWriterTest.java +++ b/solr/src/test/org/apache/solr/request/JSONWriterTest.java @@ -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