[Json-rpc-java] de/serializing java.math.BigInteger

#Cyrille37# cyrille37 at gmail.com
Fri Sep 14 18:04:17 SGT 2007


Ok, I've found errors. In BigIntegerSerializer I was using String.class 
instead of JSONObject.class

Here is the well working BigIntegerSerializer.java :

package jsonrpc;

import java.math.BigInteger;

import org.json.JSONObject;

public class BigIntegerSerializer extends AbstractSerializer
{

    private final static long serialVersionUID = 1;

    private static Class[] _serializableClasses = new Class[] { 
BigInteger.class };

    private static Class[] _JSONClasses = new Class[] { JSONObject.class };

    public Class[] getSerializableClasses(){ return _serializableClasses; }

    public Class[] getJSONClasses() { return _JSONClasses; }

    public ObjectMatch tryUnmarshall( SerializerState state, Class 
clazz, Object jso )
            throws UnmarshallException
    {
        return ObjectMatch.OKAY;
    }

    public Object unmarshall( SerializerState state, Class clazz, Object 
o ) throws UnmarshallException
    {
        JSONObject jso = (JSONObject) o;
        if( clazz == BigInteger.class )
        {
            return new BigInteger( jso.getString("biginteger") );
        }
        throw new UnmarshallException( "invalid class " + clazz );
    }

    public Object marshall( SerializerState state, Object o ) throws 
MarshallException
    {
        if( ! (o instanceof BigInteger) )
        {
            throw new MarshallException( "cannot marshall biginteger 
using class " + o.getClass() );
        }
        JSONObject obj = new JSONObject();
        if( ser.getMarshallClassHints() )
            obj.put( "javaClass", o.getClass().getName() );
        obj.put( "biginteger", ((BigInteger)o).toString() );
        return obj;
    }

}

Cyrille.




More information about the Json-rpc-java mailing list