Created
February 15, 2012 14:20
-
-
Save jalex79/1836060 to your computer and use it in GitHub Desktop.
Vraptor Custom converter Data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.jalex.util; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import javax.servlet.http.HttpServletResponse; | |
import br.com.caelum.vraptor.interceptor.TypeNameExtractor; | |
import br.com.caelum.vraptor.ioc.Component; | |
import br.com.caelum.vraptor.serialization.ProxyInitializer; | |
import br.com.caelum.vraptor.serialization.xstream.XStreamJSONSerialization; | |
import com.thoughtworks.xstream.XStream; | |
import com.thoughtworks.xstream.converters.SingleValueConverter; | |
@Component | |
public class CustomJSONSerialization extends XStreamJSONSerialization { | |
public CustomJSONSerialization(HttpServletResponse response,TypeNameExtractor extractor, ProxyInitializer initializer) { | |
super(response, extractor, initializer); | |
} | |
@Override | |
public XStream getXStream() { | |
XStream xstream = super.getXStream(); | |
xstream.aliasSystemAttribute(null, "class"); // <-------- | |
xstream.registerConverter(new SingleValueConverter() { | |
public String toString(Object value) { | |
return new SimpleDateFormat("dd/MM/yyyy hh:mm").format(value); | |
} | |
public boolean canConvert(Class clazz) { | |
return Date.class.isAssignableFrom(clazz); | |
} | |
public Object fromString(String value) { | |
return null; //não é usado | |
} | |
}); | |
return xstream; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment