我以前使用过Gson没有任何问题,但现在看起来很奇怪。我试图像这样反序列化我的对象:
try (Writer writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(path), Charset.defaultCharset()))) {
Map<String, List<String>> map = new HashMap<String, List<String>>();
map.put("sun", new ArrayList<String>(){{
add("warm");
add("yellow");
add("luminescent");
}});
map.put("tomato", new ArrayList<String>(){{
add("red");
add("tasty");
}});
Gson gson = new Gson();
writer.write(gson.toJson(map));
}catch(Exception ex){
System.out.println("ERROR: " + ex.getMessage());
}
给了我一个空对象:
{}
如何正确序列化和反序列化上面的对象?