Created
October 14, 2011 13:53
-
-
Save lucascs/1287173 to your computer and use it in GitHub Desktop.
generic converter for Json -> object #VRaptor
This file contains 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
public interface Jsonable { } |
This file contains 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
@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