From 411a719a8618919e5551e5ffc1655132c3014cb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=CC=88rg=20Prante?= Date: Wed, 24 Jun 2015 11:41:22 +0200 Subject: [PATCH] add list parse methods to XContentParser --- .../common/xcontent/XContentParser.java | 5 ++++ .../support/AbstractXContentParser.java | 25 ++++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/XContentParser.java b/core/src/main/java/org/elasticsearch/common/xcontent/XContentParser.java index b43804e1332..3901a45a181 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/XContentParser.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/XContentParser.java @@ -23,6 +23,7 @@ import org.apache.lucene.util.BytesRef; import org.elasticsearch.common.lease.Releasable; import java.io.IOException; +import java.util.List; import java.util.Map; /** @@ -130,6 +131,10 @@ public interface XContentParser extends Releasable { Map mapOrdered() throws IOException; + List list() throws IOException; + + List listOrderedMap() throws IOException; + String text() throws IOException; String textOrNull() throws IOException; diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/support/AbstractXContentParser.java b/core/src/main/java/org/elasticsearch/common/xcontent/support/AbstractXContentParser.java index 77eae8b6b50..274cd9a5e4a 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/support/AbstractXContentParser.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/support/AbstractXContentParser.java @@ -213,6 +213,16 @@ public abstract class AbstractXContentParser implements XContentParser { return readOrderedMap(this); } + @Override + public List list() throws IOException { + return readList(this); + } + + @Override + public List listOrderedMap() throws IOException { + return readListOrderedMap(this); + } + static interface MapFactory { Map newMap(); } @@ -239,6 +249,14 @@ public abstract class AbstractXContentParser implements XContentParser { return readMap(parser, ORDERED_MAP_FACTORY); } + static List readList(XContentParser parser) throws IOException { + return readList(parser, SIMPLE_MAP_FACTORY); + } + + static List readListOrderedMap(XContentParser parser) throws IOException { + return readList(parser, ORDERED_MAP_FACTORY); + } + static Map readMap(XContentParser parser, MapFactory mapFactory) throws IOException { Map map = mapFactory.newMap(); XContentParser.Token token = parser.currentToken(); @@ -259,15 +277,16 @@ public abstract class AbstractXContentParser implements XContentParser { return map; } - private static List readList(XContentParser parser, MapFactory mapFactory, XContentParser.Token token) throws IOException { + static List readList(XContentParser parser, MapFactory mapFactory) throws IOException { ArrayList list = new ArrayList<>(); + XContentParser.Token token; while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { list.add(readValue(parser, mapFactory, token)); } return list; } - private static Object readValue(XContentParser parser, MapFactory mapFactory, XContentParser.Token token) throws IOException { + static Object readValue(XContentParser parser, MapFactory mapFactory, XContentParser.Token token) throws IOException { if (token == XContentParser.Token.VALUE_NULL) { return null; } else if (token == XContentParser.Token.VALUE_STRING) { @@ -288,7 +307,7 @@ public abstract class AbstractXContentParser implements XContentParser { } else if (token == XContentParser.Token.START_OBJECT) { return readMap(parser, mapFactory); } else if (token == XContentParser.Token.START_ARRAY) { - return readList(parser, mapFactory, token); + return readList(parser, mapFactory); } else if (token == XContentParser.Token.VALUE_EMBEDDED_OBJECT) { return parser.binaryValue(); }