- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
在我的 Java 应用程序中,我定义了两个类,称为 A
和 B
其中 B
是 A
的内部类.两者都定义为可序列化
public class A implements Serializable {
int attrParent;
List<B> items = new ArrayList<B>();
public void setAttrParent(int attrParent) {
this.attrParent = attrParent;
}
public int getAttrParent() {
return attrParent;
}
public class B implements Serializable {
private int attr;
public void setAttr(int attr) {
this.attr = attr;
}
public int getAttr() {
return attr;
}
public int getSomeCalculationValue() {
return this.attr * A.this.attrParent; // Problems occurs here
}
}
}
在使用 GSON 序列化此对象之前,getSomeCalculationValue
工作良好。但是,在序列化和反序列化之后,getSomeCalculationValue
失败并显示 NullPointerException
.
发生这种情况是因为内部类 B
没有对外部类的引用 A
再这样,A.this
失败。
有谁知道我该如何解决这个问题,即在反序列化该对象时将内部引用恢复为外部引用?
最佳答案
正如 Gson 文档所说:
Gson can serialize static nested classes quite easily.
Gson can also deserialize static nested classes. However, Gson can not automatically deserialize the pure inner classes since their no-args constructor also need a reference to the containing Object which is not available at the time of deserialization. You can address this problem by either making the inner class static or by providing a custom InstanceCreator for it.
将 B 更改为静态内部类是不可能的,因为您的方法需要引用 getSomeCalculationValue
中的外部类,因此,我已尝试使用 InstanceCreator< 解决您的问题
但解决方案有点难看,所以我建议你使用自定义反序列化。我稍微更改了您的 A
类,将项目设为公开,以便更轻松地创建我向您展示的示例。
public class ADeserializer implements JsonDeserializer<A> {
public A deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
A a = new A();
a.attrParent = json.getAsJsonObject().get("attrParent").getAsInt();
JsonArray ja = json.getAsJsonObject().get("items").getAsJsonArray();
for(JsonElement e: ja){
B b = a.new B();
b.setAttr(e.getAsJsonObject().get("attr").getAsInt());
a.items.add(b);
}
return a;
}
}
这就是我使用它的方式:
public class Q19449761 {
public static void main(String[] args) {
A a = new A();
a.setAttrParent(3);
B b = a.new B();
b.setAttr(10);
a.items.add(b);
System.out.println("Before serializing: " + a.items.get(0).getSomeCalculationValue());
Gson g = new Gson();
String json = g.toJson(a, A.class);
System.out.println("JSON string: " + json);
A test2 = g.fromJson(json, A.class);
try {
System.out.println("After standard deserialization: " +test2.items.get(0).getSomeCalculationValue());
} catch (Exception e) {
e.printStackTrace();
}
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(A.class, new ADeserializer());
A test3 = builder.create().fromJson(json, A.class);
System.out.println("After custom deserialization: " + test3.items.get(0).getSomeCalculationValue());
}
}
这是我的处决:
Before serializing: 30
JSON string: {"attrParent":3,"items":[{"attr":10}]}
java.lang.NullPointerException
at stackoverflow.questions.q19449761.A$B.getSomeCalculationValue(A.java:32)
at stackoverflow.questions.q19449761.Q19449761.main(Q19449761.java:26)
After custom deserialization: 30
最后两点:
Serializable
接口(interface),JSON与Java序列化没有任何共同点关于java - GSON 不会反序列化对外部类的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19449761/
我想知道外部css文件是否有单行注释。 我正在使用外部 css 文件 (styles.css),我想在该文件中使用单行注释。我尝试使用“//”、“#”、“--”,但它们都不起作用。我知道我们可以在单行
#c{ height: 160px; width: 250px; background-color: orange; } img{ width: 100%; h
我正在与 Ivy 作斗争(我尝试过 maven,但在为 Hibernate 设置 JBoss 存储库时遇到了一个更困难的事件)。 小问题 - 我正在使用这个很棒的包: http://ooweb.sou
据我所知,在引用外部js文件时使用“async”,例如: 可能会提供最佳性能。但是,因此引用的脚本以任何顺序执行似乎也存在潜在问题。但对于 jQuery 和 jQueryUI(或任何基于 jQuer
我的整个想法有点复杂,但只是为了将事情分解,使它们简单明了……我在一个小部件上有一个按钮,我需要用它打开官方 Facebook 应用。 这是我使用的代码: Intent PlacesIntent =
我需要以 JSON 格式向远程服务器中的 HTTPS 网页发送 POST 并接收 JSON 格式的答案。 要发送到远程服务器的数据是从 URL 中获取的(栏) 我使用以下代码,没有结果,但有 20 秒
我在尝试访问外部 API 时收到套接字挂断错误。即使使用我的 API key (用于在网站上演示 API 的开发 API key ),也可以手动访问外部 API ( click here ) 并返回结
我正在尝试对当前上下文之外的 URL 执行 POST 请求,看起来 Spring 无法理解它。 测试代码: String content = mvc .perf
我公司有一个Google Brand Account管理我们的 YouTube (YT) channel 。我们正在尝试使用 YouTube Analytics & Reporting API 自动导
我们有一个内部部署的 MS CRM 2011,一些表单运行自定义 javascript 来预填充某些字段。数据来 self 们开发的 WCF RESTful Web 服务,该服务与 CRM 在同一台物
我正在尝试对外部 url 执行 ajax 请求。现在我正在用 php 执行此操作 作为 $data = array( 'TokenID' => $tokenid, 'A
我有一个 node.js express 网站,我在其中使用 jwt token 对我的用户进行身份验证。我想包括外部网络应用程序,即 agendash进入我的管理界面。 Agendash 包含在这样
当触发效果时,我想在单元测试中测试两个可观察对象,以获得这部分代码的 100% 代码覆盖率。因为 window.location.href 被触发,我无法正确测试它。 export class Rou
我正在 Spring Boot Java 应用程序中为服务类构建单元测试。 服务类对返回 JSON 响应的 REST API 服务进行外部调用。我正在使用 Mockito 模拟这个调用。我在模拟服务器
我尝试通过 jQuery 中的 $.post 发布大量文本并收到 406 响应。在 300 个字符左右的情况下它可以正常工作。下面是我的代码: index.php html jQuery
目前,我需要在 iOS UI 自动化测试脚本中从外部 JAR 文件调用一些 API。例如,在我的 UI 自动化测试脚本中,我可以输入电子邮件地址并向该地址发送电子邮件。接下来,我需要对外部 JAR 文
我在一个一直工作到上周五的 WP 网站上使用 cURL 创建了一些“基本”API 调用。 从那时起,我收到此错误消息。 错误n:60, 错误:SSL 证书问题:证书已过期。 现在我刚刚检查了https
我是一名优秀的程序员,十分优秀!