java 当前推荐的将 byte[] 转换为 JsonNode 并返回的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28273827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Current recommended way to convert byte[] to JsonNode and back
提问by Nakedible
I'd like to read a JSON "tree" from a java byte array and write a JSON "tree" back out as java byte array using Hymanson. One way to do it is shown below:
我想从 Java 字节数组中读取一个 JSON“树”,并使用 Hymanson 将一个 JSON“树”写回为 Java 字节数组。一种方法如下所示:
ObjectMapper om = new ObjectMapper();
JsonNode old = om.createObjectNode();
byte[] arr = om.writeValueAsBytes(old);
JsonNode new = om.readTree(arr);
However, Hymanson these days recommends the use of ObjectReader and ObjectWriter instead of ObjectMapper, because of thread safety in configuration, but also because of optimizations that might be relevant for them only. But, ObjectReader does not support readTree with byte arrays directly, and writeValueAsBytes is more generic than writeTree so there might be a way (and a reason) to skip the type mapping logic somehow.
然而,Hyman逊最近推荐使用 ObjectReader 和 ObjectWriter 而不是 ObjectMapper,因为配置中的线程安全,也因为可能只与它们相关的优化。但是,ObjectReader 不直接支持带有字节数组的 readTree,并且 writeValueAsBytes 比 writeTree 更通用,因此可能有一种方法(和理由)以某种方式跳过类型映射逻辑。
So, today, with the most recent Hymanson (2.5), what is the fastest/best/recommended way to do these two conversions?
那么,今天,使用最新的 Hymanson (2.5),进行这两个转换的最快/最佳/推荐方法是什么?
回答by wassgren
The problem with using the ObjectMapper
directly is that if you alter the configuration of the mapper it can lead to problems. However, if you do not change the underlying config you should be safe anyway (more reading here).
ObjectMapper
直接使用 的问题在于,如果您更改映射器的配置,则可能会导致问题。但是,如果您不更改底层配置,无论如何您应该是安全的(更多阅读此处)。
But if you use the ObjectReader
and ObjectWriter
you are totally safe, even if you actually do alter the configuration of the mapper. This is possible since the reader/writer are immutable and it is therefore not possible to change the underlying state.
但是,如果您使用ObjectReader
并且ObjectWriter
您是完全安全的,即使您确实更改了映射器的配置。这是可能的,因为读取器/写入器是不可变的,因此不可能改变底层状态。
So, to read/write to bytes the following approach works fine:
因此,要读取/写入字节,以下方法可以正常工作:
ObjectMapper om = new ObjectMapper();
JsonNode oldNode = om.createObjectNode();
// Writing...
// Create an immutable writer (in this case using the default settings)
final ObjectWriter writer = om.writer();
// Use the writer for thread safe access.
final byte[] bytes = writer.writeValueAsBytes(oldNode);
// Reading...
// Create an immutable reader
final ObjectReader reader = om.reader();
// Use the reader for thread safe access
final JsonNode newNode = reader.readTree(new ByteArrayInputStream(bytes));
So, basically you can use the same principles (with the byte streams) but if you need to be sure that you are using thread safe access to the mapper you should access the reading/writing via the ObjectReader
and ObjectWriter
.
因此,基本上您可以使用相同的原则(使用字节流),但是如果您需要确保使用线程安全访问映射器,您应该通过ObjectReader
and访问读/写ObjectWriter
。
The writeValueAsBytes
is described like this in the JavaDoc:
的writeValueAsBytes
是这样的描述在JavaDoc:
Functionally equivalent to calling
writeValue(Writer,Object)
withjava.io.ByteArrayOutputStream
and getting bytes, but more efficient.
在功能上等同于调用
writeValue(Writer,Object)
与java.io.ByteArrayOutputStream
和获取字节,但更有效。
For the reading you can simply use the readTree(InputStream)
version.
对于阅读,您可以简单地使用该readTree(InputStream)
版本。