Apache Solr open-source search software
refactor inner class MapWriterJSONWriter (#1553)
Noble Paul87d9dff0cbf6
diff --git a/solr/solrj/src/java/org/apache/solr/common/util/MapWriterJSONWriter.java b/solr/solrj/src/java/org/apache/solr/common/util/MapWriterJSONWriter.javanew file mode 100644index 00000000000..7e9ab8ef842--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/common/util/MapWriterJSONWriter.java@@ -0,0 +1,97 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++package org.apache.solr.common.util;++import java.io.IOException;+import org.apache.solr.common.IteratorWriter;+import org.apache.solr.common.MapWriter;+import org.noggit.CharArr;+import org.noggit.JSONWriter;++class MapWriterJSONWriter extends JSONWriter {++ public MapWriterJSONWriter(CharArr out, int indentSize) {+ super(out, indentSize);+ }++ @Override+ public void handleUnknownClass(Object o) {+ // avoid materializing MapWriter / IteratorWriter to Map / List+ // instead serialize them directly+ if (o instanceof MapWriter) {+ writeMapWriter((MapWriter) o);+ } else if (o instanceof IteratorWriter) {+ IteratorWriter iteratorWriter = (IteratorWriter) o;+ writeIter(iteratorWriter);+ } else {+ super.handleUnknownClass(o);+ }+ }++ private void writeIter(IteratorWriter iteratorWriter) {+ startArray();+ try {+ iteratorWriter.writeIter(+ new IteratorWriter.ItemWriter() {+ boolean first = true;++ @Override+ public IteratorWriter.ItemWriter add(Object o) {+ if (first) {+ first = false;+ } else {+ writeValueSeparator();+ }+ indent();+ write(o);+ return this;+ }+ });+ } catch (IOException e) {+ throw new RuntimeException("this should never happen", e);+ }+ endArray();+ }++ private void writeMapWriter(MapWriter mapWriter) {+ startObject();+ try {+ mapWriter.writeMap(+ new MapWriter.EntryWriter() {+ boolean first = true;++ @Override+ public MapWriter.EntryWriter put(CharSequence k, Object v) {+ if (first) {+ first = false;+ } else {+ writeValueSeparator();+ }+ indent();+ writeString(k.toString());+ writeNameSeparator();+ write(v);+ return this;+ }+ });+ } catch (IOException e) {+ throw new RuntimeException(e);+ }+ endObject();+ }+}diff --git a/solr/solrj/src/java/org/apache/solr/common/util/Utils.java b/solr/solrj/src/java/org/apache/solr/common/util/Utils.javaindex 7c4a3387b01..6cd2dae47e9 100644--- a/solr/solrj/src/java/org/apache/solr/common/util/Utils.java+++ b/solr/solrj/src/java/org/apache/solr/common/util/Utils.java@@ -81,7 +81,6 @@ import org.apache.solr.common.annotation.JsonProperty;import org.apache.solr.common.params.CommonParams;import org.noggit.CharArr;import org.noggit.JSONParser;-import org.noggit.JSONWriter;import org.noggit.ObjectBuilder;import org.slf4j.Logger;import org.slf4j.LoggerFactory;@@ -214,75 +213,9 @@ public class Utils {return writer;}- private static class MapWriterJSONWriter extends JSONWriter {-- public MapWriterJSONWriter(CharArr out, int indentSize) {- super(out, indentSize);- }-- @Override- public void handleUnknownClass(Object o) {- // avoid materializing MapWriter / IteratorWriter to Map / List- // instead serialize them directly- if (o instanceof MapWriter) {- MapWriter mapWriter = (MapWriter) o;- startObject();- final boolean[] first = new boolean[1];- first[0] = true;- int sz = mapWriter._size();- mapWriter._forEachEntry(- (k, v) -> {- if (first[0]) {- first[0] = false;- } else {- writeValueSeparator();- }- if (sz > 1) indent();- writeString(k.toString());- writeNameSeparator();- write(v);- });- endObject();- } else if (o instanceof IteratorWriter) {- IteratorWriter iteratorWriter = (IteratorWriter) o;- startArray();- final boolean[] first = new boolean[1];- first[0] = true;- try {- iteratorWriter.writeIter(- new IteratorWriter.ItemWriter() {- @Override- public IteratorWriter.ItemWriter add(Object o) throws IOException {- if (first[0]) {- first[0] = false;- } else {- writeValueSeparator();- }- indent();- write(o);- return this;- }- });- } catch (IOException e) {- throw new RuntimeException("this should never happen", e);- }- endArray();- } else {- super.handleUnknownClass(o);- }- }- }-public static byte[] toJSON(Object o) {if (o == null) return new byte[0];CharArr out = new CharArr();- // if (!(o instanceof List) && !(o instanceof Map)) {- // if (o instanceof MapWriter) {- // o = ((MapWriter) o).toMap(new LinkedHashMap<>());- // } else if (o instanceof IteratorWriter) {- // o = ((IteratorWriter) o).toList(new ArrayList<>());- // }- // }new MapWriterJSONWriter(out, 2).write(o); // indentation by defaultreturn toUTF8(out);}