首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使JsonNode可序列化

使JsonNode可序列化
EN

Stack Overflow用户
提问于 2015-08-06 16:43:28
回答 1查看 10K关注 0票数 4

这似乎很简单,但我没有得到序列化的JsonNode反序列化。这是我的考试课

代码语言:javascript
复制
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);
    }
}

当我试图反序列化时,我得到了这个错误。

代码语言:javascript
复制
com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input

这是单元测试

代码语言:javascript
复制
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);
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-06 17:34:15

您的读和写操作不匹配。

在编写方面,您使用ObjectOutputStream.writeObject(Object)编写包含序列化的JSON内容的byte[]。在读取方面,当您实际需要读取一个ObjectMapper.readValue(InputStream, Class)对象时,您尝试使用byte[]读取流中的原始字节,因为这是您编写的,然后使用ObjectMapper.readValue(byte[], Class)

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

试试这个:

代码语言:javascript
复制
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);
    }     
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31861354

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档