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

前端 未结 4 1564
星月不相逢
星月不相逢 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 14:37

    As others have pointed out, you can only iterate once over a stream. If that works for you, you can use this to serialize:

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

    If you're using a Jackson version prior to 2.5, use writerWithType() instead of writerFor().

提交回复
热议问题