- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我知道 Gson 没有类似的功能,但是有没有办法像 @JsonUnwrap
那样添加对展开 Json 字段的支持?
目标是允许这样的结构:
public class Person {
public int age;
public Name name;
}
public class Name {
public String first;
public String last;
}
被(反)序列化为:
{
"age" : 18,
"first" : "Joey",
"last" : "Sixpack"
}
代替:
{
"age" : 18,
"name" : {
"first" : "Joey",
"last" : "Sixpack"
}
}
我知道它可能会变得相当复杂,所以我不是在寻找完整的解决方案,只是寻找一些高级指南(如果可行的话)。
最佳答案
我粗略地实现了一个支持此功能的解串器。它是完全通用的(与类型无关),但也很昂贵且脆弱,我不会将它用于任何严肃的事情。我发帖只是为了向其他人展示我的成果,如果他们最终需要做类似的事情。
public class UnwrappingDeserializer implements JsonDeserializer<Object> {
//This Gson needs to be identical to the global one, sans this deserializer to prevent infinite recursion
private Gson delegate;
public UnwrappingDeserializer(Gson delegate) {
this.delegate = delegate;
}
@Override
public Object deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
Object def = delegate.fromJson(json, type); //Gson doesn't care about unknown fields
Class raw = GenericTypeReflector.erase(type);
Set<Field> unwrappedFields = ClassUtils.getAnnotatedFields(raw, GsonUnwrap.class);
for (Field field : unwrappedFields) {
AnnotatedType fieldType = GenericTypeReflector.getExactFieldType(field, type);
field.setAccessible(true);
try {
Object fieldValue = deserialize(json, fieldType.getType(), context);
field.set(def, fieldValue);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
return def;
}
}
然后可以通过 new GsonBuilder().registerTypeHierarchyAdapter(Object.class, new UnwrappingDeserializer(new Gson())).create()
或通过 为特定类型注册全局registerTypeAdapter
.
注意事项:
GsonUnwrap
,将结果缓存在并发映射中,并且仅在需要时才执行此过程。否则它应该立即返回 def
GenericTypeReflector
来自 GeAnTyRef ClassUtils#getAnnotatedFields
是我自己的实现,但它没有做任何特别的事情 - 它只是为类层次结构递归地收集声明的字段(通过 Class#getDeclaredFields
) GsonUnwrap
只是一个简单的自定义注解我想序列化也可以做类似的事情。示例链接自 Derlin's answer可以作为起点。
关于java - 如何实现与@JsonUnwrap 等效的 Gson,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43294694/
我有一个相对复杂的对象,其中包含许多字段。我需要使用自定义序列化程序序列化其中一个字段,但需要模拟 @JsonUnwrapped 功能。 为了简单起见,我将其削减为两个字段: public class
我在 SpringBoot 的上下文中遇到了这个问题,但它似乎只是 Jackson 的问题。 我有一个包含多个字段的数据对象。 DataObject 包含在模型中。 DataObject 的所有字段都
我正在使用 jackson 2.1.0。鉴于: public static final class GetCompanies { private final List companies;
我有两门课: public class ResponseInfo { private final int code; private final String description;
我可以有条件地使用@JsonUnwrapped吗?我不想在序列化期间使用它,但想在反序列化对象时使用它。 一种方法是创建两个不同的类或创建一个子类来覆盖在序列化和反序列化时需要表现不同的属性。这听起来
当我在嵌套字段上使用@JsonUnwrapped时: public class Person{ private int id; @JsonUnwrapped private F
我有一个像这样的 POJO: class Registration { String name; @JsonUnwrapped ModifiableDetails detail
我知道 Gson 没有类似的功能,但是有没有办法像 @JsonUnwrap 那样添加对展开 Json 字段的支持? 目标是允许这样的结构: public class Person { publ
我有两个这样的类: public class A { String aProp = "aProp"; public String getAProp() { return
假设我有以下类(class): public class Parent { public int age; @JsonUnwrapped public Name name; } 生成 JS
我有一个对象,其中包含另一个对象属性,如下所示: import com.fasterxml.jackson.annotation.JsonPropertyOrder; import com.faste
我想做什么 我想使用 Jackson 反序列化多态类型,使用标准 @JsonTypeInfo 注释,如下所示: @JsonTypeInfo(use = JsonTypeInfo.Id.NAME,
我正在尝试编写一个简单的类来验证 JSON 输入字符串是否可以转换为目标 JAVA 对象。如果在输入 JSON 字符串中发现任何未知字段,则 validator 应该失败。这一切都按预期工作,除非我用
我有一个 POJO,其中包含另一个 POJO: public class Outer { private Long id; private String name; @Json
所以,我有这个类,使用 @EmbeddedId 和 @JsonUnwrapped 注释 @Entity class Order { @EmbeddedId @JsonUnwrapped
我有一个类: class Car { private Engine myEngine; @JsonProperty("color") private String myColo
我想使用组合模式来重用类的公共(public)部分,就像我在 Jackson 中使用 @JsonUnwrapped 所做的那样,而不在 mongodb 文档中添加额外级别的结构,例如: class A
我是一名优秀的程序员,十分优秀!