Class XContentParserUtils

java.lang.Object
org.elasticsearch.common.xcontent.XContentParserUtils

public final class XContentParserUtils extends Object
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 Type
    Method
    Description
    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
    static void
    ensureFieldName​(org.elasticsearch.common.xcontent.XContentParser parser, org.elasticsearch.common.xcontent.XContentParser.Token token, 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 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 given parser.
    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 registered NamedXContentRegistry.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)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • ensureFieldName

      public static void ensureFieldName(org.elasticsearch.common.xcontent.XContentParser parser, org.elasticsearch.common.xcontent.XContentParser.Token token, String fieldName) throws IOException
      Makes sure that current token is of type XContentParser.Token.FIELD_NAME and the field name is equal to the provided one
      Throws:
      ParsingException - if the token is not of type XContentParser.Token.FIELD_NAME or is not equal to the given field name
      IOException
    • 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 IOException
      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 null
      • XContentParser.Token.START_OBJECT: XContentParser.mapOrdered() ()}
      • XContentParser.Token.START_ARRAY: XContentParser.listOrderedMap() ()}
      Throws:
      ParsingException - if the token is none of the allowed values
      IOException
    • parseTypedKeysObject

      public static <T> void parseTypedKeysObject(org.elasticsearch.common.xcontent.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 registered 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.
      Type Parameters:
      T - the type of the object to parse
      Parameters:
      parser - the current XContentParser
      delimiter - the delimiter to use to splits the field's name
      objectClass - the object class of the object to parse
      consumer - 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 name
      ParsingException - 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 IOException
      Parses a list of a given type from the given parser. Assumes that the parser is currently positioned on a XContentParser.Token.START_ARRAY token and will fail if it is not. The returned list may or may not be mutable.
      Parameters:
      parser - x-content parser
      valueParser - parser for expected list value type
      Returns:
      list parsed from parser
      Throws:
      IOException