gpt4 book ai didi

java - JYaml:转储对象而不包含类名

转载 作者:行者123 更新时间:2023-12-01 06:25:38 25 4
gpt4 key购买 nike

我有一个被转储到 YAML 字符串的对象的 ArrayList,并且一直在比较 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 为我提供无类名的输出,但我对如何实现这一点感到非常困惑。

最佳答案

如何测量速度? “数据量”是什么意思?是 YAML 文档的大小还是文档的数量?

JYaml 输出不正确。根据规范,数字中的下划线将被忽略,并且 1_1 = 11(至少对于 YAML 1.1)。因为它实际上是一个字符串而不是一个整数,所以表示应为:

  • “1_1”:一

或规范

  • !!str "1_1": !!str "一"

否则,当解析文档时,它将创建 Map<Integer, String> 而不是 Map<String, String>

JYaml 有许多 Unresolved 问题,并且没有实现完整的 YAML 1.1

JYaml 确实可能更快,但这是由于简化了解析和发出。

关于java - JYaml:转储对象而不包含类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/531920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com