对于对象图反/序列化,yaml 与 xml 有何优缺点?

发布于 2024-07-13 18:41:26 字数 29 浏览 11 评论 0原文

用例是以文本格式对复杂对象图进行长期序列化。

The use case is long term serialization of complex object graphs in a textual format.

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

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

发布评论

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

评论(4

孤星 2024-07-20 18:41:26

简而言之,

如果您希望人类创建/读取文档(配置文件、报告等),那么您可以考虑 YAML,否则选择 XML(用于机器对机器通信)。

长答案

长度

XML 和 YAML 大致相同。 好的 XML 库可以跳过所有空格,而 YAML 则需要这样做。 复杂的 YAML 包含大量缩进空间(不要使用制表符!)

网络故障

YAML 文档的一部分通常是有效文档,因此如果 YAML 文档不完整,则无法自动检测它。 XML 解析器将始终检查文档是否至少格式良好,并且可以自动检查模式的有效性。

语言支持

许多主要编程语言都支持 YAML 和 XML。

常识

您无需向开发人员(甚至初级开发人员)解释什么是 XML。 YAML 尚未广泛使用。

模式

通过 XML,生产者和消费者可以就模式达成一致,以建立可靠的数据交换格式。

XML语法

非常丰富:名称空间、实体、属性。

外部依赖

Java 和 Python 在标准库中提供 XML 支持。 YAML 需要这些语言的外部依赖项。

成熟度

XML 规范较旧,但坚如磐石; 而 YAML 仍在构建中。 YAML 1.1 包含不一致的地方(甚至有一个 wiki 来维护错误列表)。

XSLT

如果您需要将 XML 文档转换为另一种格式(XML、HTML、YAML、PDF),您可以使用 XSLT,而对于 YAML,您必须编写一个程序。

Short answer

if you expect humans to create/read the document (configuration files, reports, etc) then you may consider YAML, otherwise choose XML (for machine-to-machine communication).

Long answer

Length

Both XML and YAML are approximately the same. Good XML libraries can skip all whitespaces while for YAML it is required. A complex YAML contains a lot of indentation spaces (do not use tabs!)

Network failure

Part of a YAML document is often a valid document, so if a YAML document is incomplete there is no automatic way to detect it. An XML parser will always check whether a document is at least well-formed, and can check validity against a schema automatically.

Language support

Many major programming languages support both YAML and XML.

General knowledge

You do not need to explain to a developer (even junior) what is XML. YAML is not that widely used yet.

Schema

With XML both producer and consumer can agree on a Schema to establish a reliable data exchange format.

Syntax

XML is very rich: namespaces, entities, attributes.

External dependencies

Java and Python have XML support in the standard libraries. YAML requires an external dependency for these languages.

Maturity

XML specification is older and it is rock solid; whereas, YAML is still under construction. YAML 1.1 contains inconsistencies (there is even a wiki to maintain the list of mistakes).

XSLT

If you need to transform an XML document to another format (XML, HTML, YAML, PDF) you can use XSLT while for YAML you have to write a program.

厌味 2024-07-20 18:41:26

XML有xpath和xquery。

但 YAML 具有复杂的键,允许您使用别名(& * 语法)将一个值复制到多个位置。

我认为 YAML 的优势并不在于人类可读性,就像他们所说的那样……我可以很好地读取格式正确的 XML。

实际上,YAML 的优势在于人类的可写性。 如果您在构建数据模型等时在 iPhone 上快速输入一些数据,那么 YAML 会更容易输入。别名对此非常有用。

然后你可以将它转换成任何东西。

YAML 的更高级功能(例如严格类型和标签)我从不使用。 YAML 人员目前正在讨论发布一个“微型 YAML”来与 JSON 竞争,对于我们这些使用它的人来说,它基本上就像带有别名的更干净的 JSON 一样。

但正如其他人指出的那样,XML 更适合作为一种标记语言,您可以在其中直观地区分数据的哪些部分对最终用户可见。

XML has xpath and xquery.

But YAML has complex keys and lets you copy one value into many places with aliases (& * syntax).

I think YAML's strength is not human readability, like they say... I can read properly formatted XML just fine.

Actually YAML's strength is human writability. If you are on an iPhone typing up a quick bit of data as you architect a data model or such, YAML is so much easier to type in. The aliases are awesome for that.

Then you can convert it to whatever.

The more advanced features of YAML like strict typing and tags, I never use that. The YAML guys are currently discussing issuing a "micro YAML" to compete with JSON, for those of us who use it basically just as cleaner JSON with aliases.

But XML is better as a markup language where you want to visually distinguish what parts of the data are visible to the end user, as someone else pointed out.

獨角戲 2024-07-20 18:41:26

我同意:YAML 更具可读性,并且似乎很适合开发读/写配置文件。 但对于机器对机器的通信来说几乎没有什么好处。 另外,对于文本标记(xml 的传统强项,如 xhtml、docbook),xml 更好。

特别是对于对象序列化,我想不出使用 YAML 的充分理由。

事实上,我建议考虑使用 JSON:它基于对象(或结构,因为没有行为)模型,而不是分层 (xml) 或关系 (SQL) 模型。 因此,它更适合对象数据。 但 XML 也可以很好地工作,并且有许多好的工具。

最后一件事:术语“长期”和“对象序列化”不能混合。 后者意味着紧密耦合:您的对象发生变化,序列化也会发生变化。 实际的对象序列化不应该用于存储数据,数据绑定/映射更合适。 但您可能正在使用“使用方便的对象包装器存储/恢复数据”意义上的序列化; 如果是这样,那很好。

I agree: YAML is more readable, and seems like a good fit for, say, dev-read/writable configuration files. But there is little benefit for machine-to-machine communication. Also, for textual markup (xml's traditional forte, like xhtml, docbook), xml is better.

Specifically for object serialization I can't think of good reason to use YAML.

In fact, I would suggest considering JSON instead: it is based on object (or, struct, since there's no behavior) model, rather than hierarchic (xml) or relational (SQL) models. Because of this, it's bit more natural fit for object data. But XML works just fine as well, and there are many good tools.

Last thing: terms "long-term" and "object serialization" do not mix. Latter implies close coupling: your objects change, so does serialization. Actual object serialization should not be used for storing data, data binding/mapping is more appropriate. But it may be that you are using serialization in sense of "storing/restoring data using convenient object wrappers"; if so, that's fine.

人生百味 2024-07-20 18:41:26

如果您使用 YAML,则传输的字节数可能会稍微少一些。 我认为还不够重要,但在某些情况下它可能是相关的。 我还认为 YAML 在文本编辑器中更容易阅读和使用,但这确实有点主观,如果这不是正常的使用场景,那也没关系。

如果带宽不是一个因素,并且您不会经常在文本编辑器中使用 YAML/XML,那么我认为您使用哪一个并不重要。

If you use YAML, you might have a slightly fewer bytes going across the wire. Not enough to be significant I think, but in some situations it could be relevant. I also think YAML is easier to read and work with in a text editor, but that is really kind of subjective, and it wouldn't really matter if this is not a normal usage scenario.

If bandwidth is not a factor and you wont be working with the YAML/XML in a text editor often, I think it doesn't really matter which you use.

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