added additional tests for the default deserialization of dates.

This commit is contained in:
Inderjeet Singh 2008-10-06 22:00:04 +00:00
parent 1cad54f7b1
commit 0b9c739a7b
2 changed files with 22 additions and 2 deletions

View File

@ -21,7 +21,6 @@ import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

View File

@ -190,6 +190,11 @@ public class DefaultTypeAdaptersTest extends TestCase {
assertEquals("\"" + DateFormat.getDateInstance().format(now) + "\"", json);
}
public void testDefaultDateDeserialization() {
Date date = new Date();
assertEquals(date, gson.fromJson(gson.toJson(date), Date.class));
}
public void testDefaultDateSerializationUsingBuilder() throws Exception {
Gson gson = new GsonBuilder().create();
Date now = new Date();
@ -197,6 +202,13 @@ public class DefaultTypeAdaptersTest extends TestCase {
assertEquals("\"" + DateFormat.getDateInstance().format(now) + "\"", json);
}
public void testDefaultDateDeserializationUsingBuilder() throws Exception {
Gson gson = new GsonBuilder().create();
Date now = new Date();
String json = gson.toJson(now);
assertEquals(now, gson.fromJson(json, Date.class));
}
public void testDateSerializationWithPattern() throws Exception {
String pattern = "yyyy-MM-dd";
DateFormat formatter = new SimpleDateFormat(pattern);
@ -205,7 +217,16 @@ public class DefaultTypeAdaptersTest extends TestCase {
String json = gson.toJson(now);
assertEquals("\"" + formatter.format(now) + "\"", json);
}
public void testDateDeserializationWithPattern() throws Exception {
String pattern = "yyyy-MM-dd";
DateFormat formatter = new SimpleDateFormat(pattern);
Gson gson = new GsonBuilder().setDateFormat(DateFormat.LONG).setDateFormat(pattern).create();
Date now = new Date();
String json = gson.toJson(now);
assertEquals(now, gson.fromJson(json, Date.class));
}
private static class ClassWithBigDecimal {
BigDecimal value;
ClassWithBigDecimal() { }