The use case is long term serialization of complex object graphs in a textual format.
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.
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.
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.
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).
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!)
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.
Many major programming languages support both YAML and XML.
You do not need to explain to a developer (even junior) what is XML. YAML is not that widely used yet.
With XML both producer and consumer can agree on a Schema to establish a reliable data exchange format.
XML is very rich: namespaces, entities, attributes.
Java and Python have XML support in the standard libraries. YAML requires an external dependency for these languages.
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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With