You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@solr.apache.org by no...@apache.org on 2022/11/09 12:20:14 UTC
[solr] branch jira/solr-16530 created (now 9e4ac6d3c2d)
This is an automated email from the ASF dual-hosted git repository.
noble pushed a change to branch jira/solr-16530
in repository https://gitbox.apache.org/repos/asf/solr.git
at 9e4ac6d3c2d rewrite NoggitJSONWriter to override handleUnknownClass instead of write(Object)
This branch includes the following new commits:
new 9e4ac6d3c2d rewrite NoggitJSONWriter to override handleUnknownClass instead of write(Object)
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
[solr] 01/01: rewrite NoggitJSONWriter to override handleUnknownClass instead of write(Object)
Posted by no...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
noble pushed a commit to branch jira/solr-16530
in repository https://gitbox.apache.org/repos/asf/solr.git
commit 9e4ac6d3c2d8f7e234c10cbe4353eb5c1b151b84
Author: Noble Paul <no...@gmail.com>
AuthorDate: Wed Nov 9 23:19:52 2022 +1100
rewrite NoggitJSONWriter to override handleUnknownClass instead of write(Object)
---
.../apache/solr/common/util/NoggitJSONWriter.java | 87 ++++++++++------------
1 file changed, 39 insertions(+), 48 deletions(-)
diff --git a/solr/solrj/src/java/org/apache/solr/common/util/NoggitJSONWriter.java b/solr/solrj/src/java/org/apache/solr/common/util/NoggitJSONWriter.java
index 7dc474faa6b..1cbe5d93faf 100644
--- a/solr/solrj/src/java/org/apache/solr/common/util/NoggitJSONWriter.java
+++ b/solr/solrj/src/java/org/apache/solr/common/util/NoggitJSONWriter.java
@@ -18,6 +18,7 @@
package org.apache.solr.common.util;
import java.io.IOException;
+import java.util.function.BiConsumer;
import org.apache.solr.common.IteratorWriter;
import org.apache.solr.common.MapWriter;
import org.noggit.CharArr;
@@ -31,67 +32,57 @@ public class NoggitJSONWriter extends JSONWriter {
}
@Override
- public void write(Object o) {
+ 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();
- try {
- mapWriter.writeMap(entryWriter());
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
+
+ mapWriter._forEachEntry(
+ new BiConsumer() {
+ boolean first = true;
+
+ @Override
+ public void accept(Object k, Object v) {
+ if (first) {
+ first = false;
+ } else {
+ writeValueSeparator();
+ }
+ indent();
+ writeString(k.toString());
+ writeNameSeparator();
+ write(v);
+ }
+ });
endObject();
} else if (o instanceof IteratorWriter) {
IteratorWriter iteratorWriter = (IteratorWriter) o;
startArray();
try {
- iteratorWriter.writeIter(itemWriter());
+ 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();
- } else if (o instanceof MapWriter.StringValue) {
- super.write(o.toString());
} else {
- super.write(o);
+ super.handleUnknownClass(o);
}
}
-
- private IteratorWriter.ItemWriter itemWriter() {
- return new IteratorWriter.ItemWriter() {
- private boolean first = true;
-
- @Override
- public IteratorWriter.ItemWriter add(Object o) {
- if (first) {
- first = false;
- } else {
- writeValueSeparator();
- }
- indent();
- write(o);
- return this;
- }
- };
- }
-
- private MapWriter.EntryWriter entryWriter() {
- return new MapWriter.EntryWriter() {
- private 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;
- }
- };
- }
}