Apache Lucene open-source search software
SOLR-9709: add json.nl=map example comment, expand json.nl test coverage.
Christine Poerschked0e32f3e5ca2
diff --git a/solr/core/src/java/org/apache/solr/response/JSONResponseWriter.java b/solr/core/src/java/org/apache/solr/response/JSONResponseWriter.javaindex 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.javaindex 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 {@Testpublic 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 {