A content processing library for Elasticsearch that provides abstractions for parsing and generating various content formats including JSON, YAML, CBOR, and Smile.
—
The X-Content library provides both high-level fluent builders and low-level streaming generators for creating structured content in multiple formats (JSON, YAML, CBOR, SMILE).
Central factory methods for creating content builders for different formats.
/**
* Creates a JSON content builder
* @return XContentBuilder configured for JSON output
*/
public static XContentBuilder jsonBuilder() throws IOException;
/**
* Creates a JSON content builder with specific output stream
* @param os the output stream to write to
* @return XContentBuilder configured for JSON output
*/
public static XContentBuilder jsonBuilder(OutputStream os) throws IOException;
/**
* Creates a SMILE content builder (binary JSON format)
* @return XContentBuilder configured for SMILE output
*/
public static XContentBuilder smileBuilder() throws IOException;
/**
* Creates a YAML content builder
* @return XContentBuilder configured for YAML output
*/
public static XContentBuilder yamlBuilder() throws IOException;
/**
* Creates a CBOR content builder
* @return XContentBuilder configured for CBOR output
*/
public static XContentBuilder cborBuilder() throws IOException;
/**
* Creates a content builder for the specified type
* @param type the content type to create builder for
* @return XContentBuilder configured for the specified type
*/
public static XContentBuilder contentBuilder(XContentType type) throws IOException;
/**
* Gets the XContent instance for the specified type
* @param type the content type
* @return XContent instance for the type
*/
public static XContent xContent(XContentType type);High-level fluent builder for creating structured content with method chaining.
public final class XContentBuilder implements Closeable, Flushable {
/**
* Start a new object in the content
* @return this builder for chaining
*/
public XContentBuilder startObject() throws IOException;
/**
* Start a new object with a field name
* @param name the field name for the object
* @return this builder for chaining
*/
public XContentBuilder startObject(String name) throws IOException;
/**
* End the current object
* @return this builder for chaining
*/
public XContentBuilder endObject() throws IOException;
/**
* Start a new array in the content
* @return this builder for chaining
*/
public XContentBuilder startArray() throws IOException;
/**
* Start a new array with a field name
* @param name the field name for the array
* @return this builder for chaining
*/
public XContentBuilder startArray(String name) throws IOException;
/**
* End the current array
* @return this builder for chaining
*/
public XContentBuilder endArray() throws IOException;
/**
* Write a field name
* @param name the field name
* @return this builder for chaining
*/
public XContentBuilder field(String name) throws IOException;
/**
* Write a string value
* @param value the string value
* @return this builder for chaining
*/
public XContentBuilder value(String value) throws IOException;
/**
* Write an integer value
* @param value the integer value
* @return this builder for chaining
*/
public XContentBuilder value(int value) throws IOException;
/**
* Write a long value
* @param value the long value
* @return this builder for chaining
*/
public XContentBuilder value(long value) throws IOException;
/**
* Write a double value
* @param value the double value
* @return this builder for chaining
*/
public XContentBuilder value(double value) throws IOException;
/**
* Write a boolean value
* @param value the boolean value
* @return this builder for chaining
*/
public XContentBuilder value(boolean value) throws IOException;
/**
* Write a binary value (byte array)
* @param value the byte array
* @return this builder for chaining
*/
public XContentBuilder value(byte[] value) throws IOException;
/**
* Write a null value
* @return this builder for chaining
*/
public XContentBuilder nullValue() throws IOException;
/**
* Write a field with a string value
* @param name the field name
* @param value the string value
* @return this builder for chaining
*/
public XContentBuilder field(String name, String value) throws IOException;
/**
* Write a field with an integer value
* @param name the field name
* @param value the integer value
* @return this builder for chaining
*/
public XContentBuilder field(String name, int value) throws IOException;
/**
* Write a field with a long value
* @param name the field name
* @param value the long value
* @return this builder for chaining
*/
public XContentBuilder field(String name, long value) throws IOException;
/**
* Write a field with a double value
* @param name the field name
* @param value the double value
* @return this builder for chaining
*/
public XContentBuilder field(String name, double value) throws IOException;
/**
* Write a field with a boolean value
* @param name the field name
* @param value the boolean value
* @return this builder for chaining
*/
public XContentBuilder field(String name, boolean value) throws IOException;
/**
* Write a field with a binary value
* @param name the field name
* @param value the byte array
* @return this builder for chaining
*/
public XContentBuilder field(String name, byte[] value) throws IOException;
/**
* Write a null field
* @param name the field name
* @return this builder for chaining
*/
public XContentBuilder nullField(String name) throws IOException;
/**
* Write a field with any object value (uses ToXContent if available)
* @param name the field name
* @param value the object value
* @return this builder for chaining
*/
public XContentBuilder field(String name, Object value) throws IOException;
/**
* Write raw content from an input stream
* @param name the field name
* @param value the input stream containing raw content
* @param contentType the type of the raw content
* @return this builder for chaining
*/
public XContentBuilder rawField(String name, InputStream value, XContentType contentType) throws IOException;
/**
* Get the string representation of the built content
* @return string representation of the content
*/
public String toString();
/**
* Get the content as a byte array
* @return byte array representation of the content
*/
public byte[] bytes();
}Low-level streaming interface for generating content with fine-grained control.
public interface XContentGenerator extends Closeable, Flushable {
/**
* Get the content type this generator produces
* @return the content type
*/
XContentType contentType();
/**
* Enable pretty printing for human-readable output
*/
void usePrettyPrint();
/**
* Check if pretty printing is enabled
* @return true if pretty printing is enabled
*/
boolean isPrettyPrint();
/**
* Enable line feed at the end of output
*/
void usePrintLineFeedAtEnd();
/**
* Write the start of an object
*/
void writeStartObject() throws IOException;
/**
* Write the end of an object
*/
void writeEndObject() throws IOException;
/**
* Write the start of an array
*/
void writeStartArray() throws IOException;
/**
* Write the end of an array
*/
void writeEndArray() throws IOException;
/**
* Write a field name
* @param name the field name
*/
void writeFieldName(String name) throws IOException;
/**
* Write a null value
*/
void writeNull() throws IOException;
/**
* Write a null field
* @param name the field name
*/
void writeNullField(String name) throws IOException;
/**
* Write a boolean field
* @param name the field name
* @param value the boolean value
*/
void writeBooleanField(String name, boolean value) throws IOException;
/**
* Write a boolean value
* @param value the boolean value
*/
void writeBoolean(boolean value) throws IOException;
/**
* Write a number field (double)
* @param name the field name
* @param value the double value
*/
void writeNumberField(String name, double value) throws IOException;
/**
* Write a number field (int)
* @param name the field name
* @param value the integer value
*/
void writeNumberField(String name, int value) throws IOException;
/**
* Write a number field (long)
* @param name the field name
* @param value the long value
*/
void writeNumberField(String name, long value) throws IOException;
/**
* Write a string field
* @param name the field name
* @param value the string value
*/
void writeStringField(String name, String value) throws IOException;
/**
* Write a string value
* @param value the string value
*/
void writeString(String value) throws IOException;
/**
* Write a binary field
* @param name the field name
* @param value the byte array
*/
void writeBinaryField(String name, byte[] value) throws IOException;
/**
* Write a binary value
* @param value the byte array
*/
void writeBinary(byte[] value) throws IOException;
/**
* Write raw content from an input stream
* @param name the field name
* @param value the input stream
* @param contentType the content type
*/
void writeRawField(String name, InputStream value, XContentType contentType) throws IOException;
/**
* Copy the current structure from a parser
* @param parser the parser to copy from
*/
void copyCurrentStructure(XContentParser parser) throws IOException;
}Usage Examples:
import org.elasticsearch.xcontent.*;
import static org.elasticsearch.xcontent.XContentFactory.*;
// Basic object creation
XContentBuilder builder = jsonBuilder()
.startObject()
.field("name", "John Doe")
.field("age", 30)
.field("active", true)
.nullField("nickname")
.endObject();
// Array creation
XContentBuilder arrayBuilder = jsonBuilder()
.startObject()
.field("user", "john")
.startArray("roles")
.value("admin")
.value("editor")
.endArray()
.endObject();
// Nested objects
XContentBuilder nestedBuilder = jsonBuilder()
.startObject()
.field("name", "John")
.startObject("address")
.field("street", "123 Main St")
.field("city", "New York")
.endObject()
.endObject();
// Complex data structure
XContentBuilder complexBuilder = jsonBuilder()
.startObject()
.field("timestamp", System.currentTimeMillis())
.startObject("user")
.field("id", 123)
.field("email", "john@example.com")
.startArray("preferences")
.startObject()
.field("key", "theme")
.field("value", "dark")
.endObject()
.endArray()
.endObject()
.endObject();
// Different formats
XContentBuilder yamlBuilder = yamlBuilder()
.startObject()
.field("message", "Hello World")
.endObject();
XContentBuilder smileBuilder = smileBuilder() // Binary format
.startObject()
.field("data", "compressed")
.endObject();public interface XContentBuilderExtension {
/**
* Write custom content to the builder
* @param builder the builder to write to
* @param params parameters for the operation
*/
void writeTo(XContentBuilder builder, ToXContent.Params params) throws IOException;
}
public interface Writer {
/**
* Write a value to the generator
* @param generator the generator to write to
* @param value the value to write
*/
void write(XContentGenerator generator, Object value) throws IOException;
}Install with Tessl CLI
npx tessl i tessl/maven-org-elasticsearch--elasticsearch-x-content