Skip to content

Commit

Permalink
ISSUE #21
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-taix committed Jun 4, 2011
1 parent 68a9eda commit 134bbdb
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
40 changes: 40 additions & 0 deletions app/utils/DateTypeAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
*
*/
package utils;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.google.gson.InstanceCreator;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

/**
* @author eric
*/
public class DateTypeAdapter implements InstanceCreator, JsonSerializer, JsonDeserializer {

@Override
public Object createInstance(Type type) {
return null;
}

@Override
public JsonElement serialize(Object obj, Type type, JsonSerializationContext ctx) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
JsonElement ele = new JsonPrimitive(sdf.format((Date)obj));
return ele;
}

@Override
public Object deserialize(JsonElement ele, Type type, JsonDeserializationContext ctx) throws JsonParseException {
return null;
}
}
10 changes: 9 additions & 1 deletion app/utils/RenderJsonExclusion.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package utils;

import java.sql.Timestamp;
import java.util.Date;

import play.mvc.results.RenderJson;

import com.google.gson.ExclusionStrategy;
Expand All @@ -9,7 +12,12 @@ public class RenderJsonExclusion extends RenderJson {

public RenderJsonExclusion(Object o, ExclusionStrategy exclusionStrategy) {
super(new GsonBuilder().setExclusionStrategies(exclusionStrategy)
.serializeNulls().create().toJson(o));
.serializeNulls().
setDateFormat("yyyy/MM/dd").
registerTypeAdapter(Timestamp.class, new DateTypeAdapter()).
registerTypeAdapter(Date.class, new DateTypeAdapter()).
registerTypeAdapter(java.sql.Date.class, new DateTypeAdapter()).
create().toJson(o));
}

public RenderJsonExclusion(Object o) {
Expand Down

0 comments on commit 134bbdb

Please sign in to comment.