使JsonNode可序列化

编程入门 行业动态 更新时间:2024-10-11 03:23:45
本文介绍了使JsonNode可序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

This seems to be simple but I failed to get a serialized JsonNode deserialized. Here is my test class

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

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

When I tried to deserialize I got this error

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

推荐答案

您的读写操作不匹配。

Your read and write operations are not matched.

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

On the write side you use ObjectOutputStream.writeObject(Object) to write a byte[] containing the serialized JSON content. On the read side you try to read raw bytes off the stream with ObjectMapper.readValue(InputStream, Class) when you actually need to read a byte[] object first as that is what you wrote and then use ObjectMapper.readValue(byte[], Class).

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

Alternatively and probably a better solution is you could use ObjectMapper.writeValue(OutputStream, Object) instead on the write side.

试试这个:

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

更多推荐

使JsonNode可序列化

本文发布于:2023-11-25 09:16:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629186.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列化   JsonNode

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!