Note that there are some explanatory texts on larger screens.

plurals
  1. POSerialization third-party classes with Simple XML (org.simpleframework.xml)
    primarykey
    data
    text
    <p>I have decided to use <a href="http://simple.sourceforge.net/" rel="noreferrer">Simple XML serialization</a> and was stucked with basic problem. I am trying to serialize <code>java.util.UUID</code> class instance as final field in this small class:</p> <pre><code>@Root public class Identity { @Attribute private final UUID id; public Identity(@Attribute UUID id) { this.id = id; } } </code></pre> <p><a href="http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#converters" rel="noreferrer">Tutorial</a> shows how to serialize third-party objects by registering converters like this:</p> <pre><code>Registry registry = new Registry(); registry.bind(UUID.class, UUIDConverter.class); Strategy strategy = new RegistryStrategy(registry); Serializer serializer = new Persister(strategy); serializer.write( object, stream ); </code></pre> <p>appropriate converter for UUID is pretty simple:</p> <pre><code>public class UUIDConverter implements Converter&lt;UUID&gt; { @Override public UUID read(InputNode node) throws Exception { return new UUID.fromString(node.getValue()); } @Override public void write(OutputNode node, UUID value) throws Exception { node.setValue(value.toString()); } } </code></pre> <p>But this simple code just didn't work for me, during serialization objects with UUID fields was thrown exception <strong>Transform of class java.util.UUID not supported</strong>.</p> <p>I have tried something something similar with custom <code>Matcher</code> (which was not in tutorial) that works for me:</p> <pre><code>Serializer serializer = new Persister(new MyMatcher()); serializer.write( object, stream ); </code></pre> <p>and <code>Matcher</code> class looks like this:</p> <pre><code>public static class MyMatcher implements Matcher { @Override @SuppressWarnings("unchecked") public Transform match(Class type) throws Exception { if (type.equals(UUID.class)) return new UUIDTransform(); return null; } } public class UUIDTransform implements Transform&lt;UUID&gt; { @Override public UUID read(String value) throws Exception { return UUID.fromString(value); } @Override public String write(UUID value) throws Exception { return value.toString(); } } </code></pre> <p>Questions:</p> <ul> <li>Is custom Matcher always recommended practice for streaming third-party classes? </li> <li>In which case I can use Converter?</li> <li>Are there any better tutorials/examples for Simple XML out there?</li> </ul> <p>Thank you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload