CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-elasticsearch--elasticsearch-x-content

A content processing library for Elasticsearch that provides abstractions for parsing and generating various content formats including JSON, YAML, CBOR, and Smile.

Pending
Overview
Eval results
Files

content-types.mddocs/

Content Types and Factory

The X-Content library provides a unified type system and factory for working with different content formats including JSON, YAML, CBOR, and SMILE with consistent APIs across all formats.

Capabilities

XContentType Enumeration

Central enumeration defining all supported content types and their properties.

/**
 * Enumeration of supported content types
 */
public enum XContentType implements MediaType {
    
    /**
     * Standard JSON format
     */
    JSON {
        @Override
        public String mediaTypeWithoutParameters() { return "application/json"; }
        
        @Override
        public String mediaType() { return "application/json;charset=utf-8"; }
        
        @Override
        public String queryParameter() { return "json"; }
        
        @Override
        public Set<HeaderValue> headerValues() {
            return Set.of(new HeaderValue("application/json"), 
                         new HeaderValue("application/x-ndjson"), 
                         new HeaderValue("application/*"));
        }
    },
    
    /**
     * SMILE binary JSON format (fast and compact)
     */
    SMILE {
        @Override
        public String mediaTypeWithoutParameters() { return "application/smile"; }
        
        @Override
        public String mediaType() { return "application/smile"; }
        
        @Override
        public String queryParameter() { return "smile"; }
        
        @Override
        public Set<HeaderValue> headerValues() {
            return Set.of(new HeaderValue("application/smile"));
        }
    },
    
    /**
     * YAML format
     */
    YAML {
        @Override
        public String mediaTypeWithoutParameters() { return "application/yaml"; }
        
        @Override
        public String mediaType() { return "application/yaml"; }
        
        @Override
        public String queryParameter() { return "yaml"; }
        
        @Override
        public Set<HeaderValue> headerValues() {
            return Set.of(new HeaderValue("application/yaml"), 
                         new HeaderValue("text/yaml"));
        }
    },
    
    /**
     * CBOR (Concise Binary Object Representation) format
     */
    CBOR {
        @Override
        public String mediaTypeWithoutParameters() { return "application/cbor"; }
        
        @Override
        public String mediaType() { return "application/cbor"; }
        
        @Override
        public String queryParameter() { return "cbor"; }
        
        @Override
        public Set<HeaderValue> headerValues() {
            return Set.of(new HeaderValue("application/cbor"));
        }
    },
    
    /**
     * Versioned JSON format
     */
    VND_JSON,
    
    /**
     * Versioned SMILE format
     */
    VND_SMILE,
    
    /**
     * Versioned YAML format
     */
    VND_YAML,
    
    /**
     * Versioned CBOR format
     */
    VND_CBOR;
    
    /**
     * Parse content type from format string
     * @param format format string (e.g., "json", "yaml")
     * @return corresponding XContentType
     */
    public static XContentType fromFormat(String format);
    
    /**
     * Parse content type from HTTP media type header
     * @param mediaTypeHeaderValue media type header value
     * @return corresponding XContentType, or null if not recognized
     */
    public static XContentType fromMediaType(String mediaTypeHeaderValue);
    
    /**
     * Get the XContent instance for this type
     * @return XContent implementation for this type
     */
    public XContent xContent();
    
    /**
     * Get the canonical (non-versioned) type
     * @return canonical XContentType
     */
    public XContentType canonical();
    
    /**
     * Get media type without parameters
     * @return media type string
     */
    public abstract String mediaTypeWithoutParameters();
    
    /**
     * Get full media type with parameters
     * @return full media type string
     */
    public abstract String mediaType();
    
    /**
     * Get query parameter name for this type
     * @return query parameter string
     */
    public abstract String queryParameter();
    
    /**
     * Get acceptable header values for this type
     * @return set of acceptable header values
     */
    public abstract Set<HeaderValue> headerValues();
}

XContent Interface

Abstract interface for content format implementations.

/**
 * Generic abstraction for content handling inspired by JSON and pull parsing
 */
public interface XContent {
    
