Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

serialization - How to serialize JsonNode

I am saving below instance to Aerospike Database. My class which I want to serialize

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

Using above code, saving to Database is working fine(Serialization).

but when I try to get data from Database(Deserialization), I am facing below exception.

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)

Please let me know if I am missing something.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think we can't use ObjectOutputStream and ObjectInputStream more than one time. I resolved it by writing writeObject and readObject function.

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

Or you can create a POJO with 2 fields as JsonNode and serialize the POJO.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...