mirror of https://github.com/apache/lucene.git
SOLR-3035: make text response writers work with byte, short, and byte[] values
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1236894 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
cc93925b9f
commit
f6e22fd4fa
|
@ -23,8 +23,10 @@ import java.util.*;
|
||||||
|
|
||||||
import org.apache.lucene.document.Document;
|
import org.apache.lucene.document.Document;
|
||||||
import org.apache.lucene.index.IndexableField;
|
import org.apache.lucene.index.IndexableField;
|
||||||
|
import org.apache.lucene.util.BytesRef;
|
||||||
import org.apache.solr.common.SolrDocument;
|
import org.apache.solr.common.SolrDocument;
|
||||||
import org.apache.solr.common.SolrDocumentList;
|
import org.apache.solr.common.SolrDocumentList;
|
||||||
|
import org.apache.solr.common.util.Base64;
|
||||||
import org.apache.solr.common.util.FastWriter;
|
import org.apache.solr.common.util.FastWriter;
|
||||||
import org.apache.solr.common.util.NamedList;
|
import org.apache.solr.common.util.NamedList;
|
||||||
import org.apache.solr.request.SolrQueryRequest;
|
import org.apache.solr.request.SolrQueryRequest;
|
||||||
|
@ -130,20 +132,29 @@ public abstract class TextResponseWriter {
|
||||||
else {
|
else {
|
||||||
writeStr(name, f.stringValue(), true);
|
writeStr(name, f.stringValue(), true);
|
||||||
}
|
}
|
||||||
} else if (val instanceof Integer) {
|
} else if (val instanceof Number) {
|
||||||
|
if (val instanceof Integer) {
|
||||||
writeInt(name, val.toString());
|
writeInt(name, val.toString());
|
||||||
} else if (val instanceof Boolean) {
|
|
||||||
writeBool(name, val.toString());
|
|
||||||
} else if (val instanceof Long) {
|
} else if (val instanceof Long) {
|
||||||
writeLong(name, val.toString());
|
writeLong(name, val.toString());
|
||||||
} else if (val instanceof Date) {
|
|
||||||
writeDate(name,(Date)val);
|
|
||||||
} else if (val instanceof Float) {
|
} else if (val instanceof Float) {
|
||||||
// we pass the float instead of using toString() because
|
// we pass the float instead of using toString() because
|
||||||
// it may need special formatting. same for double.
|
// it may need special formatting. same for double.
|
||||||
writeFloat(name, ((Float)val).floatValue());
|
writeFloat(name, ((Float)val).floatValue());
|
||||||
} else if (val instanceof Double) {
|
} else if (val instanceof Double) {
|
||||||
writeDouble(name, ((Double)val).doubleValue());
|
writeDouble(name, ((Double)val).doubleValue());
|
||||||
|
} else if (val instanceof Short) {
|
||||||
|
writeInt(name, val.toString());
|
||||||
|
} else if (val instanceof Byte) {
|
||||||
|
writeInt(name, val.toString());
|
||||||
|
} else {
|
||||||
|
// default... for debugging only
|
||||||
|
writeStr(name, val.getClass().getName() + ':' + val.toString(), true);
|
||||||
|
}
|
||||||
|
} else if (val instanceof Boolean) {
|
||||||
|
writeBool(name, val.toString());
|
||||||
|
} else if (val instanceof Date) {
|
||||||
|
writeDate(name,(Date)val);
|
||||||
} else if (val instanceof Document) {
|
} else if (val instanceof Document) {
|
||||||
SolrDocument doc = toSolrDocument( (Document)val );
|
SolrDocument doc = toSolrDocument( (Document)val );
|
||||||
DocTransformer transformer = returnFields.getTransformer();
|
DocTransformer transformer = returnFields.getTransformer();
|
||||||
|
@ -181,6 +192,12 @@ public abstract class TextResponseWriter {
|
||||||
writeArray(name,(Object[])val);
|
writeArray(name,(Object[])val);
|
||||||
} else if (val instanceof Iterator) {
|
} else if (val instanceof Iterator) {
|
||||||
writeArray(name,(Iterator)val);
|
writeArray(name,(Iterator)val);
|
||||||
|
} else if (val instanceof byte[]) {
|
||||||
|
byte[] arr = (byte[])val;
|
||||||
|
writeByteArr(name, arr, 0, arr.length);
|
||||||
|
} else if (val instanceof BytesRef) {
|
||||||
|
BytesRef arr = (BytesRef)val;
|
||||||
|
writeByteArr(name, arr.bytes, arr.offset, arr.length);
|
||||||
} else {
|
} else {
|
||||||
// default... for debugging only
|
// default... for debugging only
|
||||||
writeStr(name, val.getClass().getName() + ':' + val.toString(), true);
|
writeStr(name, val.getClass().getName() + ':' + val.toString(), true);
|
||||||
|
@ -334,4 +351,7 @@ public abstract class TextResponseWriter {
|
||||||
/** if this form of the method is called, val is the Solr ISO8601 based date format */
|
/** if this form of the method is called, val is the Solr ISO8601 based date format */
|
||||||
public abstract void writeDate(String name, String val) throws IOException;
|
public abstract void writeDate(String name, String val) throws IOException;
|
||||||
|
|
||||||
|
public void writeByteArr(String name, byte[] buf, int offset, int len) throws IOException {
|
||||||
|
writeStr(name, Base64.byteArrayToBase64(buf, offset, len), false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,7 +41,7 @@ public class JSONWriterTest extends SolrTestCaseJ4 {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNaNInf() throws IOException {
|
public void testTypes() throws IOException {
|
||||||
SolrQueryRequest req = req("dummy");
|
SolrQueryRequest req = req("dummy");
|
||||||
SolrQueryResponse rsp = new SolrQueryResponse();
|
SolrQueryResponse rsp = new SolrQueryResponse();
|
||||||
QueryResponseWriter w = new PythonResponseWriter();
|
QueryResponseWriter w = new PythonResponseWriter();
|
||||||
|
@ -77,8 +77,12 @@ public class JSONWriterTest extends SolrTestCaseJ4 {
|
||||||
nl.add(null, 42);
|
nl.add(null, 42);
|
||||||
rsp.add("nl", nl);
|
rsp.add("nl", nl);
|
||||||
|
|
||||||
|
rsp.add("byte", Byte.valueOf((byte)-3));
|
||||||
|
rsp.add("short", Short.valueOf((short)-4));
|
||||||
|
rsp.add("bytes", "abc".getBytes("UTF-8"));
|
||||||
|
|
||||||
w.write(buf, req, rsp);
|
w.write(buf, req, rsp);
|
||||||
assertEquals("{\"nl\":[[\"data1\",\"he\\u2028llo\\u2029!\"],[null,42]]}", buf.toString());
|
assertEquals("{\"nl\":[[\"data1\",\"he\\u2028llo\\u2029!\"],[null,42]],\"byte\":-3,\"short\":-4,\"bytes\":\"YWJj\"}", buf.toString());
|
||||||
req.close();
|
req.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue