Java Code Examples for org.elasticsearch.common.xcontent.XContentHelper#toXContent()

The following examples show how to use org.elasticsearch.common.xcontent.XContentHelper#toXContent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utils.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
public static Map<String, Object> convertJsonToxToStructuredMap(ToXContent jsonContent) {
    Map<String, Object> map = null;
    try {
        final BytesReference bytes = XContentHelper.toXContent(jsonContent, XContentType.JSON, false);
        map = XContentHelper.convertToMap(bytes, false, XContentType.JSON).v2();
    } catch (IOException e1) {
        throw ExceptionsHelper.convertToElastic(e1);
    }

    return map;
}
 
Example 2
Source File: Utils.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
public static JsonNode convertJsonToJackson(ToXContent jsonContent, boolean omitDefaults) {
    try {
        Map<String, String> pm = new HashMap<>(1);
        pm.put("omit_defaults", String.valueOf(omitDefaults));
        ToXContent.MapParams params = new ToXContent.MapParams(pm);

        final BytesReference bytes = XContentHelper.toXContent(jsonContent, XContentType.JSON, params, false);
        return DefaultObjectMapper.readTree(bytes.utf8ToString());
    } catch (IOException e1) {
        throw ExceptionsHelper.convertToElastic(e1);
    }

}
 
Example 3
Source File: Utils.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
public static <T> T serializeToXContentToPojo(ToXContent jsonContent, Class<T> clazz) {
    try {

        if (jsonContent instanceof BytesReference) {
            return serializeToXContentToPojo(((BytesReference) jsonContent).utf8ToString(), clazz);
        }

        final BytesReference bytes = XContentHelper.toXContent(jsonContent, XContentType.JSON, false);
        return DefaultObjectMapper.readValue(bytes.utf8ToString(), clazz);
    } catch (IOException e1) {
        throw ExceptionsHelper.convertToElastic(e1);
    }

}
 
Example 4
Source File: TemplateUpgradeService.java    From crate with Apache License 2.0 5 votes vote down vote up
private BytesReference toBytesReference(IndexTemplateMetaData templateMetaData) {
    try {
        return XContentHelper.toXContent((builder, params) -> {
            IndexTemplateMetaData.Builder.toInnerXContent(templateMetaData, builder, params);
            return builder;
        }, XContentType.JSON, PARAMS, false);
    } catch (IOException ex) {
        throw new IllegalStateException("Cannot serialize template [" + templateMetaData.getName() + "]", ex);
    }
}
 
Example 5
Source File: ESTestCase.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the bytes that represent the XContent output of the provided {@link ToXContent} object, using the provided
 * {@link XContentType}. Wraps the output into a new anonymous object according to the value returned
 * by the {@link ToXContent#isFragment()} method returns. Shuffles the keys to make sure that parsing never relies on keys ordering.
 */
protected static BytesReference toShuffledXContent(ToXContent toXContent, XContentType xContentType, ToXContent.Params params,
                                                   boolean humanReadable,
                                                   CheckedBiFunction<XContent, BytesReference, XContentParser, IOException>
                                                           parserFunction,
                                                   String... exceptFieldNames) throws IOException{
    BytesReference bytes = XContentHelper.toXContent(toXContent, xContentType, params, humanReadable);
    try (XContentParser parser = parserFunction.apply(xContentType.xContent(), bytes)) {
        try (XContentBuilder builder = shuffleXContent(parser, rarely(), exceptFieldNames)) {
            return BytesReference.bytes(builder);
        }
    }
}
 
Example 6
Source File: AbstractXContentTestCase.java    From crate with Apache License 2.0 5 votes vote down vote up
public static <T extends ToXContent> XContentTester<T> xContentTester(
        CheckedBiFunction<XContent, BytesReference, XContentParser, IOException> createParser,
        Supplier<T> instanceSupplier,
        ToXContent.Params toXContentParams,
        CheckedFunction<XContentParser, T, IOException> fromXContent) {
    return new XContentTester<T>(
            createParser,
            instanceSupplier,
            (testInstance, xContentType) ->
                    XContentHelper.toXContent(testInstance, xContentType, toXContentParams, false),
            fromXContent);
}