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
(XContentParser.Token expected, XContentParser.Token actual, XContentParser parser) Makes sure that provided token is of the expected typestatic void
ensureFieldName
(XContentParser parser, 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 void
expectValueToken
(XContentParser.Token actual, XContentParser parser) Makes sure the provided token is a value typestatic Object
parseFieldsValue
(XContentParser parser) Parse the current token depending on its token type.static <T> List
<T> parseList
(XContentParser parser, CheckedBiFunction<XContentParser, Integer, T, IOException> valueParser) This is the same asparseList(XContentParser, CheckedFunction)
except that it passes the array index while parsing the array.static <T> List
<T> parseList
(XContentParser parser, CheckedFunction<XContentParser, T, IOException> valueParser) Parses a list of a given type from the givenparser
.static <T> void
parseTypedKeysObject
(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, XContentParser parser) static void
throwUnknownToken
(XContentParser.Token token, XContentParser parser)
-
Method Details
-
ensureFieldName
public static void ensureFieldName(XContentParser parser, XContentParser.Token token, String fieldName) throws IOException Makes 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
- Throws:
ParsingException
- with a "unknown field found" reason
-
throwUnknownToken
- Throws:
ParsingException
- with a "unknown token found" reason
-
ensureExpectedToken
public static void ensureExpectedToken(XContentParser.Token expected, XContentParser.Token actual, XContentParser parser) Makes sure that provided token is of the expected type- Throws:
ParsingException
- if the token is not equal to the expected type
-
expectValueToken
Makes sure the provided token is a value type- Throws:
ParsingException
- if the token is not a value type
-
parseFieldsValue
Parse 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(XContentParser parser, String delimiter, Class<T> objectClass, Consumer<T> consumer) throws IOException 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). 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(XContentParser parser, CheckedFunction<XContentParser, T, throws IOExceptionIOException> valueParser) Parses 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
-
parseList
public static <T> List<T> parseList(XContentParser parser, CheckedBiFunction<XContentParser, Integer, throws IOExceptionT, IOException> valueParser) This is the same asparseList(XContentParser, CheckedFunction)
except that it passes the array index while parsing the array. Parses a list of a given type from the givenparser
while passing the valueParser the current array index. 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
-