使用 YAML(SnakeYaml 库)进行 Java 序列化,HashMap 未显示在序列化输出中
当我序列化一个类对象时:
class MyClass{
String value;
public MyClass(){}
public void setValue(String value){
this.value = value;
}
public String getValue(){
return value;
}
}
并将其序列化为:
MyClass c1 = new MyClass();
c1.setValue("this is a value");
Map<String, Object> result = new HashMap<String, Object>();
result.put("MyClass", c1);
Yaml yaml = new Yaml();
String output = yaml.dump(result);
工作得很好。但是,现在如果在我的类中我有另一个值:
class MyClass{
String value;
Map<Integer, AnotherClass> MyList
public MyClass(){}
public void setValue(String value){
this.value = value;
}
Map<Integer, AnotherClass> CList = new HashMap<Integer, AnotherClass>();
public void setList(AnotherClass AL){
CList.put(1,AL);
this.MyList = CList;
}
public String getValue(){
return value;
}
}
现在我重复相同的操作,程序可以工作,但在序列化输出中,我看不到这个 HashMap。有什么问题,是否有一些不同的方法用于序列化 HashMap 类型对象?请建议....
when i serialize a class object such that:
class MyClass{
String value;
public MyClass(){}
public void setValue(String value){
this.value = value;
}
public String getValue(){
return value;
}
}
and serializing it like:
MyClass c1 = new MyClass();
c1.setValue("this is a value");
Map<String, Object> result = new HashMap<String, Object>();
result.put("MyClass", c1);
Yaml yaml = new Yaml();
String output = yaml.dump(result);
works just fine. However, now if in my class i have another value such that:
class MyClass{
String value;
Map<Integer, AnotherClass> MyList
public MyClass(){}
public void setValue(String value){
this.value = value;
}
Map<Integer, AnotherClass> CList = new HashMap<Integer, AnotherClass>();
public void setList(AnotherClass AL){
CList.put(1,AL);
this.MyList = CList;
}
public String getValue(){
return value;
}
}
and now i repeat the same, the program works but in serialized output, i do not see this HashMap. What is the problem, is there some different approach used to serialize HashMap type objects?? Please suggest....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
地图没有 getter,这就是它被忽略的原因。
There is no getter for the map, that is why it is ignored.