RE: Enums and other serialization woes

From: Jason Stiefel <jasons@xxx.net>
Date: Tue Oct 25 2005 - 08:52:49 PDT

There sure is. I wrote my own factory, but you might want to adjust the way
you implement this. Basically you set the SerializerFactory into your
HessianInput and/or HessianOutput instances:

-------

HessianInput in = new HessianInput(request.getInputStream());
in.getSerializerFactory().addFactory(serializerFactory);

HessianOutput out = new HessianOutput(response.getOutputStream());
out.getSerializerFactory().addFactory(serializerFactory);

-------

Here's my factory. I just extend their AbstractSerializerFactory and look
for my "special" types like enums and a few other classes we use that
hessian doesnt play well with (anything that implements a List with extra
methods is a good example of this as Hessian delegates those automatically
to their List serializer.)

Note that returning null from the factory methods delegates back to the
hessian factory for resolution.

-------

package com.aebn.media.util.hessian;

import com.aebn.media.util.ResultList;
import com.caucho.hessian.io.AbstractSerializerFactory;
import com.caucho.hessian.io.Deserializer;
import com.caucho.hessian.io.HessianProtocolException;
import com.caucho.hessian.io.Serializer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Replacement for the default Hessian SerializerFactory that injects our
special serializers when
 * required for an Enum or {@link ResultList}.
 *
 * @author kp
 */
public class SerializerFactory extends AbstractSerializerFactory {

    private static Log LOG = LogFactory.getLog(SerializerFactory.class);

    private JavaEnumSerializer javaEnumSerializer = new
JavaEnumSerializer();
    private JavaEnumDeSerializer javaEnumDeSerializer = new
JavaEnumDeSerializer();
    private ResultListCollectionSerializer resultListCollectionSerializer =
new ResultListCollectionSerializer();
    private ResultListCollectionDeserializer
resultListCollectionDeserializer = new ResultListCollectionDeserializer();

    public Serializer getSerializer(Class cl) throws
HessianProtocolException {

        if (LOG.isDebugEnabled())
            LOG.debug("Looking up serializer for: " + cl.getName());

        // ResultList hack! This is necessary because the result list class
is seen as just a list, so the special
        // attributes (searchTime, totalResults, etc..) are not propery
serialized.
        if (ResultList.class.isAssignableFrom(cl)) {
            return (resultListCollectionSerializer);
        }

        if (Enum.class.isAssignableFrom(cl)) {
            if (LOG.isDebugEnabled())
                LOG.debug("Returning enum serializer for class: " +
cl.getName());
            return (javaEnumSerializer);
        }

        return (null);
    }

    public Deserializer getDeserializer(Class cl) {

        if (LOG.isDebugEnabled())
            LOG.debug("Looking up deserializer for: " + cl.getName());

        if (ResultList.class.isAssignableFrom(cl)) {
            return (resultListCollectionDeserializer);

        } else if (Enum.class.isAssignableFrom(cl)) {
            return (javaEnumDeSerializer);
        }

        return (null);
    }
}

-----Original Message-----
From: owner-hessian-interest@xxx.com
[mailto:owner-hessian-interest@xxx.com]On Behalf Of Andrus Adamchik
Sent: Tuesday, October 25, 2005 10:59 AM
To: hessian-interest@xxx.com
Subject: Re: Enums and other serialization woes

Jason,

Thanks for the reply.

As it was pretty late last night when I wrote my message, I guess I
didn't make myself clear. I already wrote my own serializer/
deserializer pair - this was an easy part. My doubts were about
integrating it in Hessian. I was wondering if there is a way to
*register* custom serializer with existing Hessian classes as opposed
to subclassing HessianProxyFactory and HessianServlet.

Andrus

On Oct 25, 2005, at 10:39 AM, Jason Stiefel wrote:

> I wrote my own se/deserializer combo for enums. I'll just paste
> them and
> hope it helps you out. The gist of it is you need to know 2 things to
> deserialize an enum (and thus those two things need to be
> serialized) First
> is the classname of the enum. Second is the toString value of the
> enum.
> Using those two pieces you can call:
>
Received on Tue 25 Oct 2005 08:52:49 -0700

This archive was generated by hypermail 2.1.8 : Thu Sep 28 2006 - 20:16:41 PDT