java.lang.Object
org.elasticsearch.xcontent.XContentBuilder
- All Implemented Interfaces:
- Closeable,- Flushable,- AutoCloseable
A utility to build XContent (ie json).
- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionstatic interfaceInterface for transforming complex objects into their "raw" equivalents for human-readable fieldsstatic interface
- 
Constructor SummaryConstructorsConstructorDescriptionXContentBuilder(XContent xContent, OutputStream bos) Constructs a new builder using the provided XContent and an OutputStream.XContentBuilder(XContent xContent, OutputStream os, Set<String> includes, Set<String> excludes, ParsedMediaType responseContentType) Creates a new builder using the provided XContent, output stream and some inclusive and/or exclusive filters.XContentBuilder(XContent xContent, OutputStream os, Set<String> includes, Set<String> excludes, ParsedMediaType responseContentType, RestApiVersion restApiVersion) Creates a new builder using the provided XContent, output stream and some inclusive and/or exclusive filters.XContentBuilder(XContentType xContentType, OutputStream bos, Set<String> includes) Constructs a new builder using the provided XContent, an OutputStream and some filters.
- 
Method SummaryModifier and TypeMethodDescriptionarray(String name, BigDecimal[] values) array(String name, BigInteger[] values) array(String name, CheckedConsumer<XContentBuilder, IOException> callback) static XContentBuilderCreate a newXContentBuilderusing the givenXContentcontent.static XContentBuilderbuilder(XContent xContent, RestApiVersion restApiVersion) Create a newXContentBuilderusing the givenXContentcontent and RestApiVersion.static XContentBuilderCreate a newXContentBuilderusing the givenXContentTypexContentType and some inclusive and/or exclusive filters.voidclose()copyCurrentEvent(XContentParser parser) Copies current event from parser into this builder.copyCurrentStructure(XContentParser parser) directFieldAsBase64(String name, CheckedConsumer<OutputStream, IOException> writer) Write the content that is written to the output stream by thewriteras a string encoded in Base64 format.endArray()static voidensureNameNotNull(String name) static voidensureNotNull(Object value, String message) static voidensureToXContentable(Object value) Checks whether the given value is writeable as x-content.field(String name, BigDecimal value) field(String name, BigInteger value) field(String name, Collection<String> value) field(String name, ToXContent value) field(String name, ToXContent value, ToXContent.Params params) voidflush()Returns a version used for serialising a response.booleanhumanReadable(boolean isHumanReadable) Set the "human readable" flag.humanReadableField(String rawFieldName, String readableFieldName, Object value) booleanlatlon(double lat, double lon) lfAtEnd()Indicate that the currentXContentBuildermust write a line feed ("\n") at the end of the built object.mapContents(Map<String, ?> values) writes a map without the start object and end object headersobject(String name, CheckedConsumer<XContentBuilder, IOException> callback) percentageField(String rawFieldName, String readableFieldName, double percentage) rawField(String name, InputStream value) Deprecated.rawField(String name, InputStream value, XContentType contentType) Writes a raw field with the value taken from the bytes in the streamrawValue(InputStream stream, XContentType contentType) Writes a value with the source coming directly from the bytes in the streamWrites a value with the source coming directly from a pre-rendered string representationstartArray(String name) startObject(String name) stringListField(String name, Collection<String> values) stringStringMap(String name, Map<String, String> values) timestampField(String name, Object timestamp) Write a field with a timestamp value: if the passed timestamp is null then writes null, otherwise looks up the date transformer for the type oftimestampand uses it to format the value.timestampFieldsFromUnixEpochMillis(String rawFieldName, String humanReadableFieldName, long unixEpochMillis) Writes a field containing the raw number of milliseconds since the unix epoch, and also if thehumanReadableflag is set, writes a formatted representation of this value using the UNIX_EPOCH_MILLIS_FORMATTER.timestampValue(Object timestamp) Write a timestamp value: if the passed timestamp is null then writes null, otherwise looks up the date transformer for the type oftimestampand uses it to format the value.utf8Value(byte[] bytes, int offset, int length) Writes the binary content of the given byte array as UTF-8 bytes.value(boolean value) value(byte value) value(byte[] value) value(byte[] value, int offset, int length) value(double value) value(float value) value(int value) value(long value) value(short value) value(BigDecimal value) value(BigInteger value) value(ToXContent value) value(ToXContent value, ToXContent.Params params) xContentList(String name, Collection<? extends ToXContent> values) xContentList(String name, Collection<? extends ToXContent> values, ToXContent.Params params) xContentList(String name, ToXContent... values) xContentValuesMap(String name, Map<String, ? extends ToXContent> values) 
- 
Constructor Details- 
XContentBuilderConstructs a new builder using the provided XContent and an OutputStream. Make sure to callclose()when the builder is done with.- Throws:
- IOException
 
