序列化和反序列化 Java 具有 vavr 列表的对象

Serialize and Deserialize Java object having vavr list

我有一个带有 vavr 列表的 Java 对象。

@Value
@Builder(toBuilder = true)
public class Customer {

    private final List<Remark> remarks;

}

当我序列化对象 objectwriter.writeValueAsString(currentDossier) 并打印值时,我在 JSON 输出中看到,

    { "remarks" : {
        "empty" : true,
        "lazy" : false,
        "async" : false,
        "traversableAgain" : true,
        "sequential" : true,
        "ordered" : false,
        "singleValued" : false,
        "distinct" : false,
        "orNull" : null,
        "memoized" : false
      } 
    }

其中 remarks 是对象内部的一个 vavr 列表字段。

现在,如果我尝试将相同的值反序列化到对象 objectMapper.readValue(json, Customer.class) 中,则会出现错误:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `io.vavr.collection.List` 
(no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
 at [Source: (String)"{"remarks" : {

为什么在尝试反序列化时序列化的 JSON 文本不起作用?

  @Test
  public void mapperForVavrList() throws JsonProcessingException {
    Customer cus = Customer.builder().remarks(List.empty()).build();

    ObjectWriter ow = new ObjectMapper()
      .writer().withDefaultPrettyPrinter();

    String json1 = ow.writeValueAsString(cus);

    log.info(json1);

    ObjectMapper objectMapper = new ObjectMapper();
    Customer cus2 = objectMapper.readValue(json1, Customer.class);

    log.info(cus2.toString());

  }

在 Java 代码中是否有任何其他方法以文本格式打印 vavr 对象并将其转换回 POJO?

您可以轻松地 serialize/deserialize vavr 对象 to/from JSON 使用 jackson。您需要做的是在 ObjectMapper:

的实例上注册 VavrModule
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new VavrModule());

要获得 VavrModule 你需要添加对 vavr-jackson

的依赖

编辑

在这里你可以找到一个完整的工作示例:

import com.fasterxml.jackson.databind.ObjectMapper;
import io.vavr.collection.List;
import io.vavr.jackson.datatype.VavrModule;
import org.apache.commons.lang3.builder.ToStringBuilder;

import java.io.IOException;

class Lol2 {

    public static void main(String[] args) throws IOException {
        Customer c = new Customer();
        Remark r = new Remark();
        r.whatever = "whatever";
        c.remarks = List.of(r);

        ObjectMapper ow = new ObjectMapper();
        ow.registerModule(new VavrModule());

        System.out.println(c);
        String json = ow.writeValueAsString(c);
        System.out.println(json);
        Customer c2 = ow.readValue(json, Customer.class);
        System.out.println(c2);

    }
}

class Customer {

    List<Remark> remarks;

    public List<Remark> getRemarks() {
        return remarks;
    }

    public void setRemarks(List<Remark> remarks) {
        this.remarks = remarks;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this)
            .append("remarks", remarks)
            .toString();
    }
}

class Remark {

    String whatever;

    public String getWhatever() {
        return whatever;
    }

    public void setWhatever(String whatever) {
        this.whatever = whatever;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this)
            .append("whatever", whatever)
            .toString();
    }
}