Serialization 如何序列化JsonNode

Serialization 如何序列化JsonNode,serialization,deserialization,aerospike,Serialization,Deserialization,Aerospike,我正在将下面的实例保存到Aerospike数据库。 我要序列化的类 public class School implements Serializable { private static final long serialVersionUID = 1L; private JsonNode studentInfo; private JsonNode teacherInfo; private void writeObject(ObjectOutputStre

我正在将下面的实例保存到Aerospike数据库。 我要序列化的类

public class School  implements Serializable {
    private static final long serialVersionUID = 1L;

    private JsonNode studentInfo;

    private JsonNode teacherInfo;

    private void writeObject(ObjectOutputStream out) throws IOException {
        ObjectMapper mapper = new ObjectMapper();

        mapper.writeValue((OutputStream) out, studentInfo);
        mapper.writeValue((OutputStream) out, teacherInfo);

    }

    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
        ObjectMapper mapper = new ObjectMapper();

        this.studentInfo = mapper.readValue((InputStream) in, JsonNode.class);
        this.teacherInfo = mapper.readValue((InputStream) in, JsonNode.class);
    }
}
使用上述代码,保存到数据库工作正常(序列化)

但当我试图从数据库获取数据(反序列化)时,我面临以下异常

Caused by: com.aerospike.client.AerospikeException$Serialize: Error -10,1,30000,0,5,BB95B2FFB6EA79A 10.66.29.66 3030: ***com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input***
 at [Source: java.io.ObjectInputStream@6ff29830; line: 1, column: 0]
    at com.aerospike.client.command.Buffer.bytesToObject(Buffer.java:341)
    at com.aerospike.client.command.Buffer.bytesToParticle(Buffer.java:69)

如果我遗漏了什么,请告诉我。

我想我们不能多次使用ObjectOutputStream和ObjectInputStream。 我通过编写writeObjectreadObject函数解决了这个问题

private void writeObject(ObjectOutputStream out) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
       
        ArrayNode arrayNode = mapper.createArrayNode().add(this.studentInfo).add(this.teacherInfo);

        mapper.writeValue((OutputStream) out, arrayNode);
    }
    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
        ObjectMapper mapper = new ObjectMapper();
        ArrayNode arrayNode = null;
        arrayNode = mapper.readValue((InputStream) in, ArrayNode.class);
        this.studentInfo = arrayNode.studentInfo;
        this.teacherInfo = arrayNode.teacherInfo;
    }

或者您可以创建一个包含两个字段的POJO作为JsonNode,并序列化POJO。

我认为我们不能多次使用ObjectOutputStream和ObjectInputStream。 我通过编写writeObjectreadObject函数解决了这个问题

private void writeObject(ObjectOutputStream out) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
       
        ArrayNode arrayNode = mapper.createArrayNode().add(this.studentInfo).add(this.teacherInfo);

        mapper.writeValue((OutputStream) out, arrayNode);
    }
    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
        ObjectMapper mapper = new ObjectMapper();
        ArrayNode arrayNode = null;
        arrayNode = mapper.readValue((InputStream) in, ArrayNode.class);
        this.studentInfo = arrayNode.studentInfo;
        this.teacherInfo = arrayNode.teacherInfo;
    }
或者,您可以创建一个包含两个字段的POJO作为JsonNode,并序列化POJO