Class XContentBuilder
java.lang.Object
org.elasticsearch.common.xcontent.XContentBuilder
- All Implemented Interfaces:
java.io.Closeable,java.io.Flushable,java.lang.AutoCloseable
public final class XContentBuilder
extends java.lang.Object
implements java.io.Closeable, java.io.Flushable
A utility to build XContent (ie json).
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceXContentBuilder.HumanReadableTransformerInterface for transforming complex objects into their "raw" equivalents for human-readable fieldsstatic interfaceXContentBuilder.Writer -
Constructor Summary
Constructors Constructor Description XContentBuilder(XContent xContent, java.io.OutputStream bos)Constructs a new builder using the provided XContent and an OutputStream.XContentBuilder(XContent xContent, java.io.OutputStream bos, java.util.Set<java.lang.String> includes)Constructs a new builder using the provided XContent, an OutputStream and some filters.XContentBuilder(XContent xContent, java.io.OutputStream os, java.util.Set<java.lang.String> includes, java.util.Set<java.lang.String> excludes)Creates a new builder using the provided XContent, output stream and some inclusive and/or exclusive filters. -
Method Summary
Modifier and Type Method Description XContentBuilderarray(java.lang.String name, boolean[] values)XContentBuilderarray(java.lang.String name, double[] values)XContentBuilderarray(java.lang.String name, float[] values)XContentBuilderarray(java.lang.String name, int[] values)XContentBuilderarray(java.lang.String name, long[] values)XContentBuilderarray(java.lang.String name, short[] values)XContentBuilderarray(java.lang.String name, java.lang.Object... values)XContentBuilderarray(java.lang.String name, java.lang.String... values)XContentBuilderarray(java.lang.String name, java.math.BigDecimal[] values)XContentBuilderarray(java.lang.String name, java.math.BigInteger[] values)static XContentBuilderbuilder(XContent xContent)Create a newXContentBuilderusing the givenXContentcontent.static XContentBuilderbuilder(XContent xContent, java.util.Set<java.lang.String> includes, java.util.Set<java.lang.String> excludes)Create a newXContentBuilderusing the givenXContentcontent and some inclusive and/or exclusive filters.voidclose()XContentTypecontentType()XContentBuildercopyCurrentStructure(XContentParser parser)XContentBuilderendArray()XContentBuilderendObject()XContentBuilderfield(java.lang.String name)XContentBuilderfield(java.lang.String name, boolean value)XContentBuilderfield(java.lang.String name, byte value)XContentBuilderfield(java.lang.String name, byte[] value)XContentBuilderfield(java.lang.String name, byte[] value, int offset, int length)XContentBuilderfield(java.lang.String name, double value)XContentBuilderfield(java.lang.String name, float value)XContentBuilderfield(java.lang.String name, int value)XContentBuilderfield(java.lang.String name, long value)XContentBuilderfield(java.lang.String name, short value)XContentBuilderfield(java.lang.String name, java.lang.Boolean value)XContentBuilderfield(java.lang.String name, java.lang.Byte value)XContentBuilderfield(java.lang.String name, java.lang.Double value)XContentBuilderfield(java.lang.String name, java.lang.Float value)XContentBuilderfield(java.lang.String name, java.lang.Integer value)XContentBuilderfield(java.lang.String name, java.lang.Iterable<?> values)XContentBuilderfield(java.lang.String name, java.lang.Long value)XContentBuilderfield(java.lang.String name, java.lang.Object value)XContentBuilderfield(java.lang.String name, java.lang.Short value)XContentBuilderfield(java.lang.String name, java.lang.String value)XContentBuilderfield(java.lang.String name, java.math.BigDecimal value)XContentBuilderfield(java.lang.String name, java.math.BigInteger value)XContentBuilderfield(java.lang.String name, java.util.Map<java.lang.String,java.lang.Object> values)XContentBuilderfield(java.lang.String name, ToXContent value)XContentBuilderfield(java.lang.String name, ToXContent value, ToXContent.Params params)voidflush()XContentGeneratorgenerator()java.io.OutputStreamgetOutputStream()booleanhumanReadable()XContentBuilderhumanReadable(boolean humanReadable)Set the "human readable" flag.XContentBuilderhumanReadableField(java.lang.String rawFieldName, java.lang.String readableFieldName, java.lang.Object value)booleanisPrettyPrint()XContentBuilderlatlon(double lat, double lon)XContentBuilderlatlon(java.lang.String name, double lat, double lon)XContentBuilderlfAtEnd()Indicate that the currentXContentBuildermust write a line feed ("\n") at the end of the built object.XContentBuildermap(java.util.Map<java.lang.String,?> values)XContentBuildermapContents(java.util.Map<java.lang.String,?> values)writes a map without the start object and end object headersXContentBuildernullField(java.lang.String name)XContentBuildernullValue()XContentBuilderpercentageField(java.lang.String rawFieldName, java.lang.String readableFieldName, double percentage)XContentBuilderprettyPrint()XContentBuilderrawField(java.lang.String name, java.io.InputStream value)Deprecated.XContentBuilderrawField(java.lang.String name, java.io.InputStream value, XContentType contentType)Writes a raw field with the value taken from the bytes in the streamXContentBuilderrawValue(java.io.InputStream stream, XContentType contentType)Writes a value with the source coming directly from the bytes in the streamXContentBuilderstartArray()XContentBuilderstartArray(java.lang.String name)XContentBuilderstartObject()XContentBuilderstartObject(java.lang.String name)XContentBuildertimeField(java.lang.String name, java.lang.Object timeValue)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.XContentBuildertimeField(java.lang.String name, java.lang.String readableName, long value)If thehumanReadableflag is set, writes both a formatted and unformatted version of the time value using the date transformer for theLongclass.XContentBuildertimeValue(java.lang.Object timeValue)Write a time-based value, if the value is null a null value is written, otherwise a date transformers lookup is performed.XContentBuilderutf8Value(byte[] bytes, int offset, int length)Writes the binary content of the given byte array as UTF-8 bytes.XContentBuildervalue(boolean value)XContentBuildervalue(byte value)XContentBuildervalue(byte[] value)XContentBuildervalue(byte[] value, int offset, int length)XContentBuildervalue(double value)XContentBuildervalue(float value)XContentBuildervalue(int value)XContentBuildervalue(long value)XContentBuildervalue(short value)XContentBuildervalue(java.lang.Boolean value)XContentBuildervalue(java.lang.Byte value)XContentBuildervalue(java.lang.Double value)XContentBuildervalue(java.lang.Float value)XContentBuildervalue(java.lang.Integer value)XContentBuildervalue(java.lang.Long value)XContentBuildervalue(java.lang.Object value)XContentBuildervalue(java.lang.Short value)XContentBuildervalue(java.lang.String value)XContentBuildervalue(java.math.BigDecimal value)XContentBuildervalue(java.math.BigInteger value)XContentBuildervalue(java.nio.file.Path value)Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
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:
java.io.IOException
-
XContentBuilder
public XContentBuilder(XContent xContent, java.io.OutputStream bos, java.util.Set<java.lang.String> includes) throws java.io.IOExceptionConstructs 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:
java.io.IOException
-
XContentBuilder
public XContentBuilder(XContent xContent, java.io.OutputStream os, java.util.Set<java.lang.String> includes, java.util.Set<java.lang.String> excludes) throws java.io.IOExceptionCreates 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:
java.io.IOException
-
-
Method Details
-
builder
Create a newXContentBuilderusing the givenXContentcontent.The builder uses an internal
ByteArrayOutputStreamoutput stream to build the content.- Parameters:
xContent- theXContent- Returns:
- a new
XContentBuilder - Throws:
java.io.IOException- if anIOExceptionoccurs while building the content
-
builder
public static XContentBuilder builder(XContent xContent, java.util.Set<java.lang.String> includes, java.util.Set<java.lang.String> excludes) throws java.io.IOExceptionCreate a newXContentBuilderusing the givenXContentcontent 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:
xContent- theXContentincludes- 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:
java.io.IOException- if anIOExceptionoccurs while building the content
-
contentType
-
getOutputStream
public java.io.OutputStream 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 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.
-
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.
-
startObject
- Throws:
java.io.IOException
-
startObject
- Throws:
java.io.IOException
-
endObject
- Throws:
java.io.IOException
-
startArray
- Throws:
java.io.IOException
-
startArray
- Throws:
java.io.IOException
-
endArray
- Throws:
java.io.IOException
-
field
- Throws:
java.io.IOException
-
nullField
- Throws:
java.io.IOException
-
nullValue
- Throws:
java.io.IOException
-
field
public XContentBuilder field(java.lang.String name, java.lang.Boolean value) throws java.io.IOException- Throws:
java.io.IOException
-
field
- Throws:
java.io.IOException
-
array
- Throws:
java.io.IOException
-
value
- Throws:
java.io.IOException
-
value
- Throws:
java.io.IOException
-
field
public XContentBuilder field(java.lang.String name, java.lang.Byte value) throws java.io.IOException- Throws:
java.io.IOException
-
field
- Throws:
java.io.IOException
-
value
- Throws:
java.io.IOException
-
value
- Throws:
java.io.IOException
-
field
public XContentBuilder field(java.lang.String name, java.lang.Double value) throws java.io.IOException- Throws:
java.io.IOException
-
field
- Throws:
java.io.IOException
-
array
- Throws:
java.io.IOException
-
value
- Throws:
java.io.IOException
-
value
- Throws:
java.io.IOException
-
field
public XContentBuilder field(java.lang.String name, java.lang.Float value) throws java.io.IOException- Throws:
java.io.IOException
-
field
- Throws:
java.io.IOException
-
array
- Throws:
java.io.IOException
-
value
- Throws:
java.io.IOException
-
value
- Throws:
java.io.IOException
-
field
public XContentBuilder field(java.lang.String name, java.lang.Integer value) throws java.io.IOException- Throws:
java.io.IOException
-
field
- Throws:
java.io.IOException
-
array
- Throws:
java.io.IOException
-
value
- Throws:
java.io.IOException
-
value
- Throws:
java.io.IOException
-
field
public XContentBuilder field(java.lang.String name, java.lang.Long value) throws java.io.IOException- Throws:
java.io.IOException
-
field
- Throws:
java.io.IOException
-
array
- Throws:
java.io.IOException
-
value
- Throws:
java.io.IOException
-
value
- Throws:
java.io.IOException
-
field
public XContentBuilder field(java.lang.String name, java.lang.Short value) throws java.io.IOException- Throws:
java.io.IOException
-
field
- Throws:
java.io.IOException
-
array
- Throws:
java.io.IOException
-
value
- Throws:
java.io.IOException
-
value
- Throws:
java.io.IOException
-
field
public XContentBuilder field(java.lang.String name, java.math.BigInteger value) throws java.io.IOException- Throws:
java.io.IOException
-
array
public XContentBuilder array(java.lang.String name, java.math.BigInteger[] values) throws java.io.IOException- Throws:
java.io.IOException
-
value
- Throws:
java.io.IOException
-
field
public XContentBuilder field(java.lang.String name, java.math.BigDecimal value) throws java.io.IOException- Throws:
java.io.IOException
-
array
public XContentBuilder array(java.lang.String name, java.math.BigDecimal[] values) throws java.io.IOException- Throws:
java.io.IOException
-
value
- Throws:
java.io.IOException
-
field
public XContentBuilder field(java.lang.String name, java.lang.String value) throws java.io.IOException- Throws:
java.io.IOException
-
array
public XContentBuilder array(java.lang.String name, java.lang.String... values) throws java.io.IOException- Throws:
java.io.IOException
-
value
- Throws:
java.io.IOException
-
field
- Throws:
java.io.IOException
-
value
- Throws:
java.io.IOException
-
field
public XContentBuilder field(java.lang.String name, byte[] value, int offset, int length) throws java.io.IOException- Throws:
java.io.IOException
-
value
- Throws:
java.io.IOException
-
utf8Value
Writes the binary content of the given byte array as UTF-8 bytes. UseXContentParser.charBuffer()to read the value back- Throws:
java.io.IOException
-
timeField
public XContentBuilder timeField(java.lang.String name, java.lang.Object timeValue) throws java.io.IOExceptionWrite 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:
java.lang.IllegalArgumentException- if there is no transformers for the type of objectjava.io.IOException
-
timeField
public XContentBuilder timeField(java.lang.String name, java.lang.String readableName, long value) throws java.io.IOExceptionIf thehumanReadableflag is set, writes both a formatted and unformatted version of the time value using the date transformer for theLongclass.- Throws:
java.io.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:
java.lang.IllegalArgumentException- if there is no transformers for the type of objectjava.io.IOException
-
latlon
public XContentBuilder latlon(java.lang.String name, double lat, double lon) throws java.io.IOException- Throws:
java.io.IOException
-
latlon
- Throws:
java.io.IOException
-
value
- Throws:
java.io.IOException
-
field
public XContentBuilder field(java.lang.String name, java.lang.Object value) throws java.io.IOException- Throws:
java.io.IOException
-
array
public XContentBuilder array(java.lang.String name, java.lang.Object... values) throws java.io.IOException- Throws:
java.io.IOException
-
value
- Throws:
java.io.IOException
-
field
- Throws:
java.io.IOException
-
field
public XContentBuilder field(java.lang.String name, ToXContent value, ToXContent.Params params) throws java.io.IOException- Throws:
java.io.IOException
-
field
public XContentBuilder field(java.lang.String name, java.util.Map<java.lang.String,java.lang.Object> values) throws java.io.IOException- Throws:
java.io.IOException
-
map
- Throws:
java.io.IOException
-
mapContents
public XContentBuilder mapContents(java.util.Map<java.lang.String,?> values) throws java.io.IOExceptionwrites a map without the start object and end object headers- Throws:
java.io.IOException
-
field
public XContentBuilder field(java.lang.String name, java.lang.Iterable<?> values) throws java.io.IOException- Throws:
java.io.IOException
-
humanReadableField
public XContentBuilder humanReadableField(java.lang.String rawFieldName, java.lang.String readableFieldName, java.lang.Object value) throws java.io.IOException- Throws:
java.io.IOException
-
percentageField
public XContentBuilder percentageField(java.lang.String rawFieldName, java.lang.String readableFieldName, double percentage) throws java.io.IOException- Throws:
java.io.IOException
-
rawField
@Deprecated public XContentBuilder rawField(java.lang.String name, java.io.InputStream value) throws java.io.IOExceptionDeprecated.userawField(String, InputStream, XContentType)to avoid content type auto-detectionWrites a raw field with the value taken from the bytes in the stream- Throws:
java.io.IOException
-
rawField
public XContentBuilder rawField(java.lang.String name, java.io.InputStream value, XContentType contentType) throws java.io.IOExceptionWrites a raw field with the value taken from the bytes in the stream- Throws:
java.io.IOException
-
rawValue
public XContentBuilder rawValue(java.io.InputStream stream, XContentType contentType) throws java.io.IOExceptionWrites a value with the source coming directly from the bytes in the stream- Throws:
java.io.IOException
-
copyCurrentStructure
- Throws:
java.io.IOException
-
flush
public void flush() throws java.io.IOException- Specified by:
flushin interfacejava.io.Flushable- Throws:
java.io.IOException
-
close
public void close()- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable
-
generator
-
rawField(String, InputStream, XContentType)to avoid content type auto-detection