Class ObjectParser<Value,Context>
java.lang.Object
org.elasticsearch.common.xcontent.AbstractObjectParser<Value,Context>
org.elasticsearch.common.xcontent.ObjectParser<Value,Context>
- All Implemented Interfaces:
java.util.function.BiFunction<XContentParser,Context,Value>,ContextParser<Context,Value>
public final class ObjectParser<Value,Context> extends AbstractObjectParser<Value,Context>
A declarative, stateless parser that turns XContent into setter calls. A single parser should be defined for each object being parsed,
nested elements can be added via
AbstractObjectParser.declareObject(BiConsumer, ContextParser, ParseField) which should be satisfied where possible
by passing another instance of ObjectParser, this one customized for that Object.
This class works well for object that do have a constructor argument or that can be built using information available from earlier in the
XContent. For objects that have constructors with required arguments that are specified on the same level as other fields see
ConstructingObjectParser.
Instances of ObjectParser should be setup by declaring a constant field for the parsers and declaring all fields in a static
block just below the creation of the parser. Like this:
private static final ObjectParser<Thing, SomeContext> PARSER = new ObjectParser<>("thing", Thing::new));
static {
PARSER.declareInt(Thing::setMineral, new ParseField("mineral"));
PARSER.declareInt(Thing::setFruit, new ParseField("fruit"));
}
It's highly recommended to use the high level declare methods like AbstractObjectParser.declareString(BiConsumer, ParseField) instead of
declareField(org.elasticsearch.common.xcontent.ObjectParser.Parser<Value, Context>, org.elasticsearch.common.ParseField, org.elasticsearch.common.xcontent.ObjectParser.ValueType) which can be used to implement exceptional parsing operations not covered by the high level methods.-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceObjectParser.NamedObjectParser<T,Context>Functional interface for instantiating and parsing named objects.static interfaceObjectParser.Parser<Value,Context>static interfaceObjectParser.UnknownFieldConsumer<Value>Defines how to consume a parsed undefined fieldstatic classObjectParser.ValueType -
Constructor Summary
Constructors Constructor Description ObjectParser(java.lang.String name)Creates a new ObjectParser.ObjectParser(java.lang.String name, boolean ignoreUnknownFields, java.util.function.Supplier<Value> valueSupplier)Creates a new ObjectParser.ObjectParser(java.lang.String name, java.lang.Class<C> categoryClass, java.util.function.BiConsumer<Value,C> unknownFieldConsumer, java.util.function.Supplier<Value> valueSupplier)Creates a new ObjectParser that attempts to resolve unknown fields asnamedObjects.ObjectParser(java.lang.String name, java.util.function.Supplier<Value> valueSupplier)Creates a new ObjectParser.ObjectParser(java.lang.String name, ObjectParser.UnknownFieldConsumer<Value> unknownFieldConsumer, java.util.function.Supplier<Value> valueSupplier)Creates a new ObjectParser that consumes unknown fields as generic Objects. -
Method Summary
Modifier and Type Method Description Valueapply(XContentParser parser, Context context)<T> voiddeclareField(java.util.function.BiConsumer<Value,T> consumer, ContextParser<Context,T> parser, ParseField parseField, ObjectParser.ValueType type)Declare some field.voiddeclareField(ObjectParser.Parser<Value,Context> p, ParseField parseField, ObjectParser.ValueType type)<T> voiddeclareNamedObjects(java.util.function.BiConsumer<Value,java.util.List<T>> consumer, ObjectParser.NamedObjectParser<T,Context> namedObjectParser, java.util.function.Consumer<Value> orderedModeCallback, ParseField field)Declares named objects in the style of highlighting's field element.<T> voiddeclareNamedObjects(java.util.function.BiConsumer<Value,java.util.List<T>> consumer, ObjectParser.NamedObjectParser<T,Context> namedObjectParser, ParseField field)Declares named objects in the style of aggregations.<T> voiddeclareObjectOrDefault(java.util.function.BiConsumer<Value,T> consumer, java.util.function.BiFunction<XContentParser,Context,T> objectParser, java.util.function.Supplier<T> defaultValue, ParseField field)static <Value, Context>
ObjectParser<Value,Context>fromBuilder(java.lang.String name, java.util.function.Function<Context,Value> valueBuilder)Creates a new ObjectParser.static <Value, ElementValue>
java.util.function.BiConsumer<Value,java.util.List<ElementValue>>fromList(java.lang.Class<ElementValue> c, java.util.function.BiConsumer<Value,ElementValue[]> consumer)Adapts an array (or varags) setter into a list setter.java.lang.StringgetName()Get the name of the parser.Valueparse(XContentParser parser, Context context)Parses a Value from the givenXContentParserValueparse(XContentParser parser, Value value, Context context)Parses a Value from the givenXContentParserjava.lang.StringtoString()Methods inherited from class org.elasticsearch.common.xcontent.AbstractObjectParser
declareBoolean, declareDouble, declareDoubleArray, declareField, declareFieldArray, declareFloat, declareFloatArray, declareInt, declareIntArray, declareLong, declareLongArray, declareObject, declareObjectArray, declareString, declareStringArray, declareStringOrNull
-
Constructor Details
-
ObjectParser
public ObjectParser(java.lang.String name)Creates a new ObjectParser.- Parameters:
name- the parsers name, used to reference the parser in exceptions and messages.
-
ObjectParser
public ObjectParser(java.lang.String name, @Nullable java.util.function.Supplier<Value> valueSupplier)Creates a new ObjectParser.- Parameters:
name- the parsers name, used to reference the parser in exceptions and messages.valueSupplier- A supplier that creates a new Value instance. Used when the parser is used as an inner object parser.
-
ObjectParser
public ObjectParser(java.lang.String name, boolean ignoreUnknownFields, @Nullable java.util.function.Supplier<Value> valueSupplier)Creates a new ObjectParser.- Parameters:
name- the parsers name, used to reference the parser in exceptions and messages.ignoreUnknownFields- Should this parser ignore unknown fields? This should generally be set to true only when parsing responses from external systems, never when parsing requests from users.valueSupplier- a supplier that creates a new Value instance used when the parser is used as an inner object parser.
-
ObjectParser
public ObjectParser(java.lang.String name, ObjectParser.UnknownFieldConsumer<Value> unknownFieldConsumer, @Nullable java.util.function.Supplier<Value> valueSupplier)Creates a new ObjectParser that consumes unknown fields as generic Objects.- Parameters:
name- the parsers name, used to reference the parser in exceptions and messages.unknownFieldConsumer- how to consume parsed unknown fieldsvalueSupplier- a supplier that creates a new Value instance used when the parser is used as an inner object parser.
-
ObjectParser
public ObjectParser(java.lang.String name, java.lang.Class<C> categoryClass, java.util.function.BiConsumer<Value,C> unknownFieldConsumer, @Nullable java.util.function.Supplier<Value> valueSupplier)Creates a new ObjectParser that attempts to resolve unknown fields asnamedObjects.- Type Parameters:
C- the type of named object that unknown fields are expected to be- Parameters:
name- the parsers name, used to reference the parser in exceptions and messages.categoryClass- the type of named object that unknown fields are expected to beunknownFieldConsumer- how to consume parsed unknown fieldsvalueSupplier- a supplier that creates a new Value instance used when the parser is used as an inner object parser.
-
-
Method Details
-
fromList
public static <Value, ElementValue> java.util.function.BiConsumer<Value,java.util.List<ElementValue>> fromList(java.lang.Class<ElementValue> c, java.util.function.BiConsumer<Value,ElementValue[]> consumer)Adapts an array (or varags) setter into a list setter. -
fromBuilder
public static <Value, Context> ObjectParser<Value,Context> fromBuilder(java.lang.String name, java.util.function.Function<Context,Value> valueBuilder)Creates a new ObjectParser.- Parameters:
name- the parsers name, used to reference the parser in exceptions and messages.valueBuilder- A function that creates a new Value from the parse Context. Used when the parser is used as an inner object parser.
-
parse
Parses a Value from the givenXContentParser- Parameters:
parser- the parser to build a value fromcontext- context needed for parsing- Returns:
- a new value instance drawn from the provided value supplier on
ObjectParser(String, Supplier) - Throws:
java.io.IOException- if an IOException occurs.
-
parse
Parses a Value from the givenXContentParser- Parameters:
parser- the parser to build a value fromvalue- the value to fill from the parsercontext- a context that is passed along to all declared field parsers- Returns:
- the parsed value
- Throws:
java.io.IOException- if an IOException occurs.
-
apply
-
declareField
public void declareField(ObjectParser.Parser<Value,Context> p, ParseField parseField, ObjectParser.ValueType type) -
declareField
public <T> void declareField(java.util.function.BiConsumer<Value,T> consumer, ContextParser<Context,T> parser, ParseField parseField, ObjectParser.ValueType type)Description copied from class:AbstractObjectParserDeclare some field. Usually it is easier to useAbstractObjectParser.declareString(BiConsumer, ParseField)orAbstractObjectParser.declareObject(BiConsumer, ContextParser, ParseField)rather than call this directly.- Specified by:
declareFieldin classAbstractObjectParser<Value,Context>
-
declareObjectOrDefault
public <T> void declareObjectOrDefault(java.util.function.BiConsumer<Value,T> consumer, java.util.function.BiFunction<XContentParser,Context,T> objectParser, java.util.function.Supplier<T> defaultValue, ParseField field) -
declareNamedObjects
public <T> void declareNamedObjects(java.util.function.BiConsumer<Value,java.util.List<T>> consumer, ObjectParser.NamedObjectParser<T,Context> namedObjectParser, java.util.function.Consumer<Value> orderedModeCallback, ParseField field)Description copied from class:AbstractObjectParserDeclares named objects in the style of highlighting's field element. These are usually named inside and object like this:
but, when order is important, some may be written this way:{ "highlight": { "fields": { <------ this one "title": {}, "body": {}, "category": {} } } }
This is because json doesn't enforce ordering. Elasticsearch reads it in the order sent but tools that generate json are free to put object members in an unordered Map, jumbling them. Thus, if you care about order you can send the object in the second way. See NamedObjectHolder in ObjectParserTests for examples of how to invoke this.{ "highlight": { "fields": [ <------ this one {"title": {}}, {"body": {}}, {"category": {}} ] } }- Specified by:
declareNamedObjectsin classAbstractObjectParser<Value,Context>- Parameters:
consumer- sets the values once they have been parsednamedObjectParser- parses each named objectorderedModeCallback- called when the named object is parsed using the "ordered" mode (the array of objects)field- the field to parse
-
declareNamedObjects
public <T> void declareNamedObjects(java.util.function.BiConsumer<Value,java.util.List<T>> consumer, ObjectParser.NamedObjectParser<T,Context> namedObjectParser, ParseField field)Description copied from class:AbstractObjectParserDeclares named objects in the style of aggregations. These are named inside and object like this:
Unlike the other version of this method, "ordered" mode (arrays of objects) is not supported. See NamedObjectHolder in ObjectParserTests for examples of how to invoke this.{ "aggregations": { "name_1": { "aggregation_type": {} }, "name_2": { "aggregation_type": {} }, "name_3": { "aggregation_type": {} } } } }- Specified by:
declareNamedObjectsin classAbstractObjectParser<Value,Context>- Parameters:
consumer- sets the values once they have been parsednamedObjectParser- parses each named objectfield- the field to parse
-
getName
public java.lang.String getName()Get the name of the parser.- Specified by:
getNamein classAbstractObjectParser<Value,Context>
-
toString
public java.lang.String toString()- Overrides:
toStringin classjava.lang.Object
-