Skip to content

Commit

Permalink
Serialization: Add support for Byte to the XContentBuilder.
Browse files Browse the repository at this point in the history
Close #6127
  • Loading branch information
mfussenegger authored and jpountz committed May 28, 2014
1 parent a76da6f commit 3d386e5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
Expand Up @@ -529,6 +529,16 @@ public XContentBuilder field(XContentBuilderString name, BytesReference value) t
return this;
}

public XContentBuilder utf8Field(XContentBuilderString name, BytesRef value) throws IOException {
field(name);
generator.writeUTF8String(value.bytes, value.offset, value.length);
return this;
}

/**
* replaced with {@link #utf8Field(XContentBuilderString, org.apache.lucene.util.BytesRef)}
*/
@Deprecated
public XContentBuilder field(XContentBuilderString name, BytesRef value) throws IOException {
field(name);
generator.writeUTF8String(value.bytes, value.offset, value.length);
Expand Down Expand Up @@ -1135,6 +1145,8 @@ private void writeValue(Object value) throws IOException {
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) {
Expand Down
Expand Up @@ -144,7 +144,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
// 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);
Expand Down
Expand Up @@ -132,7 +132,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
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();
Expand Down
Expand Up @@ -172,6 +172,13 @@ public void testFieldCaseConversion() throws Exception {
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();
Expand Down Expand Up @@ -253,4 +260,4 @@ public void testCopyCurrentStructure() throws Exception {

assertThat(i, equalTo(terms.size()));
}
}
}

0 comments on commit 3d386e5

Please sign in to comment.