json - 如何序列化 AutoBean (GWT) 中的列表?

标签 json gwt serialization autobean

我试图弄清楚如何在 GWT 中使用 AutoBean 序列化列表,但我不断收到空指针异常。

这是我所拥有的:

    GuideCreatorFactory beanFactory = AutoBeanFactorySource.create(GuideCreatorFactory.class);

    List<Guide> guides = new LinkedList<Guide>();
    Guide guide = new Guide();
    guide.setText("this is the text");
    guide.setTitle("this is the title");
    guides.add(guide);

    GuideCreatorList<Guide> impl = new GuideCreatorListImpl();
    impl.setGuides(guides);

    System.out.println("Serializing the given parameter to JSON");

    // Fails on the below lines w/ NPE
    AutoBean<GuideCreatorList> bean = beanFactory.create(GuideCreatorList.class, impl);
    String json = AutoBeanCodex.encode(bean).getPayload();
    System.out.println("guides as json: " + json);

谁能帮我指出正确的方向?非常感谢你。

这是支持的类和接口(interface):
public interface GuideCreatorFactory extends AutoBeanFactory {
  AutoBean<GuideCreator> createGuide();

  AutoBean<GuideCreatorList> createGuideList();
}


public interface GuideCreator {

  public String getText();

  public void setText(String text);

  public String getTitle();

  public void setTitle(String title);

}


public interface GuideCreatorList<T extends GuideCreator> {
  public List<T> getGuides();

  public void setGuides(List<T> guides);
}



class GuideCreatorListImpl implements GuideCreatorList<Guide> {
    private List<Guide> guides;

    public GuideCreatorListImpl() {

    }

    @Override
    public List<Guide> getGuides() {
      return guides;
    }

    @Override
    public void setGuides(List<Guide> guides) {
      this.guides = guides;
    }
    };

这是NPE:
java.lang.NullPointerException
    at com.google.web.bindery.autobean.shared.impl.AutoBeanCodexImpl.doEncode(AutoBeanCodexImpl.java:558)
    at com.google.web.bindery.autobean.shared.impl.AutoBeanCodexImpl$ObjectCoder.encode(AutoBeanCodexImpl.java:321)
    at com.google.web.bindery.autobean.shared.impl.AutoBeanCodexImpl$CollectionCoder.encode(AutoBeanCodexImpl.java:163)
    at com.google.web.bindery.autobean.shared.impl.AutoBeanCodexImpl$PropertyGetter.encodeProperty(AutoBeanCodexImpl.java:413)
    at com.google.web.bindery.autobean.shared.impl.AutoBeanCodexImpl$PropertyGetter.visitReferenceProperty(AutoBeanCodexImpl.java:389)
    at com.google.web.bindery.autobean.shared.AutoBeanVisitor.visitCollectionProperty(AutoBeanVisitor.java:229)
    at com.google.web.bindery.autobean.vm.impl.ProxyAutoBean.traverseProperties(ProxyAutoBean.java:300)
    at com.google.web.bindery.autobean.shared.impl.AbstractAutoBean.traverse(AbstractAutoBean.java:166)
    at com.google.web.bindery.autobean.shared.impl.AbstractAutoBean.accept(AbstractAutoBean.java:101)
    at com.google.web.bindery.autobean.shared.impl.AutoBeanCodexImpl.doEncode(AutoBeanCodexImpl.java:558)
    at com.google.web.bindery.autobean.shared.AutoBeanCodex.encode(AutoBeanCodex.java:83)
    at com.districthp.core.ui.client.review.JsonSerializationText.testMyObject(JsonSerializationText.java:82)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

最佳答案

不幸的是,这是一个已知问题:https://github.com/gwtproject/gwt/issues/6903

问题是列表中的项目没有被包裹到 AutoBean 中。所以AutoBeanUtils#getAutoBeanAutoBeanCodexImpl.ObjectCoder#encode返回 null ,因此 AutoBeanCodexImpl#doEncode 中的 NPE .

解决方法是将列表项替换为 AutoBean。 s 包装实际值。

关于json - 如何序列化 AutoBean (GWT) 中的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11530451/

相关文章:

java - 创建 REST/JSON API

javascript - 将内部 json 数组与空格组合,将外部数组与换行符组合

javascript - 如何列出 Typeahead.js 和/或 Bloodhound 引擎的对象?

c# - 为 protobuf-net 生成序列化程序集失败

javascript - JSON 不适用于 OnChange 事件

GWT 通过属性获取元素

css - GWT 共享 CSS 样式不起作用

gwt - 在 IntelliJ 上编译 GWT 模块

C# "Enum"序列化 - 反序列化为静态实例

java - 如何在 Hadoop 中序列化 Java 对象?