public final class ObjectParser<Value,Context> extends AbstractObjectParser<Value,Context>
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.Modifier and Type | Class and Description |
---|---|
static interface |
ObjectParser.NamedObjectParser<T,Context>
Functional interface for instantiating and parsing named objects.
|
static interface |
ObjectParser.Parser<Value,Context> |
static class |
ObjectParser.ValueType |
Constructor and Description |
---|
ObjectParser(java.lang.String name)
Creates a new ObjectParser instance with a name.
|
ObjectParser(java.lang.String name,
boolean ignoreUnknownFields,
java.util.function.Supplier<Value> valueSupplier)
Creates a new ObjectParser instance which a name.
|
ObjectParser(java.lang.String name,
java.util.function.Supplier<Value> valueSupplier)
Creates a new ObjectParser instance which a name.
|
Modifier and Type | Method and Description |
---|---|
Value |
apply(XContentParser parser,
Context context) |
<T> void |
declareField(java.util.function.BiConsumer<Value,T> consumer,
ContextParser<Context,T> parser,
ParseField parseField,
ObjectParser.ValueType type)
Declare some field.
|
void |
declareField(ObjectParser.Parser<Value,Context> p,
ParseField parseField,
ObjectParser.ValueType type) |
<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)
Declares named objects in the style of highlighting's field element.
|
<T> void |
declareNamedObjects(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> 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) |
static <Value,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.String |
getName()
Get the name of the parser.
|
Value |
parse(XContentParser parser,
Context context)
Parses a Value from the given
XContentParser |
Value |
parse(XContentParser parser,
Value value,
Context context)
Parses a Value from the given
XContentParser |
java.lang.String |
toString() |
declareBoolean, declareDouble, declareDoubleArray, declareField, declareFloat, declareFloatArray, declareInt, declareIntArray, declareLong, declareLongArray, declareObject, declareObjectArray, declareRawObject, declareString, declareStringArray, declareStringOrNull
public ObjectParser(java.lang.String name)
public ObjectParser(java.lang.String name, @Nullable java.util.function.Supplier<Value> valueSupplier)
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.public ObjectParser(java.lang.String name, boolean ignoreUnknownFields, @Nullable java.util.function.Supplier<Value> valueSupplier)
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.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)
public Value parse(XContentParser parser, Context context) throws java.io.IOException
XContentParser
parser
- the parser to build a value fromcontext
- context needed for parsingObjectParser(String, Supplier)
java.io.IOException
- if an IOException occurs.public Value parse(XContentParser parser, Value value, Context context) throws java.io.IOException
XContentParser
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 parsersjava.io.IOException
- if an IOException occurs.public Value apply(XContentParser parser, Context context)
public void declareField(ObjectParser.Parser<Value,Context> p, ParseField parseField, ObjectParser.ValueType type)
public <T> void declareField(java.util.function.BiConsumer<Value,T> consumer, ContextParser<Context,T> parser, ParseField parseField, ObjectParser.ValueType type)
AbstractObjectParser
AbstractObjectParser.declareString(BiConsumer, ParseField)
or
AbstractObjectParser.declareObject(BiConsumer, ContextParser, ParseField)
rather than call this directly.declareField
in class AbstractObjectParser<Value,Context>
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)
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)
AbstractObjectParser
{
"highlight": {
"fields": { <------ this one
"title": {},
"body": {},
"category": {}
}
}
}
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.declareNamedObjects
in class AbstractObjectParser<Value,Context>
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 parsepublic <T> void declareNamedObjects(java.util.function.BiConsumer<Value,java.util.List<T>> consumer, ObjectParser.NamedObjectParser<T,Context> namedObjectParser, ParseField field)
AbstractObjectParser
{
"aggregations": {
"name_1": { "aggregation_type": {} },
"name_2": { "aggregation_type": {} },
"name_3": { "aggregation_type": {} }
}
}
}
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.declareNamedObjects
in class AbstractObjectParser<Value,Context>
consumer
- sets the values once they have been parsednamedObjectParser
- parses each named objectfield
- the field to parsepublic java.lang.String getName()
getName
in class AbstractObjectParser<Value,Context>
public java.lang.String toString()
toString
in class java.lang.Object