How to use Jackson to serialize / deserialize Java util. stream. Stream?

Suppose I have the following objects

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

How do I serialize them using Jackson?

Solution

As others have pointed out, you can only iterate over a stream once If this is useful to you, you can use it to serialize:

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

If you are using Jackson before 2.5, use writerwithtype() instead of writerfor()

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>