我有一个ArrayList
对象被转储到 YAML 字符串中,并且一直在比较 JYaml 和 SnakeYaml 在处理此问题时的性能。
ArrayList<HashMap> testList = new ArrayList<HashMap>();
HashMap<String, String> testMap1 = new HashMap<String, String>();
HashMap<String, String> testMap2 = new HashMap<String, String>();
testMap1.put("1_1", "One");
testMap1.put("1_2", "Two");
testMap1.put("1_3", "Three");
testMap2.put("2_1", "One");
testMap2.put("2_2", "Two");
testMap2.put("2_3", "Three");
testList.add(testMap1);
testList.add(testMap2);
System.out.println(jYaml.dump(testList));
System.out.println(snakeYaml.dump(testList));
JYaml 的输出包括序列化对象的类名,而 SnakeYaml 的输出不包括:
JYaml 输出:
- !java.util.HashMap
1_1: One
1_3: Three
1_2: Two
- !java.util.HashMap
2_1: One
2_2: Two
2_3: Three
SnakeYaml 输出:
- {'1_1': One, '1_3': Three, '1_2': Two}
- {'2_1': One, '2_2': Two, '2_3': Three}
我更喜欢 SnakeYaml 更“干净”的无类名输出,因为这更适合语言中立的环境。
我更喜欢 JYaml 的速度。序列化/反序列化时间随着正在处理的数据量 线性增加,而SnakeYaml 则呈指数增长。
我想强迫 JYaml 给我无类名的输出,但是对于如何实现这一点我很迷茫。