Skip to content

Instantly share code, notes, and snippets.

@jalex79
Created February 15, 2012 14:20
Show Gist options
  • Save jalex79/1836060 to your computer and use it in GitHub Desktop.
Save jalex79/1836060 to your computer and use it in GitHub Desktop.
Vraptor Custom converter Data
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