Hacker News new | ask | show | jobs
by rvkennedy 4488 days ago
HTML would be much easier to write if it were based on JSON. Less bandwidth too. Has this been attempted?
3 comments

Are you kidding me?

Currently:

  <ul>
    <li>Hello!</li>
  </ul>
By your suggestion:

  {
    "tagName": "ul",
    "children": [
      {
        "tagName": "li",
        "children": [
          "Hello!"
        ]
      }
    ]
  }
(and, yes, it has been attempted... JSON.stringify(document.body))
> JSON.stringify(document.body)

One of two things will happen, depending on your browser.

If your browser is following the WebIDL spec, so all the accessors are on the prototype, this will produce "{}".

If your browser is WebKit-based, this will throw an exception, because body.firstChild.parentNode == body and JSON.stringify throws on object graphs with loops.

Aw, damn. I didn't realise it was all fake properties, I thought that would produce something.
Nothing fake about accessor properties. Means you can lazily generate the string for .innerHTML and such, though!
I don't think your example needs to be quite so verbose.

  {
    'ul': {
      'li': 'hello!'
    }
  }
I would think it would depend upon the parser.

Regardless, I'd still rather write out HTML instead of JSON for markup.

That breaks down as soon as you have something with attributes, and it breaks down even further once you have something like:

  <p>Example <b>text</b></p>
You see, this is why I don't create standards.

I'm going to go with that JSON is the failure point with my grand vision.

Not sure your example would work in a real world situation. The UL would probably have an array attached as it can have multiple children. Then we get into the fact that all tags can have attributes, not just a text node.
You're right, I didn't consider attributes. In my simplified way the parser would need to know which keywords were attributes based on parent element versus keywords that are just new children elements. Which would defeat the purpose.

I guess I'm not changing the world today.

There's always tomorrow!
Nah, I need time to recover from this dismal failure and to reflect. Maybe next Tuesday.
LISP:

  (:ul (:li Hello))
Nah.

It would be much easier as a Lisp S-EXP.

And yes, it has been attempted.