Re: [Simple-support] how to write a transformer
Brought to you by:
niallg
|
From: Александр (R. Щ. <ir...@gm...> - 2011-04-08 10:11:49
|
just implement Matcher and Transformer and pass the first as an argument to
Persister constructior.
Here is my implementation for enums (sometimes i gotta deal with them in
non-standart way):
public class EnumMatcher implements Matcher {
public Transform match(Class aClass) throws Exception {
if (aClass.equals(Protocol.class)) {
logger.debug("Matched :" + aClass.getName());
return new EnumTransform(aClass);
} else {
return null;
}
}
}
public class EnumTransform implements Transform {
private Class clazz;
public EnumTransform(Class clazz) {
this.clazz = clazz;
}
public Object read(String s) throws Exception {
if (clazz.equals(Protocol.class)) {
return Protocol.parseString(s);
} else throw new InternalException("Invalid transform match");
}
public String write(Object o) throws Exception {
if (clazz.equals(Protocol.class)) {
return ((Protocol) o).protocol();
} else throw new InternalException("Invalid transform match");
}
}
in this example Protocol is a Enum with static method parseString(String).
2011/4/8 Niall Gallagher <gal...@ya...>
> You need to write a Matcher object to return the Matcher for the specified
> type. If you look in the test cases there is actually a Transformer for UUID
> in there somewhere.
>
> --- On *Thu, 7/4/11, jo desmet <jd...@gm...>* wrote:
>
>
> From: jo desmet <jd...@gm...>
> Subject: [Simple-support] how to write a transformer
> To: sim...@li...
> Received: Thursday, 7 April, 2011, 8:44 AM
>
> Hi,
>
> I need to serialize some object tree but it contains UUID's
>
> so when serializing I get the error
>
> Exception in thread "main"
> org.simpleframework.xml.transform.TransformException: Transform of class
> java.util.UUID not supported
>
>
> It is easy to write a transformer for this but how can I add this to the
> persister ?
>
>
>
> Jo
>
> -----Inline Attachment Follows-----
>
>
> ------------------------------------------------------------------------------
> Xperia(TM) PLAY
> It's a major breakthrough. An authentic gaming
> smartphone on the nation's most reliable network.
> And it wants your games.
> http://p.sf.net/sfu/verizon-sfdev
>
> -----Inline Attachment Follows-----
>
> _______________________________________________
> Simple-support mailing list
> Sim...@li...<http://mc/compose?to=...@li...>
> https://lists.sourceforge.net/lists/listinfo/simple-support
>
>
>
> ------------------------------------------------------------------------------
> Xperia(TM) PLAY
> It's a major breakthrough. An authentic gaming
> smartphone on the nation's most reliable network.
> And it wants your games.
> http://p.sf.net/sfu/verizon-sfdev
> _______________________________________________
> Simple-support mailing list
> Sim...@li...
> https://lists.sourceforge.net/lists/listinfo/simple-support
>
>
--
Sincerely Yours
Alexander Shchegolev
|