add the ability to control the content type the map will serialize under

This commit is contained in:
kimchy 2010-05-06 15:50:35 +03:00
parent 7a01b19b8d
commit 5603c4d09c
2 changed files with 19 additions and 2 deletions

View File

@ -214,13 +214,22 @@ public class IndexRequest extends ShardReplicationOperationRequest {
}
/**
* Writes the JSON as a {@link Map}.
* Writes the Map as a JSON.
*
* @param source The map to index
*/
@Required public IndexRequest source(Map source) throws ElasticSearchGenerationException {
return source(source, XContentType.JSON);
}
/**
* Writes the JSON as the provided content type.
*
* @param source The map to index
*/
@Required public IndexRequest source(Map source, XContentType contentType) throws ElasticSearchGenerationException {
try {
BinaryXContentBuilder builder = XContentFactory.contentBinaryBuilder(XContentType.JSON);
BinaryXContentBuilder builder = XContentFactory.contentBinaryBuilder(contentType);
builder.map(source);
this.source = builder.copiedBytes();
} catch (IOException e) {

View File

@ -20,11 +20,19 @@
package org.elasticsearch.util.xcontent;
/**
* The content type of {@link org.elasticsearch.util.xcontent.XContent}.
*
* @author kimchy (shay.banon)
*/
public enum XContentType {
/**
* A JSON based content type.
*/
JSON(0),
/**
* An optimized binary form of JSON.
*/
XSON(1);
public static XContentType fromRestContentType(String contentType) {