JSON vs. YAML – try it yourself
json2yaml.com
json2yaml.com
It just seems like YAML is making things more implicit. I like explicitness. Not against YAML, but it's no holy grail in my mind.
I'm more experienced with JSON than YAML and could be swayed on this, but so far I'm not. They seem like two roughly equal ways to do the same thing.
This seems oddly similar to the nit-picky problems people have when learning Python from a C-like background and being annoyed that semicolons and flexible indentation aren't the norm. When I spend months working on JS code, looking at Python slightly annoys me, and vice-versa, so I think I get it. I just don't think it's an important, game-changing thing.
I'll be learning about Ansible soon, so I'll have to also learn YAML syntax. I guess I'll have a more educated view once I've gotten deep into that.
I'm no YAML zealot though, I do use JSON most of the time, but in the past couple years I've started writing most of my config files in YAML where I have the opportunity.
e.g.
name: user
plural: users
label: User
...
primary_key: uid
display_key: full_name
unique_keys:
- username
- uid
client:
storage:
type: IndexedDB
...
actions:
create:
label: Save
permission: create_user
...
server:
storage:
type: mysql
...
api:
create_user:
url: /users
method: post
fields:
username:
name: username
label: Username
type: string
length: 20
required: true
editable: falseSo is this the equivalent of multiple JSON documents? I'm looking at:
http://yaml.org/spec/1.1/#id857577 and http://yaml.org/spec/1.1/#stream/information%20model
I'm actually having a hard time, even with some google-fu, finding information about how this might be used. YAML->json parsers have failed to handle docs with a '...' separating parts of a document so far. Would this triple-dot structure be used to create multiple JS objects? How would I find more information about how this might be used in practice?
I have a feeling this would be easier to figure out if I just went and played around with it in practice, but it also seems like something that could be documented in more beginner-friendly terms.
I'll probably be kicking myself once I figure it out, but I also only have a cursory understanding of how streams work. Maybe this is an opportunity to fix that. Once I figure it out, I'll respond to this if nobody else has.
However, since you asked:
In a YAML document, there is an implicit top-level object, either a hash/dict/associative array or a list/array depending on your formatting.
The '...' separates streams, or top-level objects, within a single file or IO stream.
YAML parsers generally stop when they hit the separator, even in languages that can do multiple assignment. If your IO stream behaves like a file handle, you can read it repeatedly into different variables until EOF.
Multiple streams in a YAML document are fairly uncommon. I think most people don't know they exist, but I appreciate the flexibility and use it whenever it makes sense.
Json intentionally does not support comments. Think about that for a bit and you'll realise what Json is for and what it isn't for.
As SeoxyS said: use yaml for config files, Json for APIs.
[#] Shortcuts like not quoting keys and omitting braces and commas in hash definition.
Write config files in YAML. Write APIs in JSON.
For data interchange, I don't see a lot of advantages to yaml though.
http://www.yaml.org/spec/1.2/spec.html#id2805712
additionally, it allows multiple top level objects in one file (which JSON can't do (not counting 3rd party modifications))
other than that, not much other use
That's just different syntax for a top-level list of objects.
Your parser might not support instantiating arbitrary objects, but those your programs interact with might... :(
Anyone else recall the problems with YAML vulnerabilities? An old blog post by me: http://williamedwardscoder.tumblr.com/post/43394068341/rubys...
So never ever use YAML for tainted input.
A different approach to using JSON for configuration files. It sits between JSON and YAML.
Have you ever been bitten by the dwimmy typing of numeric and true/false/null data?
No problems with dwimmy typing ;) - not saying that it couldn't happen but I think that would be the exception. Not having to use quotes/escape characters helps a lot more.
Man, It sure is condensed, but because of that, not very readable to me. If this is the standard for YAML, it's definitely interesting, but not my cup of tea. It's sometimes hard to parse where a list ends or an object begins. If you want to argue that it's more space efficient, I'd say, just use gzipped JSON. If you want to say you're using it for the spacing and/or line breaks, I'd just say find a viewer that prettifies your JSON well.
Having a delimiter, at least when you're representing chunks of data, is really useful, easier to code for, and easier to read than tabs or other systems. This, not so much. It's kind of a hot mess. Maybe that thinking works well in python, but I'm not so sure the principle translates away from code. If I have a list of objects, I really need to know where one thing ends and the next begins without having to keep track of more than one thing at a time.
JSON is free to do whatever. You could have everything in one line if you wanted. You could use indentations of 8 spaces or 21 tabs. White space does not matter! Point is, it is not hard to figure out a sensible indentation and line breaking scheme if you need it. This is the beauty of braces when dealing with data.
However I have also learned the following:
- in python, parsing yaml file is hundred times slower than json (in fact, my benchmark was showing around 400x slowdown). Therefore you can't really use it in cases where performance matters at least a little bit. A yaml 1k line yaml can load more than half a second (yrmv)
- if your yaml doc is longer than two screens, it loses its readability benefits.
Therefore it is best to use a whole directory of yaml files, each describing a specific feature. E.g. Ansible is a good example of how to use yaml files.
I am searching for an replacement of / alternative to Ansible written in Go or Rust that uses JSON or INI as config format.
(Ansible is written in Python. Both Python and YAML rely on outline indentation which causes many headaches)
This is annoying as not everybody using ISO 8601 strings as dates.
What annoys me is that JSON lacks a binary data type. The best you can do is base64, which really sucks if 99% of your binary data falls into the ascii range, but you have the occasional high bit character and you explicitly aren't trying to treat it as unicode.
JSON hasn't been designed for binary data, so it's not surprising that it lacks a binary data type. There are several options besides base64, you could e.g. use yEnc or BSON.
But unless you have really large binary data (in that case I would instead of embed it in JSON, only embed an URL in JSON and let the client download the data separately), I wouldn't bother with another encoding than base64. It is easily compressable and this is handled transparently, so you reach such a low overhead that it is hard to justify using a non-standard option like yEnc.
JSON is beloved because it's easy and simply. XML was originally simple too with just DTD as Schema. Then they come up with XML-RPC, XMLSchema, XSLT, SOAP and many other complex concepts that in the end more or less failed.
The complexity of XML has not changed since its inception.
> Then they come up with XML-RPC, XMLSchema, XSLT, SOAP and many other complex concepts that in the end more or less failed.
This doesn't really has any bearing on the specification of XML itself. XML is simple (with some caveats like entity expansion), and extremely flexible. The problem is that it is designed to be read and written by machines, while still being debugable by people. It is extremely verbose. It shines in some use cases, eg for documents with a complex structure and a lot of semantic metadata. But it is even less suitable than JSON for configuration files, for instance.
* it's a little too verbose (<xml></xml>.. while even S-expressions use just ')' as terminator)
* there is no obvious way to transform any xml to an object (pojo is the best aproxymation, but how do you differentiate between a sub-tag, a text node, and an attribute?)
* it's essentially typeless (how do you serialize a number? how to differentiate it from a string? from a boolean?)
> there is no obvious way to transform any xml to an object (pojo is the best aproxymation, but how do you differentiate between a sub-tag, a text node, and an attribute?)
That's OK. Just don't use XML to serialize data structures. IMHO, the use cases at which it is good are a lot closer to use cases for which HTML works than when you hesitate between XML and JSON.
> it's essentially typeless (how do you serialize a number? how to differentiate it from a string? from a boolean?)
That's not entirely wrong, but you can enforce a lot of things via XML schemas (eg, something like XSD or RelaxNG).
XML is structured text that can be validated. XML's biggest selling point!
http://www-01.ibm.com/support/knowledgecenter/SS9H2Y_7.1.0/c...
;-)
I've encountered many poorly written services where I would have been happy if there were a schema to be at least sure what kind of messages are exchanged.
I thought, honestly, that html5 api was able to provide the same capability as flash, as far as file/clipboard was concerned.