Class XContentParserUtils

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

public final class XContentParserUtils
extends java.lang.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, 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​(org.elasticsearch.common.xcontent.XContentParser parser)
    Parse the current token depending on its token type.
    static <T> void parseTypedKeysObject​(org.elasticsearch.common.xcontent.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, 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, java.lang.String fieldName) throws java.io.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
      java.io.IOException
    • throwUnknownField

      public static void throwUnknownField​(java.lang.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 java.lang.Object parseFieldsValue​(org.elasticsearch.common.xcontent.XContentParser parser) throws java.io.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
      java.io.IOException
    • parseTypedKeysObject

      public static <T> void parseTypedKeysObject​(org.elasticsearch.common.xcontent.XContentParser parser, java.lang.String delimiter, java.lang.Class<T> objectClass, java.util.function.Consumer<T> consumer) throws java.io.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:
      java.io.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