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

#Cyrille37# cyrille37 at gmail.com
Fri Sep 14 17:30:15 SGT 2007


here is the BigIntegerSerializer :

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[]
        { String.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 
jso ) throws UnmarshallException
    {
        String val = jso instanceof String ? (String) jso : jso.toString();
        if( clazz == BigInteger.class )
        {
            return new BigInteger( val );
        }
        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;
    }

}


#Cyrille37# a écrit :
> Hello (again),
>
> I need your help on Deserialization error :
>
> I need to transfer java.math.BigInteger, so i add a 
> BigIntegerSerializer class in json-rpc and register it as default 
> serializer.
>
> JSONSerializer.java :
>    ...
>    public void registerDefaultSerializers() throws Exception
>    {
>        registerSerializer( new BeanSerializer() );
>       ...
>        registerSerializer( new BigIntegerSerializer() ); // here is.
>        registerSerializer( new PrimitiveSerializer() );
>    }
> and I add BigIntegerSerializer in the package jsonrpc.
>
> Serialization works fine (no error) and give this :
>
> {
>   "id":1,
>   "result":
>   {
>       "g": { "biginteger":"1716...840436", 
> "javaClass":"java.math.BigInteger" },
>       "q": { "biginteger":"11005...341", 
> "javaClass":"java.math.BigInteger" },
>       "seed": [ -112,40,...,-96,-41 ],
>       "p": { "biginteger":"1819...431", 
> "javaClass":"java.math.BigInteger" },
>       "javaClass":"crypto.key.dsa.DSAParametersGenerator"
>   }
> }
>
> but deserialization failed :
>
> Caused by: jsonrpc.UnmarshallException: bean 
> crypto.key.dsa.DSAParametersGenerator can't instantiate bean 
> java.math.BigInteger: java.math.BigInteger
>    at jsonrpc.BeanSerializer.unmarshall(BeanSerializer.java:199)
>    at jsonrpc.JSONSerializer.unmarshall(JSONSerializer.java:240)
>    at jsonrpc.client.JSONRpcClient.invoke(JSONRpcClient.java:142)
>    ...
>
> Have you got a tips to help me ?
> Thanks
> Cyrille
>
>





More information about the Json-rpc-java mailing list