- 
XContentBuilderpublic XContentBuilder(XContentType xContentType, OutputStream bos, Set<String> includes) throws IOException Constructs a new builder using the provided XContent, an OutputStream and some filters. If filters are specified, only those values matching a filter will be written to the output stream. Make sure to callclose()when the builder is done with.- Throws:
- IOException
 
- 
XContentBuilderpublic XContentBuilder(XContent xContent, OutputStream os, Set<String> includes, Set<String> excludes, ParsedMediaType responseContentType) throws IOException Creates a new builder using the provided XContent, output stream and some inclusive and/or exclusive filters. When both exclusive and inclusive filters are provided, the underlying builder will first use exclusion filters to remove fields and then will check the remaining fields against the inclusive filters.Make sure to call close()when the builder is done with.- Parameters:
- os- the output stream
- includes- the inclusive filters: only fields and objects that match the inclusive filters will be written to the output.
- excludes- the exclusive filters: only fields and objects that don't match the exclusive filters will be written to the output.
- responseContentType- a content-type header value to be sent back on a response
- Throws:
- IOException
 
- 
XContentBuilderpublic XContentBuilder(XContent xContent, OutputStream os, Set<String> includes, Set<String> excludes, ParsedMediaType responseContentType, RestApiVersion restApiVersion) throws IOException Creates a new builder using the provided XContent, output stream and some inclusive and/or exclusive filters. When both exclusive and inclusive filters are provided, the underlying builder will first use exclusion filters to remove fields and then will check the remaining fields against the inclusive filters. Stores RestApiVersion to help steer the use of the builder depending on the version.- Parameters:
- os- the output stream
- includes- the inclusive filters: only fields and objects that match the inclusive filters will be written to the output.
- excludes- the exclusive filters: only fields and objects that don't match the exclusive filters will be written to the output.
- responseContentType- a content-type header value to be sent back on a response
- restApiVersion- a rest api version indicating with which version the XContent is compatible with.
- Throws:
- IOException
- See Also:
 
 
- 
- 
Method Details- 
builderCreate a newXContentBuilderusing the givenXContentcontent.The builder uses an internal ByteArrayOutputStreamoutput stream to build the content.- Parameters:
- xContent- the- XContent
- Returns:
- a new XContentBuilder
- Throws:
- IOException- if an- IOExceptionoccurs while building the content
 
- 
builderpublic static XContentBuilder builder(XContent xContent, RestApiVersion restApiVersion) throws IOException Create a newXContentBuilderusing the givenXContentcontent and RestApiVersion.The builder uses an internal ByteArrayOutputStreamoutput stream to build the content.- Parameters:
- xContent- the- XContent
- Returns:
- a new XContentBuilder
- Throws:
- IOException- if an- IOExceptionoccurs while building the content
 
- 
builderpublic static XContentBuilder builder(XContentType xContentType, Set<String> includes, Set<String> excludes) throws IOException Create a newXContentBuilderusing the givenXContentTypexContentType and some inclusive and/or exclusive filters.The builder uses an internal ByteArrayOutputStreamoutput stream to build the content. When both exclusive and inclusive filters are provided, the underlying builder will first use exclusion filters to remove fields and then will check the remaining fields against the inclusive filters.- Parameters:
- xContentType- the- XContentType
- includes- the inclusive filters: only fields and objects that match the inclusive filters will be written to the output.
- excludes- the exclusive filters: only fields and objects that don't match the exclusive filters will be written to the output.
- Throws:
- IOException- if an- IOExceptionoccurs while building the content
 
- 
getResponseContentTypeString
- 
contentType
- 
getOutputStream- Returns:
- the output stream to which the built object is being written. Note that is dangerous to modify the stream.
 
