readObject中的Jackson ObjectMap导致IOException:Stream关闭

时间:2015-08-19 23:42:03

标签: java json serialization jackson

我正在尝试在类Serializable中创建JsonNode。但是当我运行我的测试代码时,当在readObject中使用Jackson ObjectMapper时,我得到了Exception。

java.io.IOException:Stream已关闭     在java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:170) ...

这是代码 https://github.com/songyangster/vertx-in-springboot/tree/master/src/test/java/misc/test

public class Foo implements Serializable {
    private String string;
    private transient String s2;
    private transient JsonNode jsonNode;

    public Foo(String string, String s2, JsonNode jsonNode) {
        this.string = string;
        this.s2 = s2;
        this.jsonNode = jsonNode;
    }

    private void writeObject(ObjectOutputStream out) throws IOException {
        out.defaultWriteObject();
        out.writeObject(s2);
        if (this.jsonNode != null) (new ObjectMapper()).writeValue(out, jsonNode);
    }

    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
        in.defaultReadObject();
        s2 = (String) in.readObject();
        if (in.available() > 0) this.jsonNode = (new ObjectMapper()).readValue(in, JsonNode.class);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Foo)) return false;

        Foo foo = (Foo) o;

        if (string != null ? !string.equals(foo.string) : foo.string != null) return false;
        if (s2 != null ? !s2.equals(foo.s2) : foo.s2 != null) return false;
        return !(jsonNode != null ? !jsonNode.equals(foo.jsonNode) : foo.jsonNode != null);

    }

    @Override
    public int hashCode() {
        int result = string != null ? string.hashCode() : 0;
        result = 31 * result + (s2 != null ? s2.hashCode() : 0);
        result = 31 * result + (jsonNode != null ? jsonNode.hashCode() : 0);
        return result;
    }
}

public class SerializationTest {
    @Test
    public void testSerialization() {
        JsonNode node = (new ObjectMapper()).valueToTree("Test");

        // This one succeeds
        runTest(null);

        // This on fails
        runTest(node);
    }

    private void runTest(JsonNode jsonNode) {
        Foo foo = new Foo("Bar", "s2", jsonNode);

        String fileName = "foo.ser";
        try (
                OutputStream file = new FileOutputStream(fileName);
                OutputStream buffer = new BufferedOutputStream(file);
                ObjectOutput output = new ObjectOutputStream(buffer);
        ){
            output.writeObject(foo);
        }
        catch(IOException ex){
            ex.getStackTrace();
        }

        Foo fooNew = null;

        //deserialize the ser file
        try(
                InputStream file = new FileInputStream(fileName);
                InputStream buffer = new BufferedInputStream(file);
                ObjectInput input = new ObjectInputStream (buffer);
        ){
            //deserialize the Object
            fooNew = (Foo) input.readObject();
        }
        catch(ClassNotFoundException ex){
            ex.printStackTrace();
        }
        catch(IOException ex){
            ex.printStackTrace();
        }

        Assert.assertEquals(foo, fooNew);
    }

}

1 个答案:

答案 0 :(得分:0)

不确定你为什么要这样做,为什么你想让你的类中的JSonNode可序列化?
使用Jackson来序列化/反序列化数据结构,而不是JSon“元素”。