use of org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.
the class SearchSortValuesTests method testFromXContent.
public void testFromXContent() throws IOException {
SearchSortValues sortValues = createTestItem();
XContentType xcontentType = randomFrom(XContentType.values());
boolean humanReadable = randomBoolean();
BytesReference originalBytes = toXContent(sortValues, xcontentType, humanReadable);
SearchSortValues parsed;
try (XContentParser parser = createParser(xcontentType.xContent(), originalBytes)) {
// skip to the elements start array token, fromXContent advances from there if called
parser.nextToken();
parser.nextToken();
parser.nextToken();
parsed = SearchSortValues.fromXContent(parser);
parser.nextToken();
assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
assertNull(parser.nextToken());
}
assertToXContentEquivalent(originalBytes, toXContent(parsed, xcontentType, humanReadable), xcontentType);
}
use of org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.
the class NestedIdentityTests method testFromXContent.
public void testFromXContent() throws IOException {
NestedIdentity nestedIdentity = createTestItem(randomInt(3));
XContentType xcontentType = randomFrom(XContentType.values());
XContentBuilder builder = XContentFactory.contentBuilder(xcontentType);
if (randomBoolean()) {
builder.prettyPrint();
}
builder = nestedIdentity.innerToXContent(builder, ToXContent.EMPTY_PARAMS);
XContentParser parser = createParser(builder);
NestedIdentity parsedNestedIdentity = NestedIdentity.fromXContent(parser);
assertEquals(nestedIdentity, parsedNestedIdentity);
assertNull(parser.nextToken());
}
use of org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.
the class SourceLookup method loadSourceIfNeeded.
private Map<String, Object> loadSourceIfNeeded() {
if (source != null) {
return source;
}
if (sourceAsBytes != null) {
Tuple<XContentType, Map<String, Object>> tuple = sourceAsMapAndType(sourceAsBytes);
sourceContentType = tuple.v1();
source = tuple.v2();
return source;
}
try {
FieldsVisitor sourceFieldVisitor = new FieldsVisitor(true);
reader.document(docId, sourceFieldVisitor);
BytesReference source = sourceFieldVisitor.source();
if (source == null) {
this.source = emptyMap();
this.sourceContentType = null;
} else {
Tuple<XContentType, Map<String, Object>> tuple = sourceAsMapAndType(source);
this.sourceContentType = tuple.v1();
this.source = tuple.v2();
}
} catch (Exception e) {
throw new ElasticsearchParseException("failed to parse / load source", e);
}
return this.source;
}
use of org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.
the class XContentHelperTests method testToXContent.
public void testToXContent() throws IOException {
final XContentType xContentType = randomFrom(XContentType.values());
final ToXContent toXContent;
final boolean error;
if (randomBoolean()) {
if (randomBoolean()) {
error = false;
toXContent = (builder, params) -> builder.field("field", "value");
} else {
error = true;
toXContent = (builder, params) -> builder.startObject().field("field", "value").endObject();
}
} else {
if (randomBoolean()) {
error = false;
toXContent = (ToXContentObject) (builder, params) -> builder.startObject().field("field", "value").endObject();
} else {
error = true;
toXContent = (ToXContentObject) (builder, params) -> builder.field("field", "value");
}
}
if (error) {
expectThrows(IOException.class, () -> XContentHelper.toXContent(toXContent, xContentType, randomBoolean()));
} else {
BytesReference bytes = XContentHelper.toXContent(toXContent, xContentType, randomBoolean());
try (XContentParser parser = xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, bytes)) {
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
assertTrue(parser.nextToken().isValue());
assertEquals("value", parser.text());
assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
assertNull(parser.nextToken());
}
}
}
use of org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.
the class XContentMapValuesTests method testThatFilterIncludesEmptyObjectWhenUsingExcludes.
public void testThatFilterIncludesEmptyObjectWhenUsingExcludes() throws Exception {
XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("obj").endObject().endObject();
Tuple<XContentType, Map<String, Object>> mapTuple = XContentHelper.convertToMap(builder.bytes(), true, builder.contentType());
Map<String, Object> filteredSource = XContentMapValues.filter(mapTuple.v2(), Strings.EMPTY_ARRAY, new String[] { "nonExistingField" });
assertThat(mapTuple.v2(), equalTo(filteredSource));
}
Aggregations