hoodwink.d enhanced
RSS
2.0
XHTML
1.0

RedHanded

YAML is JSON #

by why in inspect

So, let’s presume you don’t use YAML because of its use of whitespace. You don’t like indentation describing data structures. For whatever reason: taste, upbringing, paranoia. Whatever. And we’ll presume you punch the sky as well. You don’t like the ether to have connotations.

Well, you can use YAML without whitespace. Using inline collections. Since the YAML 1.0 working draft, you can even use YAML inlines exclusively.

It looks just like JSON. In fact, I just realized that all JSON is valid YAML. Try it for yourself. (However, YAML doesn’t allow /* .. */ comments, so it does require some preprocessing.)

 require 'yaml'
 YAML::load %{
 {"menu": {
  "id": "file",
  "value": "File:",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}]}}}
 }

So, ethics question. Can I start to advertise Syck as a JSON parser now? That was the easiest feature I’ve ever added.

Update: JSON’s discoverer has posted in the comments that Syck’s parser could be considered a JSON parser:

As long as you accept all correct JSON texts, you have a JSON parser. If you want it to also handle non-JSON texts, that’s your business.

Believe me, I’ll let everyone know when Syck can output JSON.

said on 07 Apr 2005 at 16:09

if and only if /**/ comments are supported ;)

said on 07 Apr 2005 at 16:12

You’d need to support C-style comments when in “JSON mode”—and more importantly not accept malformed JSON either.

Given both of those, please do.

said on 07 Apr 2005 at 16:19

If all JSON is YAML , ipso facto.

said on 07 Apr 2005 at 16:50

All JSON notation is valid YAML : Strings, numbers, objects, arrays, true, false, null.

Why should I need to support the comments? The JSON site claims, “This feature can also be found in Python.” But Python doesn’t support C-style comments.

I should also be allowed to accept malformed JSON . Look at this recommendation from json.org, concerning using JavaScript to load JSON :

To convert a JSON string into an object, use the eval() function. eval() invokes the JavaScript compiler. Since JSON is a proper subset of JavaScript, the compiler will correctly parse the string and produce an object structure.

eval(“window.newJSONObject = ” + aJSONString);

This means that the JavaScript parser is allowed to accept any valid JavaScript as valid JSON . So, really. Why would I be held to more stringent guidelines?

said on 07 Apr 2005 at 16:52

Oh. Wow.

Go to it man.

said on 07 Apr 2005 at 16:59

Why, you realize with all the hubbub about JavaScript in the Rails community right now, if Syck and your Ruby frontend start offering a JSON output and input mode, Rails and JavaScript could share data structres!

That’s just wow. The implications for that are staggering. Ruby functions could call js functions when the page is rendered, serializing the data into JSON , which JS can respond to by serializing back to JSON in cookies which is also YAML .

said on 07 Apr 2005 at 18:22

When things like this happen, that’s when we know what sorta stuff needs to be the real standard.

said on 07 Apr 2005 at 18:24

Thank you! I’m one of those sky punchers. It’s one of the main reasons I chose Ruby over Python way-back-when.

said on 07 Apr 2005 at 19:03

I’m a sky puncher as well. I just can’t get past white-space having meaning.

Even with the inline collections I still don’t buy into YAML . Take a look at that last line in your example:


{"value": "Close", "onclick": "CloseDoc()"}]}}}

Exactly how many “}” and “]” characters is that and what order do they go in? I sure hope everybody has a decent text editor that matches opening symbols. Also hope your data structures don’t span more than one page. Occasionally I run into someone elses code that used deeply nested block of code that looks like:


if( foo > bar )
{
  while (i < 10)
  {
  if( cat > dog )
     {
       until( z < q )
       {
         for( n=0; n<5; n++ )
         {
            if( cow > grape )
            {
              while( z < q )
              {
                ...do something...
              }
              ...bunch of code...
            }
            ...more code...
          }
          ...several pages of code...
        }
        ...you think I'm joking...
      }
      ...I've really seen stuff...
    }
    ....about this bad...
  }
  ....seriously!.....
}

When I end up maintaining code that looks like this I almost always have to rewrite the logic to be reasonable otherwise I spend forever debugging stupid missing or extra “}” characters.

If I don’t want that in my code why would I want it in my data?

I’m not a XML fanatic but at least in xml the closing tags have obviously matching opening tags and ensuring you don’t have two few or two many closing tags is also obvious.

Just my 2 cents.

said on 07 Apr 2005 at 19:15

Eric, you cannot seriously be suggesting that problems like the above are unheard of in XML . You’ve obviously never written a JSP if you are. :-)

said on 07 Apr 2005 at 22:37

Oh, I haven’t tried loading XML into YAML !! Be right back!! (Fingers XROSSD !!)

said on 08 Apr 2005 at 01:59

I used JSON with Rails before Ajax came to it, and everything was good and easy. It was OrbJson implementation by James Britt. I’m even using it in one project, cause it’s better for it’s purposes than Ajax. I’ll be happy if it will find it’s way to Rails, but ….

said on 08 Apr 2005 at 11:06

Real men use s-expressions! :-)

said on 08 Apr 2005 at 15:40

