微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决序列化和反序列化具有 vavr 列表的 Java 对象

我有一个带有 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
      } 
    }

其中备注是对象内的 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?

解决方法

您可以使用 jackson 轻松地将 vavr 对象序列化/反序列化为 JSON。您需要做的是在 VavrModule 的实例上注册 ObjectMapper

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();
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。