    /**
     * Get the type this content handles and produces
     * @return XContentType for this implementation
     */
    XContentType type();
    
    /**
     * Get the bulk separator byte for this format
     * @return bulk separator byte
     */
    byte bulkSeparator();
    
    /**
     * Detect if the given bytes represent this content type
     * @param bytes byte array to check
     * @param offset starting offset
     * @param length number of bytes to check  
     * @return true if bytes match this content type
     * @deprecated Use MediaTypeRegistry for content detection
     */
    @Deprecated
    boolean detectContent(byte[] bytes, int offset, int length);
    
    /**
     * Detect if the given character sequence represents this content type
     * @param chars character sequence to check
     * @return true if chars match this content type
     * @deprecated Use MediaTypeRegistry for content detection
     */
    @Deprecated  
    boolean detectContent(CharSequence chars);
    
    /**
     * Create a generator using the provided output stream
     * @param os output stream to write to
     * @return XContentGenerator for this format
     */
    default XContentGenerator createGenerator(OutputStream os) throws IOException {
        return createGenerator(os, Collections.emptySet(), Collections.emptySet());
    }
    
    /**
     * Create a generator with include/exclude filtering
     * @param os output stream to write to
     * @param includes field patterns to include (empty means include all)
     * @param excludes field patterns to exclude
     * @return XContentGenerator with filtering
     */
    XContentGenerator createGenerator(OutputStream os, Set<String> includes, Set<String> excludes) throws IOException;
    
    /**
     * Create parser from string content
     * @param config parser configuration
     * @param content string content to parse
     * @return XContentParser for this format
     */
    XContentParser createParser(XContentParserConfiguration config, String content) throws IOException;
    
    /**
     * Create parser from input stream
     * @param config parser configuration
     * @param is input stream to parse
     * @return XContentParser for this format
     */
    XContentParser createParser(XContentParserConfiguration config, InputStream is) throws IOException;
    
    /**
     * Create parser from byte array
     * @param config parser configuration
     * @param data byte array to parse
     * @return XContentParser for this format
     */
    default XContentParser createParser(XContentParserConfiguration config, byte[] data) throws IOException {
        return createParser(config, data, 0, data.length);
    }
    
    /**
     * Create parser from byte array with offset and length
     * @param config parser configuration
     * @param data byte array to parse
     * @param offset starting offset
     * @param length number of bytes to parse
     * @return XContentParser for this format
     */
    XContentParser createParser(XContentParserConfiguration config, byte[] data, int offset, int length) 
        throws IOException;
    
    /**
     * Create parser from Reader
     * @param config parser configuration
     * @param reader Reader to parse from
     * @return XContentParser for this format
     */
    XContentParser createParser(XContentParserConfiguration config, Reader reader) throws IOException;
}

Format-Specific Implementations

Individual format implementations with their specific characteristics.

/**
 * JSON format implementation
 */
public final class JsonXContent implements XContent {
    /**
     * Singleton JSON XContent instance
     */
    public static final XContent jsonXContent = new JsonXContent();
    
    /**
     * Create a JSON content builder
     * @return XContentBuilder for JSON
     */
    public static XContentBuilder contentBuilder() throws IOException;
}

/**
 * YAML format implementation  
 */
public final class YamlXContent implements XContent {
    /**
     * Singleton YAML XContent instance
     */
    public static final XContent yamlXContent = new YamlXContent();
    
    /**
     * Create a YAML content builder
     * @return XContentBuilder for YAML
     */
    public static XContentBuilder contentBuilder() throws IOException;
}

/**
 * SMILE binary format implementation
 */
public final class SmileXContent implements XContent {
    /**
     * Singleton SMILE XContent instance
     */
    public static final XContent smileXContent = new SmileXContent();
    
    /**
     * Create a SMILE content builder
     * @return XContentBuilder for SMILE
     */
    public static XContentBuilder contentBuilder() throws IOException;
}

/**
 * CBOR format implementation
 */
public final class CborXContent implements XContent {
    /**
     * Singleton CBOR XContent instance
     */
    public static final XContent cborXContent = new CborXContent();
    
