How can I serialize / deserialize java.util.stream.Stream using Jackson?

Assuming I have the following object

public class DataObjectA { private Stream<DataObjectB> dataObjectBStream; } 

How can I serialize them using Jackson?

+2
source share
3 answers

As others have pointed out, you can only repeat once over the stream. If this works for you, you can use this for serialization:

 new ObjectMapper().writerFor(Iterator.class).writeValueAsString(dataObjectBStream.iterator()) 

If you are using Jackson version 2.5, use writerWithType() instead of writerFor() .

+3
source

You do not want.

A Stream is a one-time chain of operations and should never be permanent. Even storing it in the instance field, as in your question, is an indicator of a misunderstanding of its purpose. After applying terminal operation to a thread, this is useless, and threads cannot be cloned. In this case, it makes no sense to remember an unused stream in a field.

Since the only operations offered by Stream associate more operations with the pipeline and finally evaluate it, there is no way to query its state so that it can create an equivalent stream regarding its behavior. Therefore, no storage system can save it. The only thing that the infrastructure can do is move the resulting elements of the stream operation and save them, but this means efficient storage of some Stream collection. In addition, the one-time nature of the Stream also implies that the structure of the vault passing through the stream to store items had a side effect that made the stream unusable at the same time.

If you want to store items, resort to the usual Collection .

On the other hand, if you really want to save the behavior, you will end up saving an instance of the object whose actual class implements the behavior. This still works with Stream , since you can save an instance of a class that has a factory method that creates the desired stream. Of course, you do not actually save the behavior, but a symbolic link to it, but this always happens when you use the OO framework to store behavior, not data.

+1
source

See https://github.com/FasterXML/jackson-modules-java8/issues/3 for an open issue to add java.util.Stream support for Jackson. There was included a preliminary version of the code. (editing: now it is integrated and supported in 2.9.0).

Streaming support seems to work in a natural / safe way if the stream is a top-level object that you (de) serialize, for example, return java.util.stream.Stream<T> from a JAX-RS resource or read Stream from JAX -RS.

A stream as a member variable of a serialized object (de), as you have in your example, is more complicated because it is changed and used once:

private Stream<DataObjectB> dataObjectBStream ;

Assuming it was supported, all the caveats around storing thread references will apply. You won’t be able to serialize the object more than once, and once you deserialize the wrapper object, it is assumed that the stream element will retain a live connection through the JAX-RS client and the HTTP connection, which may cause surprises.

+1
source

Source: https://habr.com/ru/post/1258009/


All Articles