Search in sources :

Example 11 with ParseField

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.ParseField in project elasticsearch by elastic.

the class ObjectParserTests method testIgnoreUnknownObjects.

public void testIgnoreUnknownObjects() throws IOException {
    XContentBuilder b = XContentBuilder.builder(XContentType.JSON.xContent());
    b.startObject();
    {
        b.field("test", "foo");
        b.startObject("junk");
        {
            b.field("really", "junk");
        }
        b.endObject();
    }
    b.endObject();
    b = shuffleXContent(b);
    XContentParser parser = createParser(JsonXContent.jsonXContent, b.bytes());
    class TestStruct {

        public String test;
    }
    ObjectParser<TestStruct, Void> objectParser = new ObjectParser<>("foo", true, null);
    objectParser.declareField((i, c, x) -> c.test = i.text(), new ParseField("test"), ObjectParser.ValueType.STRING);
    TestStruct s = objectParser.parse(parser, new TestStruct(), null);
    assertEquals(s.test, "foo");
}
Also used : NamedObjectParser(org.elasticsearch.common.xcontent.ObjectParser.NamedObjectParser) ParseField(org.elasticsearch.common.ParseField)

Example 12 with ParseField

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.ParseField in project elasticsearch by elastic.

the class ObjectParserTests method testParseNamedObjectInOrderNotSupported.

public void testParseNamedObjectInOrderNotSupported() throws IOException {
    XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [\n" + "  {\"a\": {}}" + "]}");
    // Create our own parser for this test so we can disable support for the "ordered" mode specified by the array above
    ObjectParser<NamedObjectHolder, Void> objectParser = new ObjectParser<>("named_object_holder", NamedObjectHolder::new);
    objectParser.declareNamedObjects(NamedObjectHolder::setNamed, NamedObject.PARSER, new ParseField("named"));
    // Now firing the xml through it fails
    ParsingException e = expectThrows(ParsingException.class, () -> objectParser.apply(parser, null));
    assertEquals("[named_object_holder] failed to parse field [named]", e.getMessage());
    assertEquals("[named] doesn't support arrays. Use a single object with multiple fields.", e.getCause().getMessage());
}
Also used : NamedObjectParser(org.elasticsearch.common.xcontent.ObjectParser.NamedObjectParser) ParsingException(org.elasticsearch.common.ParsingException) ParseField(org.elasticsearch.common.ParseField)

Example 13 with ParseField

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.ParseField in project elasticsearch by elastic.

the class ObjectParserTests method testParseNestedShortcut.

public void testParseNestedShortcut() throws IOException {
    XContentParser parser = createParser(JsonXContent.jsonXContent, "{ \"test\" : 1, \"object\" : { \"test\": 2}}");
    ObjectParser<StaticTestStruct, Void> objectParser = new ObjectParser<>("foo", StaticTestStruct::new);
    objectParser.declareInt(StaticTestStruct::setTest, new ParseField("test"));
    objectParser.declareObject(StaticTestStruct::setObject, objectParser, new ParseField("object"));
    StaticTestStruct s = objectParser.parse(parser, null);
    assertEquals(s.test, 1);
    assertEquals(s.object.test, 2);
}
Also used : NamedObjectParser(org.elasticsearch.common.xcontent.ObjectParser.NamedObjectParser) ParseField(org.elasticsearch.common.ParseField)

Example 14 with ParseField

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.ParseField in project elasticsearch by elastic.

the class ObjectParserTests method testFailOnValueType.

public void testFailOnValueType() throws IOException {
    XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"numeric_value\" : false}");
    class TestStruct {

        public String test;
    }
    ObjectParser<TestStruct, Void> objectParser = new ObjectParser<>("foo");
    TestStruct s = new TestStruct();
    objectParser.declareField((i, c, x) -> c.test = i.text(), new ParseField("numeric_value"), ObjectParser.ValueType.FLOAT);
    try {
        objectParser.parse(parser, s, null);
        fail("wrong type - must be number");
    } catch (IllegalArgumentException ex) {
        assertEquals(ex.getMessage(), "[foo] numeric_value doesn't support values of type: VALUE_BOOLEAN");
    }
}
Also used : NamedObjectParser(org.elasticsearch.common.xcontent.ObjectParser.NamedObjectParser) ParseField(org.elasticsearch.common.ParseField)

Example 15 with ParseField

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.ParseField in project crate by crate.

the class BasePrimaryAllocationCommand method createAllocatePrimaryParser.

protected static <T extends Builder<?>> ObjectParser<T, Void> createAllocatePrimaryParser(String command) {
    ObjectParser<T, Void> parser = AbstractAllocateAllocationCommand.createAllocateParser(command);
    parser.declareBoolean(Builder::setAcceptDataLoss, new ParseField(ACCEPT_DATA_LOSS_FIELD));
    return parser;
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ParseField(org.elasticsearch.common.ParseField)

Aggregations

ParseField (org.elasticsearch.common.ParseField)31 NamedObjectParser (org.elasticsearch.common.xcontent.ObjectParser.NamedObjectParser)17 ParsingException (org.elasticsearch.common.ParsingException)7 IOException (java.io.IOException)6 List (java.util.List)6 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)5 ArrayList (java.util.ArrayList)4 ObjectParser (org.elasticsearch.common.xcontent.ObjectParser)4 UncheckedIOException (java.io.UncheckedIOException)3 Arrays (java.util.Arrays)3 HashMap (java.util.HashMap)3 URI (java.net.URI)2 Map (java.util.Map)2 CheckedFunction (org.elasticsearch.common.CheckedFunction)2 Nullable (org.elasticsearch.common.Nullable)2 BytesReference (org.elasticsearch.common.bytes.BytesReference)2 ConstructingObjectParser.constructorArg (org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg)2 ConstructingObjectParser.optionalConstructorArg (org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg)2 ValueType (org.elasticsearch.common.xcontent.ObjectParser.ValueType)2 XContentParser (org.elasticsearch.common.xcontent.XContentParser)2