Package org.elasticsearch.xcontent
Class XContentBuilder
java.lang.Object
org.elasticsearch.xcontent.XContentBuilder
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
A utility to build XContent (ie json).
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Interface for transforming complex objects into their "raw" equivalents for human-readable fieldsstatic interface
-
Constructor Summary
ConstructorDescriptionXContentBuilder
(XContent xContent, OutputStream bos) Constructs a new builder using the provided XContent and an OutputStream.XContentBuilder
(XContent xContent, OutputStream bos, Set<String> includes) Constructs a new builder using the provided XContent, an OutputStream and some filters.XContentBuilder
(XContent xContent, OutputStream os, Set<String> includes, Set<String> excludes) Creates a new builder using the provided XContent, output stream and some inclusive and/or exclusive 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
Create a newXContentBuilder
using the givenXContent
content and some inclusive and/or exclusive filters.void
close()
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) field
(String name, BigDecimal value) field
(String name, BigInteger value) field
(String name, ToXContent value) field
(String name, ToXContent value, ToXContent.Params params) void
flush()
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 streamstartArray
(String name) startObject
(String name) stringListField
(String name, Collection<String> values) stringStringMap
(String name, Map<String, String> values) Write a time-based field and value, if the passed timeValue is null a null value is written, otherwise a date transformers lookup is performed.If thehumanReadable
flag is set, writes both a formatted and unformatted version of the time value using the date transformer for theLong
class.Write a time-based value, if the value is null a null value is written, otherwise a date transformers lookup is performed.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) xContentList
(String name, Collection<? extends ToXContent> values) 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(XContent xContent, 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) 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.- Throws:
IOException
-
-
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, Set<String> includes, Set<String> excludes) throws IOException Create a newXContentBuilder
using the givenXContent
content 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:
xContent
- theXContent
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
-
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
-
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
-
timeField
Write a time-based field and value, if the passed timeValue is null a null value is written, otherwise a date transformers lookup is performed.- Throws:
IllegalArgumentException
- if there is no transformers for the type of objectIOException
-
timeField
If thehumanReadable
flag is set, writes both a formatted and unformatted version of the time value using the date transformer for theLong
class.- Throws:
IOException
-
timeValue
Write a time-based value, if the value is null a null value is written, otherwise a date transformers lookup is performed.- Throws:
IllegalArgumentException
- if there is no transformers for the type of objectIOException
-
latlon
- Throws:
IOException
-
latlon
- Throws:
IOException
-
value
- 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
-
stringListField
- Throws:
IOException
-
xContentList
public XContentBuilder xContentList(String name, Collection<? extends ToXContent> values) throws IOException - Throws:
IOException
-
xContentList
- Throws:
IOException
-
enumSet
- Throws:
IOException
-
field
- 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
-
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
-
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
-
rawField(String, InputStream, XContentType)
to avoid content type auto-detection