Serialization: Add support for Byte to the XContentBuilder.

Close #6127
This commit is contained in:
Mathias Fussenegger 2014-05-12 13:30:22 +02:00 committed by Adrien Grand
parent 3534ffcd1d
commit 82e9a4e80a
4 changed files with 13 additions and 4 deletions

View File

@ -529,7 +529,7 @@ public final class XContentBuilder implements BytesStream {
return this;
}
public XContentBuilder field(XContentBuilderString name, BytesRef value) throws IOException {
public XContentBuilder utf8Field(XContentBuilderString name, BytesRef value) throws IOException {
field(name);
generator.writeUTF8String(value.bytes, value.offset, value.length);
return this;
@ -1135,6 +1135,8 @@ public final class XContentBuilder implements BytesStream {
generator.writeNumber(((Float) value).floatValue());
} else if (type == Double.class) {
generator.writeNumber(((Double) value).doubleValue());
} else if (type == Byte.class) {
generator.writeNumber(((Byte)value).byteValue());
} else if (type == Short.class) {
generator.writeNumber(((Short) value).shortValue());
} else if (type == Boolean.class) {

View File

@ -144,7 +144,7 @@ public class SignificantStringTerms extends InternalSignificantTerms {
// and I end up with buckets that contravene the user's min_doc_count criteria in my reducer
if (bucket.subsetDf >= minDocCount) {
builder.startObject();
builder.field(CommonFields.KEY, ((Bucket) bucket).termBytes);
builder.utf8Field(CommonFields.KEY, ((Bucket) bucket).termBytes);
builder.field(CommonFields.DOC_COUNT, bucket.getDocCount());
builder.field("score", bucket.score);
builder.field("bg_count", bucket.supersetDf);

View File

@ -132,7 +132,7 @@ public class StringTerms extends InternalTerms {
builder.startArray(CommonFields.BUCKETS);
for (InternalTerms.Bucket bucket : buckets) {
builder.startObject();
builder.field(CommonFields.KEY, ((Bucket) bucket).termBytes);
builder.utf8Field(CommonFields.KEY, ((Bucket) bucket).termBytes);
builder.field(CommonFields.DOC_COUNT, bucket.getDocCount());
((InternalAggregations) bucket.getAggregations()).toXContentInternal(builder, params);
builder.endObject();

View File

@ -172,6 +172,13 @@ public class XContentBuilderTests extends ElasticsearchTestCase {
assertThat(builder.string(), equalTo("{\"test_name\":\"value\"}"));
}
@Test
public void testByteConversion() throws Exception {
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
builder.startObject().field("test_name", (Byte)(byte)120).endObject();
assertThat(builder.bytes().toUtf8(), equalTo("{\"test_name\":120}"));
}
@Test
public void testDateTypesConversion() throws Exception {
Date date = new Date();