A.J.

Someday somehow someone is going to steal your carbon.

Posts Tagged ‘caucho

BigInteger and hessian.

leave a comment »

Out of the box hessian does not have support for java.math.BigInteger.
It easy to add custom hessian serializers to hessian to extend it to handle cases like these.

The deserializer:


package com.foo.hessian;

import com.caucho.hessian.io.AbstractDeserializer;
import com.caucho.hessian.io.AbstractHessianInput;

import java.io.IOException;
import java.math.BigInteger;

public class BigIntegerDeSerializer extends AbstractDeserializer
{

    public Class getType()
    {
        return BigInteger.class;
    }

    public Object readMap(AbstractHessianInput in)
            throws IOException
    {
        int ref = in.addRef(null);

        String initValue = null;

        while (!in.isEnd())
        {
            String key = in.readString();

            if (key.equals("value"))
                initValue = in.readString();
            else
                in.readString();
        }

        in.readMapEnd();

        Object value = new BigInteger(initValue);

        in.setRef(ref, value);

        return value;
    }

    public Object readObject(AbstractHessianInput in, String[] fieldNames)
            throws IOException
    {
        int ref = in.addRef(null);

        String initValue = null;

        for (String key : fieldNames)
        {
            if (key.equals("value"))
                initValue = in.readString();
            else
                in.readObject();
        }

        Object value = new BigInteger(initValue);

        in.setRef(ref, value);

        return value;
    }

}

The serializer:

package com.foo.hessian;

import com.caucho.hessian.io.AbstractSerializer;
import com.caucho.hessian.io.AbstractHessianOutput;

import java.io.IOException;
import java.math.BigInteger;

public class BigIntegerSerializer extends AbstractSerializer
{
    public void writeObject(Object obj, AbstractHessianOutput out) throws IOException
    {

        if (obj == null)
            out.writeNull();
        else
        {
            Class cl = obj.getClass();

            if (out.addRef(obj))
                return;

            int ref = out.writeObjectBegin(cl.getName());

            BigInteger bi = (BigInteger) obj;

            if (ref < -1)
            {
                out.writeString("value");
                out.writeString(bi.toString());
                out.writeMapEnd();
            }
            else
            {
                if (ref == -1)
                {
                    out.writeInt(1);
                    out.writeString("value");
                    out.writeObjectBegin(cl.getName());
                }

                out.writeString(bi.toString());
            }
        }
    }
}

Next you will need a serializer factory for the BigInteger Serializer/Deserializer

public class BigIntegerSerializerFactory extends AbstractSerializerFactory
{
    public Serializer getSerializer(Class cl) throws HessianProtocolException
    {
        if (BigInteger.class.isAssignableFrom(cl)) {
            return new BigIntegerSerializer();
        }
        return null;
    }

    public Deserializer getDeserializer(Class cl) throws HessianProtocolException
    {
        if (BigInteger.class.isAssignableFrom(cl)) {
            return new BigIntegerDeSerializer();
        }
        return null;
    }
}

Finally add this to the main via the SerializerFactory#addFactory method.

In the next post I will show how to tie this all into spring.

Written by AJ

February 11, 2009 at 3:56 pm

Posted in java

Tagged with , ,

Follow

Get every new post delivered to your Inbox.