    /**
     * Create a CBOR content builder
     * @return XContentBuilder for CBOR
     */
    public static XContentBuilder contentBuilder() throws IOException;
}

Usage Examples:

import org.elasticsearch.xcontent.*;
import static org.elasticsearch.xcontent.XContentFactory.*;

// Working with different content types
XContentType jsonType = XContentType.JSON;
XContentType yamlType = XContentType.YAML;
XContentType smileType = XContentType.SMILE;
XContentType cborType = XContentType.CBOR;

// Get XContent instances
XContent jsonContent = jsonType.xContent();
XContent yamlContent = yamlType.xContent();

// Create builders for different formats
XContentBuilder jsonBuilder = jsonBuilder()
    .startObject()
        .field("message", "Hello JSON")
    .endObject();

XContentBuilder yamlBuilder = yamlBuilder()
    .startObject()
        .field("message", "Hello YAML")
    .endObject();

XContentBuilder smileBuilder = smileBuilder()  // Binary format
    .startObject()
        .field("message", "Hello SMILE")
    .endObject();

// Parse content in different formats
String jsonString = """{"name": "John", "age": 30}""";
XContentParser jsonParser = XContentType.JSON.xContent()
    .createParser(XContentParserConfiguration.EMPTY, jsonString);

String yamlString = """
name: John
age: 30
""";
XContentParser yamlParser = XContentType.YAML.xContent()
    .createParser(XContentParserConfiguration.EMPTY, yamlString);

// Content type detection from media type
XContentType detectedType = XContentType.fromMediaType("application/json");
assert detectedType == XContentType.JSON;

XContentType yamlDetected = XContentType.fromMediaType("application/yaml");
assert yamlDetected == XContentType.YAML;

// Content type from format string
XContentType fromFormat = XContentType.fromFormat("json");
assert fromFormat == XContentType.JSON;

// Get media type properties
String mediaType = XContentType.JSON.mediaType();  // "application/json;charset=utf-8"
String queryParam = XContentType.JSON.queryParameter();  // "json"
Set<MediaType.HeaderValue> headers = XContentType.JSON.headerValues();

// Working with canonical types
XContentType canonical = XContentType.VND_JSON.canonical();  // Returns JSON

MediaType Interface

Base interface for media type handling.

/**
 * Interface for HTTP media type support
 */
public interface MediaType {
    
    /**
     * Header value record for media type headers
     */
    public record HeaderValue(String value, Map<String, String> parameters) {
        /**
         * Simple header value without parameters
         * @param value the header value
         */
        public HeaderValue(String value) {
            this(value, Collections.emptyMap());
        }
    }
    
    /**
     * Get the query parameter name for this media type
     * @return query parameter string
     */
    String queryParameter();
    
    /**
     * Get acceptable header values for this media type
     * @return set of header values
     */
    Set<HeaderValue> headerValues();
}

Content Detection and Registry

Media type detection and registry functionality.

/**
 * Registry for media type detection and handling
 */
public class MediaTypeRegistry {
    
    /**
     * Parse media type from string
     * @param mediaType media type string
     * @return ParsedMediaType instance
     */
    public static ParsedMediaType parseMediaType(String mediaType);
    
    /**
     * Get content type from parsed media type
     * @param parsedMediaType parsed media type
     * @return corresponding XContentType, or null if not supported
     */
    public static XContentType fromParsedMediaType(ParsedMediaType parsedMediaType);
}

/**
 * Parsed representation of a media type
 */
public class ParsedMediaType {
    
    /**
     * Get the media type
     * @return media type string
     */
    public String mediaType();
    
    /**
     * Get media type parameters
     * @return map of parameters
     */
    public Map<String, String> parameters();
    
    /**
     * Get a specific parameter value
     * @param parameter parameter name
     * @return parameter value, or null if not present
     */
    public String parameter(String parameter);
}

Install with Tessl CLI

npx tessl i tessl/maven-org-elasticsearch--elasticsearch-x-content

docs

configuration.md

content-generation.md

content-parsing.md

content-types.md

index.md

object-mapping.md

tile.json