If a value/field is a Calendar, it will be converted to a Date using getTime()

Closes #2911
This commit is contained in:
Lucas Ward 2013-04-17 12:04:46 -04:00 committed by Simon Willnauer
parent 0eb298fe64
commit 99c101c37e
3 changed files with 40 additions and 0 deletions

View File

@ -39,6 +39,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
@ -734,6 +735,8 @@ public final class XContentBuilder implements BytesStream {
field(name, ((Boolean) value).booleanValue());
} else if (value instanceof Date) {
field(name, (Date) value);
} else if (value instanceof Calendar) {
field(name, convertCalendar((Calendar) value));
} else if (type == byte[].class) {
field(name, (byte[]) value);
} else if (value instanceof ReadableInstant) {
@ -843,6 +846,8 @@ public final class XContentBuilder implements BytesStream {
value((byte[]) value);
} else if (value instanceof Date) {
value((Date) value);
} else if (value instanceof Calendar) {
value(convertCalendar((Calendar) value));
} else if (value instanceof ReadableInstant) {
value((ReadableInstant) value);
} else if (value instanceof BytesReference) {
@ -860,6 +865,10 @@ public final class XContentBuilder implements BytesStream {
return this;
}
private Date convertCalendar(Calendar value) {
return value.getTime();
}
public XContentBuilder field(String name, boolean value) throws IOException {
field(name);
generator.writeBoolean(value);

View File

@ -178,6 +178,8 @@ public class XContentMapConverter {
gen.writeBinary((byte[]) value);
} else if (value instanceof Date) {
gen.writeString(XContentBuilder.defaultDatePrinter.print(((Date) value).getTime()));
} else if (value instanceof Calendar) {
gen.writeString(XContentBuilder.defaultDatePrinter.print((((Calendar) value)).getTimeInMillis()));
} else if (value instanceof BytesReference) {
BytesReference bytes = (BytesReference) value;
if (!bytes.hasArray()) {

View File

@ -28,6 +28,8 @@ import org.elasticsearch.common.xcontent.XContentGenerator;
import org.elasticsearch.common.xcontent.XContentType;
import org.testng.annotations.Test;
import java.util.*;
import static org.elasticsearch.common.xcontent.XContentBuilder.FieldCaseConversion.CAMELCASE;
import static org.elasticsearch.common.xcontent.XContentBuilder.FieldCaseConversion.UNDERSCORE;
import static org.hamcrest.MatcherAssert.assertThat;
@ -106,4 +108,31 @@ public class XContentBuilderTests {
builder.startObject().field("testName", "value").endObject();
assertThat(builder.string(), equalTo("{\"test_name\":\"value\"}"));
}
@Test
public void testDateTypesConversion() throws Exception {
Date date = new Date();
String expectedDate = XContentBuilder.defaultDatePrinter.print(date.getTime());
Calendar calendar = new GregorianCalendar();
String expectedCalendar = XContentBuilder.defaultDatePrinter.print(calendar.getTimeInMillis());
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
builder.startObject().field("date", date).endObject();
assertThat(builder.string(), equalTo("{\"date\":\"" + expectedDate + "\"}"));
builder = XContentFactory.contentBuilder(XContentType.JSON);
builder.startObject().field("calendar", calendar).endObject();
assertThat(builder.string(), equalTo("{\"calendar\":\"" + expectedCalendar + "\"}"));
builder = XContentFactory.contentBuilder(XContentType.JSON);
Map<String, Object> map = new HashMap<String, Object>();
map.put("date", date);
builder.map(map);
assertThat(builder.string(), equalTo("{\"date\":\"" + expectedDate + "\"}"));
builder = XContentFactory.contentBuilder(XContentType.JSON);
map = new HashMap<String, Object>();
map.put("calendar", calendar);
builder.map(map);
assertThat(builder.string(), equalTo("{\"calendar\":\"" + expectedCalendar + "\"}"));
}
}