MessagePack Java Jackson 在不关闭输入流(input stream)的情况下反序列化多变量
com.fasterxml.jackson.databind.ObjectMapper 在读取输入流变量的时候默认的将会关闭输入流。
如果你不希望关闭输入流,你可以设置 JsonParser.Feature.AUTO_CLOSE_SOURCE 参数为 false。
/** * Serialization Not Close input stream */ @Test public void testMessagePackSerializationNotCloseInputStream() { logger.debug("testMessagePackSerializationNotCloseInputStream"); try { File tempFile = File.createTempFile("messagepack-", "-cwiki.us"); MessagePacker packer = MessagePack.newDefaultPacker(new FileOutputStream(tempFile)); packer.packInt(42); packer.packString("Hello"); packer.close(); FileInputStream in = new FileInputStream(tempFile); ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory()); objectMapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false); System.out.println(objectMapper.readValue(in, Integer.class)); System.out.println(objectMapper.readValue(in, String.class)); in.close(); tempFile.deleteOnExit(); } catch (IOException ex) { logger.error("Serialize Error", ex); } }
https://www.cwiki.us/display/Serialization/MessagePack+Jackson+Dataformat

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
MessagePack Java Jackson 在不关闭输出流(output stream)的情况下序列化多变量
com.fasterxml.jackson.databind.ObjectMapper在默认的情况下在写出输入后将会关闭输出流(output stream)。 如果你希望序列化多值变量在同一个输出流的情况下,你不希望在输出完一个就关闭输出流,你可以设置JsonGenerator.Feature.AUTO_CLOSE_TARGET参数为 False。 本测试方法,可以在https://github.com/cwiki-us-demo/serialize-deserialize-demo-java/blob/master/src/test/java/com/insight/demo/serialize/MessagePackSerializer.java中找到。 /** * Serialization Not Close output stream */ @Test public void testMessagePackSerializationNotCloseOutputStream() { logger.debug("testMessagePackSerializationNotClo...
- 下一篇
MessagePack Java Jackson Dataformat 不使用 str8 数据类型的序列化
老的 msgpack-java(例如 0.6.7)并不支持MessagePack str8 数据类型。 当你的希望的你的应用程序需要支持老的版本的话,你需要禁用这个数据类型,例如使用下面的语句: MessagePack.PackerConfig config = new MessagePack.PackerConfig().withStr8FormatSupport(false); ObjectMapper mapperWithConfig = new ObjectMapper(new MessagePackFactory(config)); // This string is serialized as bin8 type byte[] resultWithoutStr8Format = mapperWithConfig.writeValueAsBytes(str8LengthString); https://www.cwiki.us/display/Serialization/MessagePack+Jackson+Dataformat
相关文章
文章评论
共有0条评论来说两句吧...