Skip to content

Instantly share code, notes, and snippets.

@lucascs
Created October 14, 2011 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucascs/1287173 to your computer and use it in GitHub Desktop.
Save lucascs/1287173 to your computer and use it in GitHub Desktop.
generic converter for Json -> object #VRaptor
public interface Jsonable { }
@Convert(Jsonable.class)
public class JsonableConverter implements Converter<Jsonable> {
private XStreamBuilder builder;
public JsonableConverter(XStreamBuilder builder) {
this.builder = builder;
}
public Jsonable convert(String value, Class<? extends Jsonable> type, ResourceBundle bundle) {
if (Strings.isNullOrEmpty(value)) return null;
XStream stream = builder.jsonInstance();
String json = String.format("{\"%s\" : %s}", type.getName(), value);
return stream.fromXML(json);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment