java - 使 JsonNode 可序列化

标签 java json serialization jackson

这看起来很简单,但我未能将序列化的 JsonNode 反序列化。这是我的测试类

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

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

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

    private void writeObject(ObjectOutputStream out) throws IOException {
        out.defaultWriteObject();
        if (this.jsonNode != null) out.writeObject((new ObjectMapper()).writeValueAsBytes(this.jsonNode));
//        out.writeObject(this.jsonNode.textValue());
    }

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

当我尝试反序列化时出现此错误

com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input

这是单元测试

import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.testng.annotations.Test;

import java.io.*;

import static org.testng.Assert.assertEquals;

public class FooTest {
    @Test
    public void testSerialization() {
        JsonNodeFactory nodeFactory = new JsonNodeFactory(false);
        ObjectNode node = nodeFactory.objectNode();
        ObjectNode child = nodeFactory.objectNode(); // the child
        child.put("message", "test");
        node.put("notification", child);

        Foo foo = new Foo("Bar", node);

        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();
        }

        assertEquals(foo, fooNew);
    }
}

最佳答案

您的读写操作不匹配。

在写入端,您使用 ObjectOutputStream.writeObject(Object) 编写包含序列化 JSON 内容的 byte[]。在读取端,您尝试使用 ObjectMapper.readValue(InputStream, Class) 从流中读取原始字节,而您实际上需要首先读取一个 byte[] 对象是你写的,然后使用 ObjectMapper.readValue(byte[], Class)

或者,可能更好的解决方案是您可以在写入端使用 ObjectMapper.writeValue(OutputStream, Object)

试试这个:

private void writeObject(ObjectOutputStream out) throws IOException {
    out.defaultWriteObject();
    if(jsonNode == null){
        out.writeBoolean(false);
    } else {
        out.writeBoolean(true);
        new ObjectMapper().configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false).writeValue(out, jsonNode);
    }
}

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    in.defaultReadObject();
    if(in.readBoolean()){
        this.jsonNode = new ObjectMapper().configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false).readValue(in, JsonNode.class);
    }     
}

关于java - 使 JsonNode 可序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31861354/

相关文章:

JavaFX 动态添加元素到 GridPane

java - 将 List<Object> 转换为 List<HashMap<String,Double>> 以获取 json 数组

json - 将 JSON 结构附加到 JSON 文件 Golang

java - 将手动创建的 JSONObject 的内容传递给 Jackson

javascript - 使用 jQuery.serialize() 序列化时如何对表单内容进行 *HTML* 编码?

相当于 mysql 字符串搜索运算符 'like' 的 java

java - 名为 ThreadSafe 的注解

c# - 如何比较两个 .NET 对象图的差异?

java - 无法在mysql表字段中存储完整的json字段

gwt - 使用带有 HashMap 的 GWT 的 AutoBean 反序列化时出现 NullPointerException