Package org.elasticsearch.xcontent
Class ObjectParser<Value,Context>
java.lang.Object
org.elasticsearch.xcontent.AbstractObjectParser<Value,Context>
org.elasticsearch.xcontent.ObjectParser<Value,Context>
- All Implemented Interfaces:
BiFunction<XContentParser,,Context, Value> ContextParser<Context,Value>
public final class ObjectParser<Value,Context>
extends AbstractObjectParser<Value,Context>
implements BiFunction<XContentParser,Context,Value>, ContextParser<Context,Value>
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.xcontent.ObjectParser.Parser<Value, Context>, org.elasticsearch.xcontent.ParseField, org.elasticsearch.xcontent.ObjectParser.ValueType) which can be used to implement exceptional parsing operations not covered by the high level methods.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceFunctional interface for instantiating and parsing named objects.static interfacestatic interfaceDefines how to consume a parsed undefined fieldstatic enum -
Constructor Summary
ConstructorsConstructorDescriptionObjectParser(String name) Creates a new ObjectParser.ObjectParser(String name, boolean ignoreUnknownFields, Supplier<Value> valueSupplier) Creates a new ObjectParser.ObjectParser(String name, Class<C> categoryClass, BiConsumer<Value, C> unknownFieldConsumer, Supplier<Value> valueSupplier) Creates a new ObjectParser that attempts to resolve unknown fields asnamedObjects.ObjectParser(String name, Supplier<Value> valueSupplier) Creates a new ObjectParser.ObjectParser(String name, ObjectParser.UnknownFieldConsumer<Value> unknownFieldConsumer, Supplier<Value> valueSupplier) Creates a new ObjectParser that consumes unknown fields as generic Objects. -
Method Summary
Modifier and TypeMethodDescriptionapply(XContentParser parser, Context context) voiddeclareExclusiveFieldSet(String... exclusiveSet) Declares a set of fields of which at most one must appear for parsing to succeed E.g.<T> voiddeclareField(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> voiddeclareNamedObject(BiConsumer<Value, T> consumer, ObjectParser.NamedObjectParser<T, Context> namedObjectParser, ParseField field) Declares a single named object.<T> voiddeclareNamedObjects(BiConsumer<Value, List<T>> consumer, ObjectParser.NamedObjectParser<T, Context> namedObjectParser, Consumer<Value> orderedModeCallback, ParseField field) Declares named objects in the style of highlighting's field element.<T> voiddeclareNamedObjects(BiConsumer<Value, List<T>> consumer, ObjectParser.NamedObjectParser<T, Context> namedObjectParser, ParseField field) Declares named objects in the style of aggregations.<T> voiddeclareObjectOrDefault(BiConsumer<Value, T> consumer, BiFunction<XContentParser, Context, T> objectParser, Supplier<T> defaultValue, ParseField field) voiddeclareRequiredFieldSet(String... requiredSet) Declares a set of fields that are required for parsing to succeed.static <Value,Context>
ObjectParser<Value,Context> fromBuilder(String name, Function<Context, Value> valueBuilder) Creates a new ObjectParser.static <Value,ElementValue>
BiConsumer<Value,List<ElementValue>> fromList(Class<ElementValue> c, BiConsumer<Value, ElementValue[]> consumer) Adapts an array (or varags) setter into a list setter.getName()Get the name of the parser.parse(XContentParser parser, Context context) Parses a Value from the givenXContentParserparse(XContentParser parser, Value value, Context context) Parses a Value from the givenXContentParsertoString()Methods inherited from class org.elasticsearch.xcontent.AbstractObjectParser
declareBoolean, declareDouble, declareDoubleArray, declareDoubleOrNull, declareField, declareFieldArray, declareFloat, declareFloatArray, declareFloatOrNull, declareInt, declareIntArray, declareIntOrNull, declareLong, declareLongArray, declareLongOrNull, declareObject, declareObjectArray, declareObjectArrayOrNull, declareObjectOrNull, declareString, declareString, declareStringArray, declareStringOrNullMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.function.BiFunction
andThen
-
Constructor Details
-
ObjectParser
Creates a new ObjectParser.- Parameters:
name- the parsers name, used to reference the parser in exceptions and messages.
-
ObjectParser
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(String name, boolean ignoreUnknownFields, @Nullable 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(String name, ObjectParser.UnknownFieldConsumer<Value> unknownFieldConsumer, @Nullable 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(String name, Class<C> categoryClass, BiConsumer<Value, C> unknownFieldConsumer, @Nullable 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> BiConsumer<Value,List<ElementValue>> fromList(Class<ElementValue> c, BiConsumer<Value, ElementValue[]> consumer) Adapts an array (or varags) setter into a list setter. -
fromBuilder
public static <Value,Context> ObjectParser<Value,Context> fromBuilder(String name, 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- Specified by:
parsein interfaceContextParser<Value,Context> - 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:
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:
IOException- if an IOException occurs.
-
apply
- Specified by:
applyin interfaceBiFunction<XContentParser,Context, Value>
-
declareField
public void declareField(ObjectParser.Parser<Value, Context> p, ParseField parseField, ObjectParser.ValueType type) -
declareField
public <T> void declareField(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(BiConsumer<Value, T> consumer, BiFunction<XContentParser, Context, T> objectParser, Supplier<T> defaultValue, ParseField field) -
declareNamedObject
public <T> void declareNamedObject(BiConsumer<Value, T> consumer, ObjectParser.NamedObjectParser<T, Context> namedObjectParser, ParseField field) Description copied from class:AbstractObjectParserDeclares a single named object.{ "object_name": { "instance_name": { "field1": "value1", ... } } } }- Specified by:
declareNamedObjectin classAbstractObjectParser<Value,Context> - Parameters:
consumer- sets the value once it has been parsednamedObjectParser- parses the named objectfield- the field to parse
-
declareNamedObjects
public <T> void declareNamedObjects(BiConsumer<Value, List<T>> consumer, ObjectParser.NamedObjectParser<T, Context> namedObjectParser, 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(BiConsumer<Value, 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
Get the name of the parser.- Specified by:
getNamein classAbstractObjectParser<Value,Context>
-
declareRequiredFieldSet
Description copied from class:AbstractObjectParserDeclares a set of fields that are required for parsing to succeed. Only one of the values provided per String[] must be matched. E.g.declareRequiredFieldSet("foo", "bar");means at least one of "foo" or "bar" fields must be present. If neither of those fields are present, an exception will be thrown. Multiple required sets can be configured:
requires that one of "foo" or "bar" fields are present, and also that one of "bizz" or "buzz" fields are present. In JSON, it means any of these combinations are acceptable:parser.declareRequiredFieldSet("foo", "bar"); parser.declareRequiredFieldSet("bizz", "buzz");{"foo":"...", "bizz": "..."}{"bar":"...", "bizz": "..."}{"foo":"...", "buzz": "..."}{"bar":"...", "buzz": "..."}{"foo":"...", "bar":"...", "bizz": "..."}{"foo":"...", "bar":"...", "buzz": "..."}{"foo":"...", "bizz":"...", "buzz": "..."}{"bar":"...", "bizz":"...", "buzz": "..."}{"foo":"...", "bar":"...", "bizz": "...", "buzz": "..."}
failure cases Provided JSON Reason for failure {"foo":"..."}Missing "bizz" or "buzz" field {"bar":"..."}Missing "bizz" or "buzz" field {"bizz": "..."}Missing "foo" or "bar" field {"buzz": "..."}Missing "foo" or "bar" field {"foo":"...", "bar": "..."}Missing "bizz" or "buzz" field {"bizz":"...", "buzz": "..."}Missing "foo" or "bar" field {"unrelated":"..."}Missing "foo" or "bar" field, and missing "bizz" or "buzz" field - Specified by:
declareRequiredFieldSetin classAbstractObjectParser<Value,Context> - Parameters:
requiredSet- A set of required fields, where at least one of the fields in the array _must_ be present
-
declareExclusiveFieldSet
Description copied from class:AbstractObjectParserDeclares a set of fields of which at most one must appear for parsing to succeed E.g.declareExclusiveFieldSet("foo", "bar");means that only one of 'foo' or 'bar' must be present, and if both appear then an exception will be thrown. Note that this does not make 'foo' or 'bar' required - seeAbstractObjectParser.declareRequiredFieldSet(String...)for required fields. Multiple exclusive sets may be declared- Specified by:
declareExclusiveFieldSetin classAbstractObjectParser<Value,Context> - Parameters:
exclusiveSet- a set of field names, at most one of which must appear
-
toString
-