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 Jackson. One way to do it is shown below:
ObjectMapper om = new ObjectMapper();
JsonNode old = om.createObjectNode();
byte[] arr = om.writeValueAsBytes(old);
JsonNode new = om.readTree(arr);
However, Jackson 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.
So, today, with the most recent Jackson (2.5), what is the fastest/best/recommended way to do these two conversions?
Best Solution
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).But if you use the
ObjectReader
andObjectWriter
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.So, to read/write to bytes the following approach works fine:
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
andObjectWriter
.The
writeValueAsBytes
is described like this in the JavaDoc:For the reading you can simply use the
readTree(InputStream)
version.