Search in sources :

Example 6 with ParseField

use of 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 7 with ParseField

use of 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 8 with ParseField

use of 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 9 with ParseField

use of org.elasticsearch.common.ParseField in project elasticsearch by elastic.

the class ObjectParserTests method testObjectOrDefault.

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

Example 10 with ParseField

use of org.elasticsearch.common.ParseField in project elasticsearch by elastic.

the class ObjectParserTests method testIgnoreUnknownArrays.

public void testIgnoreUnknownArrays() throws IOException {
    XContentBuilder b = XContentBuilder.builder(XContentType.JSON.xContent());
    b.startObject();
    {
        b.field("test", "foo");
        b.startArray("junk");
        {
            b.startObject();
            {
                b.field("really", "junk");
            }
            b.endObject();
        }
        b.endArray();
    }
    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)

Aggregations

ParseField (org.elasticsearch.common.ParseField)29 NamedObjectParser (org.elasticsearch.common.xcontent.ObjectParser.NamedObjectParser)17 ParsingException (org.elasticsearch.common.ParsingException)7 IOException (java.io.IOException)5 List (java.util.List)5 ArrayList (java.util.ArrayList)4 Arrays (java.util.Arrays)3 ObjectParser (org.elasticsearch.common.xcontent.ObjectParser)3 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)3 UncheckedIOException (java.io.UncheckedIOException)2 URI (java.net.URI)2 HashMap (java.util.HashMap)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 JsonXContent (org.elasticsearch.common.xcontent.json.JsonXContent)2