Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the advantages and disadvantes of yaml vs xml for Object graph de/serialization?

Tags:

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

like image 258
flybywire Avatar asked Feb 18 '09 14:02

flybywire


People also ask

Is YAML better than XML?

The biggest difference, though, is that XML is meant to be a markup language and YAML is really more of a data format. Representing simple, hierarchical data tends to be more gracefully done in YAML, but actual marked-up text is awkward to represent.

What is the difference between XML and JSON and YAML?

YAML stands for “YAML Aint Markup Language“. JSON stands for “JavaScript Object Notation“. XML is “eXtensible Markup Language” whereas YML is not a markup language. XML uses a tag to define the structure just like HTML.

Is YAML more readable than JSON and XML?

YAML, depending on how you use it, can be more readable than JSON. JSON is often faster and is probably still interoperable with more systems. It's possible to write a "good enough" JSON parser very quickly.


2 Answers

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.

like image 143
Andrey Avatar answered Oct 24 '22 15:10

Andrey


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.

like image 27
StaxMan Avatar answered Oct 24 '22 15:10

StaxMan