add list parse methods to XContentParser

This commit is contained in:
Jörg Prante 2015-06-24 11:41:22 +02:00 committed by Adrien Grand
parent 028f31b7d6
commit 411a719a86
2 changed files with 27 additions and 3 deletions

View File

@ -23,6 +23,7 @@ import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.lease.Releasable; import org.elasticsearch.common.lease.Releasable;
import java.io.IOException; import java.io.IOException;
import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
@ -130,6 +131,10 @@ public interface XContentParser extends Releasable {
Map<String, Object> mapOrdered() throws IOException; Map<String, Object> mapOrdered() throws IOException;
List<Object> list() throws IOException;
List<Object> listOrderedMap() throws IOException;
String text() throws IOException; String text() throws IOException;
String textOrNull() throws IOException; String textOrNull() throws IOException;

View File

@ -213,6 +213,16 @@ public abstract class AbstractXContentParser implements XContentParser {
return readOrderedMap(this); return readOrderedMap(this);
} }
@Override
public List<Object> list() throws IOException {
return readList(this);
}
@Override
public List<Object> listOrderedMap() throws IOException {
return readListOrderedMap(this);
}
static interface MapFactory { static interface MapFactory {
Map<String, Object> newMap(); Map<String, Object> newMap();
} }
@ -239,6 +249,14 @@ public abstract class AbstractXContentParser implements XContentParser {
return readMap(parser, ORDERED_MAP_FACTORY); return readMap(parser, ORDERED_MAP_FACTORY);
} }
static List<Object> readList(XContentParser parser) throws IOException {
return readList(parser, SIMPLE_MAP_FACTORY);
}
static List<Object> readListOrderedMap(XContentParser parser) throws IOException {
return readList(parser, ORDERED_MAP_FACTORY);
}
static Map<String, Object> readMap(XContentParser parser, MapFactory mapFactory) throws IOException { static Map<String, Object> readMap(XContentParser parser, MapFactory mapFactory) throws IOException {
Map<String, Object> map = mapFactory.newMap(); Map<String, Object> map = mapFactory.newMap();
XContentParser.Token token = parser.currentToken(); XContentParser.Token token = parser.currentToken();
@ -259,15 +277,16 @@ public abstract class AbstractXContentParser implements XContentParser {
return map; return map;
} }
private static List<Object> readList(XContentParser parser, MapFactory mapFactory, XContentParser.Token token) throws IOException { static List<Object> readList(XContentParser parser, MapFactory mapFactory) throws IOException {
ArrayList<Object> list = new ArrayList<>(); ArrayList<Object> list = new ArrayList<>();
XContentParser.Token token;
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
list.add(readValue(parser, mapFactory, token)); list.add(readValue(parser, mapFactory, token));
} }
return list; 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) { if (token == XContentParser.Token.VALUE_NULL) {
return null; return null;
} else if (token == XContentParser.Token.VALUE_STRING) { } else if (token == XContentParser.Token.VALUE_STRING) {
@ -288,7 +307,7 @@ public abstract class AbstractXContentParser implements XContentParser {
} else if (token == XContentParser.Token.START_OBJECT) { } else if (token == XContentParser.Token.START_OBJECT) {
return readMap(parser, mapFactory); return readMap(parser, mapFactory);
} else if (token == XContentParser.Token.START_ARRAY) { } else if (token == XContentParser.Token.START_ARRAY) {
return readList(parser, mapFactory, token); return readList(parser, mapFactory);
} else if (token == XContentParser.Token.VALUE_EMBEDDED_OBJECT) { } else if (token == XContentParser.Token.VALUE_EMBEDDED_OBJECT) {
return parser.binaryValue(); return parser.binaryValue();
} }