- 
prettyPrint
- 
isPrettyPrintpublic boolean isPrettyPrint()
- 
lfAtEndIndicate that the currentXContentBuildermust write a line feed ("\n") at the end of the built object.This only applies for JSON XContent type. It has no effect for other types. 
- 
humanReadableSet the "human readable" flag. Once set, some types of values are written in a format easier to read for a human.
- 
humanReadablepublic boolean humanReadable()- Returns:
- the value of the "human readable" flag. When the value is equal to true, some types of values are written in a format easier to read for a human.
 
- 
objectpublic XContentBuilder object(String name, CheckedConsumer<XContentBuilder, IOException> callback) throws IOException- Throws:
- IOException
 
- 
startObject- Throws:
- IOException
 
- 
startObject- Throws:
- IOException
 
- 
endObject- Throws:
- IOException
 
- 
arraypublic XContentBuilder array(String name, CheckedConsumer<XContentBuilder, IOException> callback) throws IOException- Throws:
- IOException
 
- 
startArray- Throws:
- IOException
 
- 
startArray- Throws:
- IOException
 
- 
endArray- Throws:
- IOException
 
- 
field- Throws:
- IOException
 
- 
nullField- Throws:
- IOException
 
- 
nullValue- Throws:
- IOException
 
- 
field- Throws:
- IOException
 
- 
field- Throws:
- IOException
 
- 
array- Throws:
- IOException
 
- 
value- Throws:
- IOException
 
- 
value- Throws:
- IOException
 
- 
field- Throws:
- IOException
 
- 
field- Throws:
- IOException
 
- 
value- Throws:
- IOException
 
- 
value- Throws:
- IOException
 
- 
array- Throws:
- IOException
 
- 
field- Throws:
- IOException
 
- 
field- Throws:
- IOException
 
- 
array- Throws:
- IOException
 
- 
value- Throws:
- IOException
 
- 
value- Throws:
- IOException
 
- 
field- Throws:
- IOException
 
- 
field- Throws:
- IOException
 
- 
array- Throws:
- IOException
 
- 
value- Throws:
- IOException
 
- 
value- Throws:
- IOException
 
- 
field- Throws:
- IOException
 
- 
field- Throws:
- IOException
 
- 
array- Throws:
- IOException
 
- 
value- Throws:
- IOException
 
- 
value- Throws:
- IOException
 
- 
field- Throws:
- IOException
 
- 
field- Throws:
- IOException
 
- 
array- Throws:
- IOException
 
- 
value- Throws:
- IOException
 
- 
value- Throws:
- IOException
 
- 
field- Throws:
- IOException
 
- 
field- Throws:
- IOException
 
- 
array- Throws:
- IOException
 
- 
value- Throws:
- IOException
 
- 
value- Throws:
- IOException
 
- 
field- Throws:
- IOException
 
- 
array- Throws:
- IOException
 
- 
value- Throws:
- IOException
 
- 
field- Throws:
- IOException
 
- 
array- Throws:
- IOException
 
- 
value- Throws:
- IOException
 
- 
field- Throws:
- IOException
 
- 
array- Throws:
- IOException
 
- 
value- Throws:
- IOException
 
- 
field- Throws:
- IOException
 
- 
value- Throws:
- IOException
 
- 
field- Throws:
- IOException
 
- 
value- Throws:
- IOException
 
- 
utf8ValueWrites the binary content of the given byte array as UTF-8 bytes. UseXContentParser.charBuffer()to read the value back- Throws:
- IOException
 
- 
timestampFieldWrite a field with a timestamp value: if the passed timestamp is null then writes null, otherwise looks up the date transformer for the type oftimestampand uses it to format the value.- Throws:
- IllegalArgumentException- if there is no transformer for the given value type
- IOException
 
- 
timestampFieldsFromUnixEpochMillispublic XContentBuilder timestampFieldsFromUnixEpochMillis(String rawFieldName, String humanReadableFieldName, long unixEpochMillis) throws IOException Writes a field containing the raw number of milliseconds since the unix epoch, and also if thehumanReadableflag is set, writes a formatted representation of this value using the UNIX_EPOCH_MILLIS_FORMATTER.- Throws:
- IOException
 
