public final class XContentParserUtils
extends java.lang.Object
XContentParser.Token
from XContentParser
while checking for their types and throw ParsingException
if needed.Modifier and Type | Method | Description |
---|---|---|
static void |
ensureExpectedToken(XContentParser.Token expected,
XContentParser.Token actual,
java.util.function.Supplier<XContentLocation> location) |
Makes sure that provided token is of the expected type
|
static void |
ensureFieldName(XContentParser parser,
XContentParser.Token token,
java.lang.String fieldName) |
Makes sure that current token is of type
XContentParser.Token.FIELD_NAME and the field name is equal to the provided one |
static java.lang.Object |
parseFieldsValue(XContentParser parser) |
Parse the current token depending on its token type.
|
static <T> void |
parseTypedKeysObject(XContentParser parser,
java.lang.String delimiter,
java.lang.Class<T> objectClass,
java.util.function.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 registered
NamedXContentRegistry.Entry ,
"#" is the delimiter and "foo" the name of the object to parse). |
static void |
throwUnknownField(java.lang.String field,
XContentLocation location) |
|
static void |
throwUnknownToken(XContentParser.Token token,
XContentLocation location) |
public static void ensureFieldName(XContentParser parser, XContentParser.Token token, java.lang.String fieldName) throws java.io.IOException
XContentParser.Token.FIELD_NAME
and the field name is equal to the provided oneParsingException
- if the token is not of type XContentParser.Token.FIELD_NAME
or is not equal to the given field namejava.io.IOException
public static void throwUnknownField(java.lang.String field, XContentLocation location)
ParsingException
- with a "unknown field found" reasonpublic static void throwUnknownToken(XContentParser.Token token, XContentLocation location)
ParsingException
- with a "unknown token found" reasonpublic static void ensureExpectedToken(XContentParser.Token expected, XContentParser.Token actual, java.util.function.Supplier<XContentLocation> location)
ParsingException
- if the token is not equal to the expected typepublic static java.lang.Object parseFieldsValue(XContentParser parser) throws java.io.IOException
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()
()}ParsingException
- if the token is none of the allowed valuesjava.io.IOException
public static <T> void parseTypedKeysObject(XContentParser parser, java.lang.String delimiter, java.lang.Class<T> objectClass, java.util.function.Consumer<T> consumer) throws java.io.IOException
NamedXContentRegistry.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 the XContentParser.namedObject(Class, String, Object)
method.T
- the type of the object to parseparser
- the current XContentParser
delimiter
- the delimiter to use to splits the field's nameobjectClass
- the object class of the object to parseconsumer
- something to consume the parsed objectjava.io.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