use of org.elasticsearch.common.ParseField in project elasticsearch by elastic.
the class ObjectParserTests method testIgnoreUnknownFields.
public void testIgnoreUnknownFields() throws IOException {
XContentBuilder b = XContentBuilder.builder(XContentType.JSON.xContent());
b.startObject();
{
b.field("test", "foo");
b.field("junk", 2);
}
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.elasticsearch.common.ParseField in project elasticsearch by elastic.
the class ObjectParserTests method testParseEnumFromString.
public void testParseEnumFromString() throws IOException {
class TestStruct {
public TestEnum test;
public void set(TestEnum value) {
test = value;
}
}
XContentParser parser = createParser(JsonXContent.jsonXContent, "{ \"test\" : \"FOO\" }");
ObjectParser<TestStruct, Void> objectParser = new ObjectParser<>("foo");
objectParser.declareString((struct, value) -> struct.set(TestEnum.valueOf(value)), new ParseField("test"));
TestStruct s = objectParser.parse(parser, new TestStruct(), null);
assertEquals(s.test, TestEnum.FOO);
}
use of org.elasticsearch.common.ParseField in project elasticsearch by elastic.
the class ObjectParserTests method testExceptions.
public void testExceptions() throws IOException {
XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"test\" : \"foo\"}");
class TestStruct {
public void setTest(int test) {
}
}
ObjectParser<TestStruct, Void> objectParser = new ObjectParser<>("the_parser");
TestStruct s = new TestStruct();
objectParser.declareInt(TestStruct::setTest, new ParseField("test"));
try {
objectParser.parse(parser, s, null);
fail("numeric value expected");
} catch (ParsingException ex) {
assertEquals(ex.getMessage(), "[the_parser] failed to parse field [test]");
assertTrue(ex.getCause() instanceof NumberFormatException);
}
parser = createParser(JsonXContent.jsonXContent, "{\"not_supported_field\" : \"foo\"}");
try {
objectParser.parse(parser, s, null);
fail("field not supported");
} catch (IllegalArgumentException ex) {
assertEquals(ex.getMessage(), "[the_parser] unknown field [not_supported_field], parser not found");
}
}
use of org.elasticsearch.common.ParseField in project elasticsearch by elastic.
the class ObjectParserTests method testBasics.
public void testBasics() throws IOException {
XContentParser parser = createParser(JsonXContent.jsonXContent, "{\n" + " \"test\" : \"foo\",\n" + " \"test_number\" : 2,\n" + " \"test_array\": [1,2,3,4]\n" + "}");
class TestStruct {
public String test;
int testNumber;
List<Integer> ints = new ArrayList<>();
public void setTestNumber(int testNumber) {
this.testNumber = testNumber;
}
public void setInts(List<Integer> ints) {
this.ints = ints;
}
}
ObjectParser<TestStruct, Void> objectParser = new ObjectParser<>("foo");
TestStruct s = new TestStruct();
objectParser.declareField((i, c, x) -> c.test = i.text(), new ParseField("test"), ObjectParser.ValueType.STRING);
objectParser.declareInt(TestStruct::setTestNumber, new ParseField("test_number"));
objectParser.declareIntArray(TestStruct::setInts, new ParseField("test_array"));
objectParser.parse(parser, s, null);
assertEquals(s.test, "foo");
assertEquals(s.testNumber, 2);
assertEquals(s.ints, Arrays.asList(1, 2, 3, 4));
assertEquals(objectParser.toString(), "ObjectParser{name='foo', fields=[" + "FieldParser{preferred_name=test, supportedTokens=[VALUE_STRING], type=STRING}, " + "FieldParser{preferred_name=test_array, supportedTokens=[START_ARRAY, VALUE_STRING, VALUE_NUMBER], type=INT_ARRAY}, " + "FieldParser{preferred_name=test_number, supportedTokens=[VALUE_STRING, VALUE_NUMBER], type=INT}]}");
}
use of org.elasticsearch.common.ParseField in project elasticsearch by elastic.
the class ObjectParserTests method testUseClassicPullParsingSubParser.
/**
* This test ensures we can use a classic pull-parsing parser
* together with the object parser
*/
public void testUseClassicPullParsingSubParser() throws IOException {
class ClassicParser {
URI parseURI(XContentParser parser) throws IOException {
String fieldName = null;
String host = "";
int port = 0;
XContentParser.Token token;
while ((token = parser.currentToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
fieldName = parser.currentName();
} else if (token == XContentParser.Token.VALUE_STRING) {
if (fieldName.equals("host")) {
host = parser.text();
} else {
throw new IllegalStateException("boom");
}
} else if (token == XContentParser.Token.VALUE_NUMBER) {
if (fieldName.equals("port")) {
port = parser.intValue();
} else {
throw new IllegalStateException("boom");
}
}
parser.nextToken();
}
return URI.create(host + ":" + port);
}
}
class Foo {
public String name;
public URI uri;
public void setName(String name) {
this.name = name;
}
public void setURI(URI uri) {
this.uri = uri;
}
}
class CustomParseContext {
public final ClassicParser parser;
CustomParseContext(ClassicParser parser) {
this.parser = parser;
}
public URI parseURI(XContentParser parser) {
try {
return this.parser.parseURI(parser);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"url\" : { \"host\": \"http://foobar\", \"port\" : 80}, \"name\" : \"foobarbaz\"}");
ObjectParser<Foo, CustomParseContext> objectParser = new ObjectParser<>("foo");
objectParser.declareString(Foo::setName, new ParseField("name"));
objectParser.declareObjectOrDefault(Foo::setURI, (p, s) -> s.parseURI(p), () -> null, new ParseField("url"));
Foo s = objectParser.parse(parser, new Foo(), new CustomParseContext(new ClassicParser()));
assertEquals(s.uri.getHost(), "foobar");
assertEquals(s.uri.getPort(), 80);
assertEquals(s.name, "foobarbaz");
}
Aggregations