Class XContentParserUtils
java.lang.Object
org.elasticsearch.common.xcontent.XContentParserUtils
A set of static methods to get
XContentParser.Token
from XContentParser
while checking for their types and throw ParsingException
if needed.-
Method Summary
Modifier and TypeMethodDescriptionstatic void
ensureExpectedToken(org.elasticsearch.common.xcontent.XContentParser.Token expected, org.elasticsearch.common.xcontent.XContentParser.Token actual, org.elasticsearch.common.xcontent.XContentParser parser)
Makes sure that provided token is of the expected typestatic void
ensureFieldName(org.elasticsearch.common.xcontent.XContentParser parser, org.elasticsearch.common.xcontent.XContentParser.Token token, String fieldName)
Makes sure that current token is of typeXContentParser.Token.FIELD_NAME
and the field name is equal to the provided onestatic Object
parseFieldsValue(org.elasticsearch.common.xcontent.XContentParser parser)
Parse the current token depending on its token type.static <T> List<T>
parseList(org.elasticsearch.common.xcontent.XContentParser parser, org.elasticsearch.core.CheckedFunction<org.elasticsearch.common.xcontent.XContentParser,T,IOException> valueParser)
Parses a list of a given type from the givenparser
.static <T> void
parseTypedKeysObject(org.elasticsearch.common.xcontent.XContentParser parser, String delimiter, Class<T> objectClass, Consumer<T> consumer)
This method expects that the current field name is the concatenation of a type, a delimiter and a name (ex: terms#foo where "terms" refers to the type of a registeredNamedXContentRegistry.Entry
, "#" is the delimiter and "foo" the name of the object to parse).static void
throwUnknownField(String field, org.elasticsearch.common.xcontent.XContentLocation location)
static void
throwUnknownToken(org.elasticsearch.common.xcontent.XContentParser.Token token, org.elasticsearch.common.xcontent.XContentLocation location)
-
Method Details
-
ensureFieldName
public static void ensureFieldName(org.elasticsearch.common.xcontent.XContentParser parser, org.elasticsearch.common.xcontent.XContentParser.Token token, String fieldName) throws IOExceptionMakes sure that current token is of typeXContentParser.Token.FIELD_NAME
and the field name is equal to the provided one- Throws:
ParsingException
- if the token is not of typeXContentParser.Token.FIELD_NAME
or is not equal to the given field nameIOException
-
throwUnknownField
public static void throwUnknownField(String field, org.elasticsearch.common.xcontent.XContentLocation location)- Throws:
ParsingException
- with a "unknown field found" reason
-
throwUnknownToken
public static void throwUnknownToken(org.elasticsearch.common.xcontent.XContentParser.Token token, org.elasticsearch.common.xcontent.XContentLocation location)- Throws:
ParsingException
- with a "unknown token found" reason
-
ensureExpectedToken
public static void ensureExpectedToken(org.elasticsearch.common.xcontent.XContentParser.Token expected, org.elasticsearch.common.xcontent.XContentParser.Token actual, org.elasticsearch.common.xcontent.XContentParser parser)Makes sure that provided token is of the expected type- Throws:
ParsingException
- if the token is not equal to the expected type
-
parseFieldsValue
public static Object parseFieldsValue(org.elasticsearch.common.xcontent.XContentParser parser) throws IOExceptionParse the current token depending on its token type. The following token types will be parsed by the corresponding parser methods:XContentParser.Token.VALUE_STRING
:XContentParser.text()
XContentParser.Token.VALUE_NUMBER
:XContentParser.numberValue()
()}XContentParser.Token.VALUE_BOOLEAN
:XContentParser.booleanValue()
()}XContentParser.Token.VALUE_EMBEDDED_OBJECT
:XContentParser.binaryValue()
()}XContentParser.Token.VALUE_NULL
: returns nullXContentParser.Token.START_OBJECT
:XContentParser.mapOrdered()
()}XContentParser.Token.START_ARRAY
:XContentParser.listOrderedMap()
()}
- Throws:
ParsingException
- if the token is none of the allowed valuesIOException
-
parseTypedKeysObject
public static <T> void parseTypedKeysObject(org.elasticsearch.common.xcontent.XContentParser parser, String delimiter, Class<T> objectClass, Consumer<T> consumer) throws IOExceptionThis method expects that the current field name is the concatenation of a type, a delimiter and a name (ex: terms#foo where "terms" refers to the type of a registeredNamedXContentRegistry.Entry
, "#" is the delimiter and "foo" the name of the object to parse). It also expected that following this field name is either an Object or an array xContent structure and the cursor points to the start token of this structure. The method splits the field's name to extract the type and name and then parses the object using theXContentParser.namedObject(Class, String, Object)
method.- Type Parameters:
T
- the type of the object to parse- Parameters:
parser
- the currentXContentParser
delimiter
- the delimiter to use to splits the field's nameobjectClass
- the object class of the object to parseconsumer
- something to consume the parsed object- Throws:
IOException
- if anything went wrong during parsing or if the type or name cannot be derived from the field's nameParsingException
- if the parser isn't positioned on either START_OBJECT or START_ARRAY at the beginning
-
parseList
public static <T> List<T> parseList(org.elasticsearch.common.xcontent.XContentParser parser, org.elasticsearch.core.CheckedFunction<org.elasticsearch.common.xcontent.XContentParser,T,IOException> valueParser) throws IOExceptionParses a list of a given type from the givenparser
. Assumes that the parser is currently positioned on aXContentParser.Token.START_ARRAY
token and will fail if it is not. The returned list may or may not be mutable.- Parameters:
parser
- x-content parservalueParser
- parser for expected list value type- Returns:
- list parsed from parser
- Throws:
IOException
-