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

发布于 2024-07-13 14:27:34 字数 1234 浏览 5 评论 0原文

我有一个 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 给我无类名的输出,但我很不知道如何实现这一点。

I have an ArrayList of objects being dumped to a YAML string and have been comparing the performance of JYaml and SnakeYaml in handling this.

    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));

The output from JYaml includes the serialised object's class name whereas the output from SnakeYaml does not:

JYaml output:

- !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 output:

- {'1_1': One, '1_3': Three, '1_2': Two}
- {'2_1': One, '2_2': Two, '2_3': Three}

I prefer the more 'clean' class name-less output of SnakeYaml as this would be more suitable for a language-neutral environment.

I prefer the speed of JYaml. Serialisation/deserialisation times increase linearly with the amount of data being processed, as opposed to exponentially with SnakeYaml.

I'd like to coerce JYaml into giving me class name-less output but am quite lost as to how this can be achieved.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无边思念无边月 2024-07-20 14:27:36

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

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

  • '1_1': One

or canonically

  • !!str "1_1": !!str "One"

否则,当解析文档时,它将创建 Map<整数、字符串> 而不是 Map<String, String>

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

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

How do you measure the speed ? What do you mean 'amount of data' ? Is it a size of a YAML document or an amount of documents ?

JYaml output is incorrect. According to the specification underscores in numbers are ignored and 1_1 = 11 (at least for YAML 1.1). Because it is in fact a String and not an Integer the representation shall be:

  • '1_1': One

or canonically

  • !!str "1_1": !!str "One"

Otherwise when the document is parsed it will create Map<Integer, String> instead of Map<String, String>

JYaml has many open issues and does not implement complete YAML 1.1

JYaml may indeed be faster but it is due to the simplified parsing and emitting.

把梦留给海 2024-07-20 14:27:36

检查 SnakeYAML 最新源。 现在可以(与 JYaml 中相同)忽略隐式类型并始终将标量解析为字符串。 这要快几倍。
请查看此处此处了解如何使用新功能。

(当正则表达式关闭时,序列化/反序列化时间随着处理的数据量线性增加。)

Check the SnakeYAML latest source. It is now possible (same as in JYaml) to ignore implicit typing and always parse scalars as Strings. This is a few times faster.
Look here and here to see how to use the new feature.

(With the RegularExpressions off serialisation/deserialisation times increase linearly with the amount of data being processed.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文