java.lang.Object
org.elasticsearch.xcontent.XContentBuilder
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
A utility to build XContent (ie json).
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
Interface for transforming complex objects into their "raw" equivalents for human-readable fieldsstatic interface
-
Constructor Summary
ConstructorsConstructorDescriptionXContentBuilder
(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 Summary
Modifier and TypeMethodDescriptionarray
(String name, BigDecimal[] values) array
(String name, BigInteger[] values) array
(String name, CheckedConsumer<XContentBuilder, IOException> callback) static XContentBuilder
Create a newXContentBuilder
using the givenXContent
content.static XContentBuilder
builder
(XContent xContent, RestApiVersion restApiVersion) Create a newXContentBuilder
using the givenXContent
content and RestApiVersion.static XContentBuilder
Create a newXContentBuilder
using the givenXContentType
xContentType and some inclusive and/or exclusive filters.void
close()
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 thewriter
as a string encoded in Base64 format.endArray()
static void
ensureNameNotNull
(String name) static void
ensureNotNull
(Object value, String message) static void
ensureToXContentable
(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) void
flush()
Returns a version used for serialising a response.boolean
humanReadable
(boolean isHumanReadable) Set the "human readable" flag.humanReadableField
(String rawFieldName, String readableFieldName, Object value) boolean
latlon
(double lat, double lon) lfAtEnd()
Indicate that the currentXContentBuilder
must 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 oftimestamp
and 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 thehumanReadable
flag 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 oftimestamp
and 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
-
XContentBuilder
Constructs a new builder using the provided XContent and an OutputStream. Make sure to callclose()
when the builder is done with.- Throws:
IOException
-
XContentBuilder
public 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
-
XContentBuilder
public 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 streamincludes
- 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
-
XContentBuilder
public 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 streamincludes
- 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 responserestApiVersion
- a rest api version indicating with which version the XContent is compatible with.- Throws:
IOException
- See Also:
-
-
Method Details
-
builder
Create a newXContentBuilder
using the givenXContent
content.The builder uses an internal
ByteArrayOutputStream
output stream to build the content.- Parameters:
xContent
- theXContent
- Returns:
- a new
XContentBuilder
- Throws:
IOException
- if anIOException
occurs while building the content
-
builder
public static XContentBuilder builder(XContent xContent, RestApiVersion restApiVersion) throws IOException Create a newXContentBuilder
using the givenXContent
content and RestApiVersion.The builder uses an internal
ByteArrayOutputStream
output stream to build the content.- Parameters:
xContent
- theXContent
- Returns:
- a new
XContentBuilder
- Throws:
IOException
- if anIOException
occurs while building the content
-
builder
public static XContentBuilder builder(XContentType xContentType, Set<String> includes, Set<String> excludes) throws IOException Create a newXContentBuilder
using the givenXContentType
xContentType and some inclusive and/or exclusive filters.The builder uses an internal
ByteArrayOutputStream
output 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
- theXContentType
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 anIOException
occurs 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
-
isPrettyPrint
public boolean isPrettyPrint() -
lfAtEnd
Indicate that the currentXContentBuilder
must 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.
-
humanReadable
Set the "human readable" flag. Once set, some types of values are written in a format easier to read for a human. -
humanReadable
public 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.
-
object
public XContentBuilder object(String name, CheckedConsumer<XContentBuilder, IOException> callback) throws IOException- Throws:
IOException
-
startObject
- Throws:
IOException
-
startObject
- Throws:
IOException
-
endObject
- Throws:
IOException
-
array
public 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
-
utf8Value
Writes the binary content of the given byte array as UTF-8 bytes. UseXContentParser.charBuffer()
to read the value back- Throws:
IOException
-
timestampField
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 oftimestamp
and uses it to format the value.- Throws:
IllegalArgumentException
- if there is no transformer for the given value typeIOException
-
timestampFieldsFromUnixEpochMillis
public 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 thehumanReadable
flag is set, writes a formatted representation of this value using the UNIX_EPOCH_MILLIS_FORMATTER.- Throws:
IOException
-
timestampValue
Write a timestamp value: if the passed timestamp is null then writes null, otherwise looks up the date transformer for the type oftimestamp
and uses it to format the value.- Throws:
IllegalArgumentException
- if there is no transformer for the given value typeIOException
-
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
-
field
public 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
-
xContentList
public XContentBuilder xContentList(String name, Collection<? extends ToXContent> values) throws IOException - Throws:
IOException
-
xContentList
public 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
-
xContentValuesMap
public XContentBuilder xContentValuesMap(String name, Map<String, ? extends ToXContent> values) throws IOException- Throws:
IOException
-
mapContents
writes a map without the start object and end object headers- Throws:
IOException
-
field
- Throws:
IOException
-
humanReadableField
public XContentBuilder humanReadableField(String rawFieldName, String readableFieldName, Object value) throws IOException - Throws:
IOException
-
percentageField
public XContentBuilder percentageField(String rawFieldName, String readableFieldName, double percentage) throws IOException - Throws:
IOException
-
field
- Throws:
IOException
-
rawField
Deprecated.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
-
rawField
public 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
-
rawValue
Writes a value with the source coming directly from the bytes in the stream- Throws:
IOException
-
rawValue
Writes a value with the source coming directly from a pre-rendered string representation- Throws:
IOException
-
copyCurrentEvent
Copies 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
-
directFieldAsBase64
public XContentBuilder directFieldAsBase64(String name, CheckedConsumer<OutputStream, IOException> writer) throws IOExceptionWrite the content that is written to the output stream by thewriter
as 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
-
getRestApiVersion
Returns a version used for serialising a response.- Returns:
- a compatible version
-
flush
- Specified by:
flush
in interfaceFlushable
- Throws:
IOException
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
generator
-
ensureNameNotNull
-
ensureNotNull
-
ensureToXContentable
Checks whether the given value is writeable as x-content. If the value cannot be passed tovalue(Object)
without error, an errorIllegalArgumentException
is thrown.
-
rawField(String, InputStream, XContentType)
to avoid content type auto-detection