Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-24-2013, 01:58 AM   PM User | #1
Hulkiedo
New to the CF scene

 
Join Date: Jan 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Hulkiedo is an unknown quantity at this point
Simplify JSON

JSON likes to call itself "The Fat-Free Alternative to XML" but is it really as healthy as it proclaims to be?

I think there are ways to make it smaller and more human-readable while preserving the excellent machine-readability

A few things I thought of while parsing JSON:
  • Why put quotes around the key? Have you ever seen a key with whitespace or curly braces? (even if you did you can escape them) ==> Let's remove the quotes
  • An id-value pair is separated from others by a comma. In a pair it is always obvious (because of formatting) what is the key and what is the value (unless one of either 2 is missing). So let's get rid of the colon.
  • Everything in JSON is a string. Let's not pretend otherwise. ==> Enclose all values in double quotes, or make quotes optional entirely.
  • For a better type system we could think of optional prefixes to let the reader know what data type it should be converted to, stuff like:
    • {mydata x"1F A9 0C"} for hex
    • {mynumber d"824"} for a decimal integer
    • {mynumber ld"46,319,878,234"} for a long 64-bit decimal int.
    • {mynumber f"100.29" } for a float
    If the parser or target language can't process the type it stays a string. This sounds a lot more flexible and extensible than the current type system.

Lets see an example from JSON.org. Before:
Code:
{"menu": {
   "id": "file",
   "value": "File",
   "popup": {
      "menuitem": [
         {"value": "New", "onclick": "CreateNewDoc()"},
         {"value": "Open", "onclick": "OpenDoc()"},
         {"value": "Close", "onclick": "CloseDoc()"}
      ]
   }
}}
After:
Code:
{menu {
   id "file",
   value "File",
   popup {
      menuitem [
         {value "New", onclick "CreateNewDoc()"},
         {value "Open", onclick "OpenDoc()"},
         {value "Close", onclick "CloseDoc()"}
      ]
   }
}}
Without quotes:
Code:
{menu {
   id file,
   value File,
   popup {
      menuitem [
         {value New, onclick CreateNewDoc() },
         {value Open, onclick OpenDoc() },
         {value Close, onclick CloseDoc() }
      ]
   }
}}
Obviously there are a few edge cases I can think of which need to have their behavior defined. But real-world examples of those are scarce.

I've looked into an alternative like YAML, but I dont want to leave delimiters entirely (just the superfluous ones). Relying on indentation brings up new issues and limitations, and it makes nested data bigger. It also is more difficult to parse.

Thoughts?

Last edited by Hulkiedo; 01-24-2013 at 03:38 AM..
Hulkiedo is offline   Reply With Quote
Old 01-24-2013, 02:41 AM   PM User | #2
Hulkiedo
New to the CF scene

 
Join Date: Jan 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Hulkiedo is an unknown quantity at this point
I've changed my mind on adding newline as a delimiter (with the same functionality as comma), because it makes parsing harder. For the rest I stand by my points.

Last edited by Hulkiedo; 01-24-2013 at 03:01 AM..
Hulkiedo is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:35 AM.


Advertisement
Log in to turn off these ads.