我的一个Java类(对象)里面有一个字段,以_开头,序列化之后,_没有了。例如: 我的字段是_id, 序列化之后变成了id。尽管有如下两个方案解决:
但是,我不清楚这么做的目的是什么? 相关代码如下:
zqdjd7g91#
6jygbczu2#
又做了一个测试:public class MainTest {public static void main(String args[]) {ProcEleInfo p = new ProcEleInfo();p.set_("f");p.set_id("fff");System.out.println(JSON.toJSONString(p));System.out.println(JSON.toJSONString(p, true));SerializeConfig config = new SerializeConfig(true);System.out.println(JSON.toJSONString(p, config));}}输出结果:/**{"":"f","cpuPercent":0.0,"cpuTotal":0,"diskIOBytesTotal":0,"id":"fff","mPercent":0.0,"memResident":0,"memSize":0}{"":"f","cpuPercent":0.0,"cpuTotal":0,"diskIOBytesTotal":0,"id":"fff","mPercent":0.0,"memResident":0,"memSize":0}{"_":"f","_id":"fff","cpuPercent":0.0,"cpuTotal":0,"diskIOBytesTotal":0,"mPercent":0.0,"memResident":0,"memSize":0}*/
所以,SerializeConfig里面的fieldBase字段是不是应该默认为true呢?
2条答案
按热度按时间zqdjd7g91#
6jygbczu2#
又做了一个测试:
public class MainTest {
public static void main(String args[]) {
ProcEleInfo p = new ProcEleInfo();
p.set_("f");
p.set_id("fff");
System.out.println(JSON.toJSONString(p));
System.out.println(JSON.toJSONString(p, true));
SerializeConfig config = new SerializeConfig(true);
System.out.println(JSON.toJSONString(p, config));
}
}
输出结果:
/**
{"":"f","cpuPercent":0.0,"cpuTotal":0,"diskIOBytesTotal":0,"id":"fff","mPercent":0.0,"memResident":0,"memSize":0}
{
"":"f",
"cpuPercent":0.0,
"cpuTotal":0,
"diskIOBytesTotal":0,
"id":"fff",
"mPercent":0.0,
"memResident":0,
"memSize":0
}
{"_":"f","_id":"fff","cpuPercent":0.0,"cpuTotal":0,"diskIOBytesTotal":0,"mPercent":0.0,"memResident":0,"memSize":0}
*/
所以,SerializeConfig里面的fieldBase字段是不是应该默认为true呢?