Skip to main content

How to encode Enums?

Posted by malenkov on August 7, 2006 at 6:00 PM PDT

As you may know, Enums were introduced in Tiger, but they are not supported by XMLEncoder.

This article describes how to encode them into XML properly.

Let's create a simple Enum with static method to test encoding into XML:

import java.beans.XMLEncoder;
import java.io.ByteArrayOutputStream;

public enum TestEnum {
    LEFT, RIGHT;

    public static void main( String[] args ) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();

        XMLEncoder encoder = new XMLEncoder( stream );
        encoder.writeObject( TestEnum.LEFT );
        encoder.writeObject( TestEnum.RIGHT );
        encoder.close();

        System.out.println( stream );
    }
}

If the test method is executed it will print the following result:

java.lang.InstantiationException: TestEnum
Continuing ...
java.lang.Exception: XMLEncoder: discarding statement XMLEncoder.writeObject(TestEnum);
Continuing ...
java.lang.InstantiationException: TestEnum
Continuing ...
java.lang.Exception: XMLEncoder: discarding statement XMLEncoder.writeObject(TestEnum);
Continuing ...
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.5.0_07" class="java.beans.XMLDecoder">
</java>

As you can see, some exceptions were thrown during encoding and XML does not contain any object.

Let's change the test program.
We should create persistence delegate and register it before encoding:

import java.beans.Encoder;
import java.beans.Expression;
import java.beans.PersistenceDelegate;
import java.beans.XMLEncoder;
import java.io.ByteArrayOutputStream;

public enum TestEnum {
    LEFT, RIGHT;

    public static void main( String[] args ) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();

        XMLEncoder encoder = new XMLEncoder( stream );
        encoder.setPersistenceDelegate( TestEnum.class, new EnumPersistenceDelegate() );
        encoder.writeObject( TestEnum.LEFT );
        encoder.writeObject( TestEnum.RIGHT );
        encoder.close();

        System.out.println( stream );
    }
}

class EnumPersistenceDelegate extends PersistenceDelegate {
    protected boolean mutatesTo( Object oldInstance, Object newInstance ) {
        return oldInstance == newInstance;
    }

    protected Expression instantiate( Object oldInstance, Encoder out ) {
        Enum e = ( Enum )oldInstance;
        return new Expression( e, e.getClass(), "valueOf", new Object[]{e.name()} );
    }
}

Note that EnumPersistenceDelegate does not depend on concrete Enum.

So, it is possible to use one instance of such persistence delegate with any Enum,

but it is necessary to register it with concrete class of required Enum.

As a result, XML contains both encoded objects:

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.5.0_07" class="java.beans.XMLDecoder">
<object class="TestEnum" method="valueOf">
  <string>LEFT</string>
</object>
<object class="TestEnum" method="valueOf">
  <string>RIGHT</string>
</object>

</java>

I have good news for you: JDK 6 already supports Enum encoding.

So, you should not create persistence delegate and register it for your Enum anymore.

Related Topics >>