fir*_*ter 1 java json jackson vavr
我有一个带有 vavr 列表的 Java 对象。
@Value
@Builder(toBuilder = true)
public class Customer {
private final List<Remark> remarks;
}
Run Code Online (Sandbox Code Playgroud)
当我序列化对象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
}
}
Run Code Online (Sandbox Code Playgroud)
其中备注是对象内的 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" : {
Run Code Online (Sandbox Code Playgroud)
为什么尝试反序列化时序列化的 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());
}
Run Code Online (Sandbox Code Playgroud)
Java代码中是否有其他方法以文本格式打印vavr对象并将其转换回POJO?
您可以使用 jackson 轻松地将 vavr 对象序列化为 JSON 或从 JSON 序列化/反序列化。VavrModule您需要做的是在以下实例上注册ObjectMapper:
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new VavrModule());
Run Code Online (Sandbox Code Playgroud)
为此,您需要添加对vavr-jackson 的VavrModule依赖
编辑
在这里您可以找到一个完整的工作示例:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1197 次 |
| 最近记录: |