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

前端 未结 4 1563
星月不相逢
星月不相逢 2021-01-14 14:22

Assuming I have the following object

public class DataObjectA {
    private Stream dataObjectBStream;
}

How can I serial

4条回答
  •  悲&欢浪女
    2021-01-14 15:03

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

    Streaming support feels like it would work naturally/safely if the stream is the top level object you were (de)serializing, eg returning a java.util.stream.Stream from a JAX-RS resource, or reading a Stream from a JAX-RS client.

    A Stream as a member variable of a (de)serialized object, as you have in your example, is trickier, because it's mutable and single use:

    private Stream dataObjectBStream;

    Assuming it was supported, all of the caveats around storing references to streams would apply. You wouldn't be able to serialize the object more than once, and once you deserialized the wrapping object presumably it's stream member would retain a live connection back through the JAX-RS client and HTTP connection, which could create surprises.

提交回复
热议问题