如何使用Kryo序列化Multimaps?

时间:2015-01-04 23:16:21

标签: java serialization deserialization kryo

问题

我正在尝试使用LinkedHashMultimap序列化Kryo Serialization library,但在反序列化时获得NullPointerException。最小的工作示例如下:

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.google.common.collect.LinkedHashMultimap;
import java.io.*;

public class SerializationTest {

    private static final String ioFileName = "someIO.bin";

    public static void main(String[] args0) {

        // Create LinkedHashMultimap to serialize
        LinkedHashMultimap<String, Object> outObj = LinkedHashMultimap.create();
        outObj.put("x", 1);
        outObj.put("y", "abc");

        // Try to serialize and deserialize
        Kryo kryo = new Kryo();
        writeObj(kryo, outObj);
        LinkedHashMultimap<String, Object> inObj = (LinkedHashMultimap<String, Object>) readObj(kryo);

        System.out.println(inObj);
    }

    public static Object readObj(Kryo kryo) {
        Object obj = null;
        try {
            Input input = new Input(new FileInputStream(ioFileName));
            obj = kryo.readClassAndObject(input);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return obj;
    }

    public static void writeObj(Kryo kryo, Object obj) {
        try {
            Output output = new Output(new FileOutputStream(ioFileName));
            kryo.writeClassAndObject(output, obj);
            output.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

调用kryo.readClassAndObject()(第30行)后,您可以立即看到问题(IntelliJ 14中的调试屏幕截图):

enter image description here

LinkedHashMultimap在序列化时被破坏或者只是被错误地反序列化,导致NullPointerException

调用System.out.println(inObj)时产生的完整堆栈跟踪:

Exception in thread "main" java.lang.NullPointerException
Disconnected from the target VM, address: '127.0.0.1:60310', transport: 'socket'
    at com.google.common.collect.AbstractMapBasedMultimap$AsMap.toString(AbstractMapBasedMultimap.java:1293)
    at com.google.common.collect.AbstractMultimap.toString(AbstractMultimap.java:239)
    at com.google.common.collect.LinkedHashMultimap.toString(LinkedHashMultimap.java:81)
    at java.lang.String.valueOf(String.java:2854)
    at java.io.PrintStream.println(PrintStream.java:821)
    at SerializationTest.main(SerializationTest.java:23)

有谁知道如何解决这个问题?

动机:

为了帮助进行远程调试(Java),能够请求远程服务器通过任意对象发送到本地计算机进行检查非常有用。但是,这意味着远程服务器必须能够序列化在运行时事先不知道的任意java对象。

所以我四处询问并偶然发现Kryo serialization library。从Kryo's documentation开始,一个主要特征是它在序列化任意java对象时非常强大。对象

  • 不必实施Serializable
  • 不需要no-arg构造函数可反序列化和
  • 在序列化之前,我甚至不需要了解对象的结构。

1 个答案:

答案 0 :(得分:-1)

在进行序列化/反序列化之前尝试下一步(在我的例子中,它适用于kryo.WriteObject和kryo.readObject)

    JavaSerializer serializer = new JavaSerializer();
    kryo.register(LinkedHashMultimap.class, serializer);

工作示例:

public class SerializationTest {

private static final String ioFileName = "someIO.bin";

public static void main(String[] args0) {

    // Create LinkedHashMultimap to serialize
    LinkedHashMultimap<String, Object> outObj = LinkedHashMultimap.create();
    outObj.put("x", 1);
    outObj.put("y", "abc");

    // Try to serialize and deserialize
    Kryo kryo = new Kryo();
    kryo.register(LinkedHashMultimap.class, new JavaSerializer());
    writeObj(kryo, outObj);
    LinkedHashMultimap<String, Object> inObj = (LinkedHashMultimap<String, Object>) readObj(kryo);

    System.out.println(inObj);
}

public static Object readObj(Kryo kryo) {
    Object obj = null;
    try {
        Input input = new Input(new FileInputStream(ioFileName));
        obj = kryo.readClassAndObject(input);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return obj;
}

public static void writeObj(Kryo kryo, Object obj) {
    try {
        Output output = new Output(new FileOutputStream(ioFileName));
        kryo.writeClassAndObject(output, obj);
        output.flush();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

}

输出:

{x=[1], y=[abc]}