Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>After some investigation of the source code, i have discovered that the library uses interface <code>Transform</code> to transform values to Strings. The default behavior of enum transformations is defined by class <code>EnumTransform</code>. In order to customize that, you can define you own Transform class. The following version of <code>Transform</code> implementation would call <code>toString()</code> instead of the default <code>name()</code> on the enum objects.</p> <pre><code>class MyEnumTransform implements Transform&lt;Enum&gt; { private final Class type; public MyEnumTransform(Class type) { this.type = type; } public Enum read(String value) throws Exception { for (Object o : type.getEnumConstants()) { if (o.toString().equals(value)) { return (Enum) o; } } return null; } public String write(Enum value) throws Exception { return value.toString(); } } </code></pre> <p><code>Transform</code> objects are returned from <code>match</code> method by objects of <code>Matcher</code> interface. There could be several <code>Matcher</code> objects. The library tries them one by one until it finds one that returns a non-null <code>Transformer</code> object. You can define your own <code>Matcher</code> object and pass it as argument to the constructor of <code>Persister</code> class. This object will get the highest priority. </p> <pre><code>Persister serializer = new Persister(new Matcher() { public Transform match(Class type) throws Exception { if (type.isEnum()) { return new MyEnumTransform(type); } return null; } }); </code></pre> <p>Finally, you wont forget to define a toString method on your enum classes. Then the combination of codes above will do you the work of encoding enum objects using their toString values.</p>
 

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