Class InstantiatingObjectParser<Value,Context>

java.lang.Object
org.elasticsearch.xcontent.InstantiatingObjectParser<Value,Context>
All Implemented Interfaces:
BiFunction<XContentParser,Context,Value>, ContextParser<Context,Value>

public class InstantiatingObjectParser<Value,Context> extends Object implements BiFunction<XContentParser,Context,Value>, ContextParser<Context,Value>
Like ConstructingObjectParser but works with objects which have a constructor that matches declared fields.

Declaring a InstantiatingObjectParser is intentionally quite similar to declaring an ConstructingObjectParser with two important differences.

The main differences being that it is using Builder to construct the parser and takes a class of the target object instead of the object builder. The target object must have exactly one constructor with the number and order of arguments matching the number of order of declared fields. If there are more then 2 constructors with the same number of arguments, one of them needs to be marked with ParserConstructor annotation.


   public static class Thing{
       public Thing(String animal, String vegetable, int mineral) {
           ....
       }

       public void setFruit(int fruit) { ... }

       public void setBug(int bug) { ... }

   }

   private static final InstantiatingObjectParser<Thing, SomeContext> PARSER = new InstantiatingObjectParser<>("thing", Thing.class);
   static {
       PARSER.declareString(constructorArg(), new ParseField("animal"));
       PARSER.declareString(constructorArg(), new ParseField("vegetable"));
       PARSER.declareInt(optionalConstructorArg(), new ParseField("mineral"));
       PARSER.declareInt(Thing::setFruit, new ParseField("fruit"));
       PARSER.declareInt(Thing::setBug, new ParseField("bug"));
       PARSER.finalizeFields()
   }