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");
}
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());
}
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);
}
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");
}
}
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;
}
Aggregations