From 04a51deb50eb25c8b937e0a33caaf6d3e84b7983 Mon Sep 17 00:00:00 2001 From: Christine Poerschke Date: Wed, 2 Nov 2016 12:12:44 +0000 Subject: [PATCH] SOLR-9709: add json.nl=map example comment, expand json.nl test coverage. --- .../solr/response/JSONResponseWriter.java | 4 ++++ .../apache/solr/response/JSONWriterTest.java | 23 +++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/solr/core/src/java/org/apache/solr/response/JSONResponseWriter.java b/solr/core/src/java/org/apache/solr/response/JSONResponseWriter.java index ad128d2465e..218d2e786e1 100644 --- a/solr/core/src/java/org/apache/solr/response/JSONResponseWriter.java +++ b/solr/core/src/java/org/apache/solr/response/JSONResponseWriter.java @@ -82,11 +82,14 @@ class JSONWriter extends TextResponseWriter { final protected String namedListStyle; static final String JSON_NL_STYLE="json.nl"; + static final int JSON_NL_STYLE_COUNT = 5; // for use by JSONWriterTest + static final String JSON_NL_MAP="map"; static final String JSON_NL_FLAT="flat"; static final String JSON_NL_ARROFARR="arrarr"; static final String JSON_NL_ARROFMAP="arrmap"; static final String JSON_NL_ARROFNVP="arrnvp"; + static final String JSON_WRAPPER_FUNCTION="json.wrf"; public JSONWriter(Writer writer, SolrQueryRequest req, SolrQueryResponse rsp) { @@ -181,6 +184,7 @@ class JSONWriter extends TextResponseWriter { * repeating any keys if they are repeated in the NamedList. null is mapped * to "". */ + // NamedList("a"=1,"bar"="foo",null=3) => {"a":1,"bar":"foo","":3} protected void writeNamedListAsMapWithDups(String name, NamedList val) throws IOException { int sz = val.size(); writeMapOpener(sz); diff --git a/solr/core/src/test/org/apache/solr/response/JSONWriterTest.java b/solr/core/src/test/org/apache/solr/response/JSONWriterTest.java index a61cff32aaf..b096a094d5a 100644 --- a/solr/core/src/test/org/apache/solr/response/JSONWriterTest.java +++ b/solr/core/src/test/org/apache/solr/response/JSONWriterTest.java @@ -76,7 +76,20 @@ public class JSONWriterTest extends SolrTestCaseJ4 { @Test public void testJSON() throws IOException { - final String namedListStyle = (random().nextBoolean() ? JSONWriter.JSON_NL_ARROFARR : JSONWriter.JSON_NL_ARROFNVP); + final String[] namedListStyles = new String[] { + JSONWriter.JSON_NL_FLAT, + JSONWriter.JSON_NL_MAP, + JSONWriter.JSON_NL_ARROFARR, + JSONWriter.JSON_NL_ARROFMAP, + JSONWriter.JSON_NL_ARROFNVP, + }; + for (final String namedListStyle : namedListStyles) { + implTestJSON(namedListStyle); + } + assertEquals(JSONWriter.JSON_NL_STYLE_COUNT, namedListStyles.length); + } + + private void implTestJSON(final String namedListStyle) throws IOException { SolrQueryRequest req = req("wt","json","json.nl",namedListStyle); SolrQueryResponse rsp = new SolrQueryResponse(); JSONResponseWriter w = new JSONResponseWriter(); @@ -94,8 +107,14 @@ public class JSONWriterTest extends SolrTestCaseJ4 { w.write(buf, req, rsp); final String expectedNLjson; - if (namedListStyle == JSONWriter.JSON_NL_ARROFARR) { + if (namedListStyle == JSONWriter.JSON_NL_FLAT) { + expectedNLjson = "\"nl\":[\"data1\",\"he\\u2028llo\\u2029!\",null,42]"; + } else if (namedListStyle == JSONWriter.JSON_NL_MAP) { + expectedNLjson = "\"nl\":{\"data1\":\"he\\u2028llo\\u2029!\",\"\":42}"; + } else if (namedListStyle == JSONWriter.JSON_NL_ARROFARR) { expectedNLjson = "\"nl\":[[\"data1\",\"he\\u2028llo\\u2029!\"],[null,42]]"; + } else if (namedListStyle == JSONWriter.JSON_NL_ARROFMAP) { + expectedNLjson = "\"nl\":[{\"data1\":\"he\\u2028llo\\u2029!\"},42]"; } else if (namedListStyle == JSONWriter.JSON_NL_ARROFNVP) { expectedNLjson = "\"nl\":[{\"name\":\"data1\",\"str\":\"he\\u2028llo\\u2029!\"},{\"int\":42}]"; } else {