[ot] Believe it or not, the Evolution email client includes an s-expression interpreter to handle queries (see evolution-data-server-1.0.2/libedataserver/e-sexp.c).

So you can give it a query like (contains ‘full_name’ ‘Guidry’) and it’ll give you back Ron Guidry, if he happens to be in your address book.

said on 08 Apr 2005 at 22:27

Eric, it’s funny you say you don’t like relevant whitespace, and then goes on to say how matching closing braces suck. You know, with meaningful indentation you don’t have to close anything.

In fact regarding xml/xhtml I even wrote my own yaml-ish withspace-sentitive no-closing-things notation language (which itself is not too different from pxsl, just simpler)

And then again, back in rails 0.6 or so it took me little time to add to_js methods to all base classes and be done with json. I don’t get what the fuzz is all about. (Except that maybe I could just have used yaml to generate json?)

(I also found a js yaml lib out there, but don’t know how good it is)

said on 09 Apr 2005 at 18:19

While I don’t like the “macho” attitude implied in Chris2’s statement (I’m certainly not a “real man”, nor do I think s-expressions is difficult or challenging at all – it’s way easier than XML or even JSON ), I’ve also been using s-expressions for a while and I’m not very fond of YAML at all. It uses a crazy mesh of { and [ characters instead of just parentheses, and (while I don’t have a knee-jerk reaction against whitespace sensitivity) I’ve been saved by the non-whitespace-sensitivity several times in my recent project since the lines have been edited sloppily (by me – yeah, I’m such a slob).

For me, the idea “easier for the lexer/reader/parser to parse = easier for the brain to parse” holds true.

Also, with s-expressions I can use the same notation for code and data, as long as I’m programming in a s-exp language (which is my preference) – JSON also has this property, if you’re programming in javascript.

YAML is reinventing what scheme/lisp programmers have had for years and years. While it’s a good thing that more people are discovering this, I think it’s a shame why you aren’t really improving on the ol’ s-expressions, just making a (unnecessarily) complex version that’s harder to generate and harder to parse.

(My current project is (currently) over six hundred lines of hand-written s-expressions, and that’s just the textual data. Not counting code (which is less – 328 lines of scheme, to generate (currently) over six thousand lines of XML from those six hundred lines of s-expressions.))

Same with Ruby. I don’t see what makes it so “easy” and Scheme so “difficult”. Macros can be tricky, but Ruby’s MOP is not kindergarten stuff either. (The Ruby interpreter is a remarkable piece of software, however, as is much software from the Ruby community.)

said on 11 Apr 2005 at 11:28

While I don’t like the “macho” attitude implied in Chris2’s statement

He was joking (lightly). There’s a meme in America from a book called “Real Men Don’t Eat Quiche” written (I think) in the 1970’s.

said on 11 Apr 2005 at 12:04

Did anyone notice that Sunnan’s post is mostly in parens??

said on 12 Apr 2005 at 06:21

So how do you tell YAML.dump() to output structures in this form? If I want to give people structured data to edit, I must create it in the first place, ideally automatically. Could you add some more recipies to the YAML cookbook for this flavour of data, please? Thank you.

said on 13 Apr 2005 at 16:40

So in response to the original question, Austin had the correct answer. As long as you accept all correct JSON texts, you have a JSON parser. If you want it to also handle non-JSON texts, that’s your business.

said on 14 Apr 2005 at 05:04

UnAmerican: I’ve seen the book; but the meme is so entrenched these days that I don’t always think of it as a joke.

Why: Thanks for noticing. (I like subtle little jokes like that. (I’ve been known to hand in anagram-filled essays that’s also one long acrostic. (The teacher didn’t notice anything.)))

I might’ve developed parenthesis synaesthesia.

said on 14 Apr 2005 at 05:22

For the curious, here’s one possible s-expression version of the JSON example (assuming we’re to use strings rather than a mix of symbols and strings). I can see some pros and cons here; I guess tastes can vary.


("glossary" 
 (("title" "example glossary")
  ("GlossDiv" 
   (("title" "S")
    ("GlossList" 
     (("ID" "SGML")
      ("SortAs" "SGML")
      ("GlossTerm" 
       "Standard Generalized Markup Language")
      ("Acronym" "SGML")
      ("Abbrev" "ISO 88791986")
      ("GlossDef" 
       "A meta-markup language used to create markup languages such as DocBook.")
      ("GlossSeeAlso" ("GML" "XML" "markup"))))))))

said on 25 Sep 2005 at 00:28

Seems the s-expression contains less information. In YAML /JSON, I’m told whether a list is an integer-indexed array or part of a hash; and I don’t see that info anywhere in the s-expressions.

said on 10 Oct 2005 at 22:25

I’ve been known to hand in anagram-filled essays that’s also one long acrostic.

Good to know I’m not the only one.

My favorite was one that spelled out “Don’t smoke pot”, complete with the apostrophe. It was a work of singular genius.

said on 03 Nov 2005 at 09:21

Hmm. So JSON = YAML + Comments? All I want is to be able to talk to more langauges. People keep waving JSON parsers in my face.

Comments are closed for this entry.