- 
timestampValueWrite a timestamp value: if the passed timestamp is null then writes null, otherwise looks up the date transformer for the type oftimestampand uses it to format the value.- Throws:
- IllegalArgumentException- if there is no transformer for the given value type
- IOException
 
- 
latlon- Throws:
- IOException
 
- 
latlon- Throws:
- IOException
 
- 
value- Throws:
- IOException
 
- 
field- Throws:
- IOException
 
- 
field- Throws:
- IOException
 
- 
field- Throws:
- IOException
 
- 
field- Throws:
- IOException
 
- 
array- Throws:
- IOException
 
- 
value- Throws:
- IOException
 
- 
field- Throws:
- IOException
 
- 
fieldpublic XContentBuilder field(String name, ToXContent value, ToXContent.Params params) throws IOException - Throws:
- IOException
 
- 
value- Throws:
- IOException
 
- 
value- Throws:
- IOException
 
- 
value- Throws:
- IOException
 
- 
stringListField- Throws:
- IOException
 
- 
xContentListpublic XContentBuilder xContentList(String name, Collection<? extends ToXContent> values) throws IOException - Throws:
- IOException
 
- 
xContentListpublic XContentBuilder xContentList(String name, Collection<? extends ToXContent> values, ToXContent.Params params) throws IOException - Throws:
- IOException
 
- 
xContentList- Throws:
- IOException
 
- 
enumSet- Throws:
- IOException
 
- 
field- Throws:
- IOException
 
- 
map- Throws:
- IOException
 
- 
map- Throws:
- IOException
 
- 
stringStringMap- Throws:
- IOException
 
- 
xContentValuesMappublic XContentBuilder xContentValuesMap(String name, Map<String, ? extends ToXContent> values) throws IOException- Throws:
- IOException
 
- 
mapContentswrites a map without the start object and end object headers- Throws:
- IOException
 
- 
field- Throws:
- IOException
 
- 
humanReadableFieldpublic XContentBuilder humanReadableField(String rawFieldName, String readableFieldName, Object value) throws IOException - Throws:
- IOException
 
- 
percentageFieldpublic XContentBuilder percentageField(String rawFieldName, String readableFieldName, double percentage) throws IOException - Throws:
- IOException
 
- 
field- Throws:
- IOException
 
- 
rawFieldDeprecated.userawField(String, InputStream, XContentType)to avoid content type auto-detectionWrites a raw field with the value taken from the bytes in the stream- Throws:
- IOException
 
- 
rawFieldpublic XContentBuilder rawField(String name, InputStream value, XContentType contentType) throws IOException Writes a raw field with the value taken from the bytes in the stream- Throws:
- IOException
 
- 
rawValueWrites a value with the source coming directly from the bytes in the stream- Throws:
- IOException
 
- 
rawValueWrites a value with the source coming directly from a pre-rendered string representation- Throws:
- IOException
 
- 
copyCurrentEventCopies current event from parser into this builder. The difference withcopyCurrentStructure(XContentParser)is that this method does not copy sub-objects as a single entity.- Parameters:
- parser-
- Throws:
- IOException
 
- 
copyCurrentStructure- Throws:
- IOException
 
- 
directFieldAsBase64public XContentBuilder directFieldAsBase64(String name, CheckedConsumer<OutputStream, IOException> writer) throws IOExceptionWrite the content that is written to the output stream by thewriteras a string encoded in Base64 format. This API can be used to generate XContent directly without the intermediate results to reduce memory usage. Note that this method supports only JSON.- Throws:
- IOException
 
- 
getRestApiVersionReturns a version used for serialising a response.- Returns:
- a compatible version
 
- 
flush- Specified by:
- flushin interface- Flushable
- Throws:
- IOException
 
- 
closepublic void close()- Specified by:
- closein interface- AutoCloseable
- Specified by:
- closein interface- Closeable
 
- 
generator
- 
ensureNameNotNull
- 
ensureNotNull
- 
ensureToXContentableChecks whether the given value is writeable as x-content. If the value cannot be passed tovalue(Object)without error, an errorIllegalArgumentExceptionis thrown.
 
- 
rawField(String, InputStream, XContentType)to avoid content type auto-detection