java - How to deserialize single JSON field with Jackson, having JSON path and DTO?
问题描述
Having JSON path String, like "foo.bar[1].baz", and corresponding DTO class in Java, how can I implement a generic method, that would deserialize single field like ObjectMapper#readValue(String,Class) would? By "corresponding DTO" I mean DTO for type that contains the path, not the type of json path object. In other words, DTO contains foo field of some type, and that type has bar list of objects with baz field. E.g.
public class MyDto {
@JsonDeserialize(using = MyDeserializer.class)
public LocalDate date;
public String str;
}
public class Example {
public static void main(String[] args){
String json = "{\"date\":\"2021-01-01\",\"str\":\"bar\"}";
Object deserializedPath = Example.deserialize(json, "date", MyDto.class);
}
public static Object deserialize(String json, String path, Class<?> dto) {
// Here path should be deserialized with MyDeserializer(in this example),
// and it should work for all kinds of JSON/DTO regardless of deserializer.
}
}
解决方案
推荐阅读
- sql-server - 如何使用域/路径在 sql server 中设置服务器名称?
- python - 如何在系统托盘或菜单栏上显示文本?
- python - 看不到更新变量
- c# - 使用.net core c#将json字符串从一种类型转换为另一种类型
- javascript - React 测试库 - onClick/onChange 错误中的上下文
- reactjs - React 组件在空时和从服务器加载时收缩
- c# - 解析完成前遇到流结束意外异常
- php - 使用 PHP 访问 sdtClass 对象
- javascript - 页面重新加载时自动提交表单
- python - 为什么从以下代码中删除 Py_Finalize() 使其工作?