Serialization: Add support for Byte to the XContentBuilder.
Close #6127
This commit is contained in:
parent
3534ffcd1d
commit
82e9a4e80a
|
@ -529,7 +529,7 @@ public final class XContentBuilder implements BytesStream {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public XContentBuilder field(XContentBuilderString name, BytesRef value) throws IOException {
|
public XContentBuilder utf8Field(XContentBuilderString name, BytesRef value) throws IOException {
|
||||||
field(name);
|
field(name);
|
||||||
generator.writeUTF8String(value.bytes, value.offset, value.length);
|
generator.writeUTF8String(value.bytes, value.offset, value.length);
|
||||||
return this;
|
return this;
|
||||||
|
@ -1135,6 +1135,8 @@ public final class XContentBuilder implements BytesStream {
|
||||||
generator.writeNumber(((Float) value).floatValue());
|
generator.writeNumber(((Float) value).floatValue());
|
||||||
} else if (type == Double.class) {
|
} else if (type == Double.class) {
|
||||||
generator.writeNumber(((Double) value).doubleValue());
|
generator.writeNumber(((Double) value).doubleValue());
|
||||||
|
} else if (type == Byte.class) {
|
||||||
|
generator.writeNumber(((Byte)value).byteValue());
|
||||||
} else if (type == Short.class) {
|
} else if (type == Short.class) {
|
||||||
generator.writeNumber(((Short) value).shortValue());
|
generator.writeNumber(((Short) value).shortValue());
|
||||||
} else if (type == Boolean.class) {
|
} else if (type == Boolean.class) {
|
||||||
|
|
|
@ -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
|
// and I end up with buckets that contravene the user's min_doc_count criteria in my reducer
|
||||||
if (bucket.subsetDf >= minDocCount) {
|
if (bucket.subsetDf >= minDocCount) {
|
||||||
builder.startObject();
|
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(CommonFields.DOC_COUNT, bucket.getDocCount());
|
||||||
builder.field("score", bucket.score);
|
builder.field("score", bucket.score);
|
||||||
builder.field("bg_count", bucket.supersetDf);
|
builder.field("bg_count", bucket.supersetDf);
|
||||||
|
|
|
@ -132,7 +132,7 @@ public class StringTerms extends InternalTerms {
|
||||||
builder.startArray(CommonFields.BUCKETS);
|
builder.startArray(CommonFields.BUCKETS);
|
||||||
for (InternalTerms.Bucket bucket : buckets) {
|
for (InternalTerms.Bucket bucket : buckets) {
|
||||||
builder.startObject();
|
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(CommonFields.DOC_COUNT, bucket.getDocCount());
|
||||||
((InternalAggregations) bucket.getAggregations()).toXContentInternal(builder, params);
|
((InternalAggregations) bucket.getAggregations()).toXContentInternal(builder, params);
|
||||||
builder.endObject();
|
builder.endObject();
|
||||||
|
|
|
@ -172,6 +172,13 @@ public class XContentBuilderTests extends ElasticsearchTestCase {
|
||||||
assertThat(builder.string(), equalTo("{\"test_name\":\"value\"}"));
|
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
|
@Test
|
||||||
public void testDateTypesConversion() throws Exception {
|
public void testDateTypesConversion() throws Exception {
|
||||||
Date date = new Date();
|
Date date = new Date();
|
||||||
|
@ -253,4 +260,4 @@ public class XContentBuilderTests extends ElasticsearchTestCase {
|
||||||
|
|
||||||
assertThat(i, equalTo(terms.size()));
|
assertThat(i, equalTo(terms.size()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue