我对 Google 的 AutoBean 序列化和反序列化有一些问题。我有一个包含原始类型和地图的 AutoBean。我可以毫无问题地序列化和反序列化原始类型,但是当我尝试读取反序列化的 Map 时,我得到 NullPointerException。你以前遇到过类似的问题吗?有一个代表我的问题的 JUnit 测试。前两个断言通过,但第三个失败。
public class AutoBeanTest {
@Test
public void test() throws Exception {
MyFactory myFactory = AutoBeanFactorySource.create(MyFactory.class);
Options options = myFactory.options().as();
options.setMyInt(5);
HashMap<Double, Boolean> map = newHashMap();
map.put(8.0, true);
map.put(9.1, false);
options.setMyMap(map);
Options deserialized = AutoBeanCodex.decode(myFactory, Options.class, AutoBeanCodex.encode(AutoBeanUtils.getAutoBean(options)).getPayload()).as();
assertEquals(deserialized.getMyInt(),5);
assertTrue(options.getMyMap().containsKey(8d));
assertTrue(deserialized.getMyMap().containsKey(8d));
}
public interface MyFactory extends AutoBeanFactory {
AutoBean<Options> options();
}
public interface Options {
public int getMyInt();
void setMyInt(int myInt);
Map<Double, Boolean> getMyMap();
void setMyMap(Map<Double, Boolean> myMap);
}
}