JSOMON

JavaScript Object + Methods Over Notation

JSON Evolution

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write.

In this way starts official JSON Introduction and this is exactly what JSON is, the simplest way to send or recieve data using different program languages.

Since Ajax and Web 2.0 era began, JSON has been used to transport big amount of informations between client and server and the best JSON feature is that it's naturally compatible with JavaScript, the most used scripting language You can find surfing the net.

One obvious JSON limit is that not every program language can send or recieve easily every kind of information, but JavaScript, a runtime interpreted scripting language, should do that!

JSON protocol transports correctly and quite perfectly nested informations, complete lists and / or objects, but there's one thing JSON doesn't transport: methods.

JSOMON implements object methods using a portable, readable and simple SOH notation, allowing developers to copy and past JSOMON strings and server languages to parse before regular JSON evaluation too.

JSOMON breaks original JSON limitations expecially with, and for, JavaScript scripting language.

How does JSOMON work

This is JSOMON structure:

[s] = (\n|\r|\r\n) [JSON] = JSON compatible string [JSOMON] = [function1(){},...,functionN(){}] [text] = every kind of extra information /*[s]{0,1}[JSON]([s][JSOMON]){0,1}*/{[s][text]){0,1}

As You can see, JSOMON writes itself inside a comment, a quite better way to send or recieve JSON strings. In this way data is a bit securer but please read more about JSON and Ajax security issues.

At the same time, JSOMON is totally compatible with JSON so You don't need to send one or more functions inside JSOMON list. This means that a lot of application should be compatible with JSOMON without big changes.

Last optional information should be useful to add copyrights, disclaimers or every kind of extra info and why not, JavaScript too.

SOH based JSOMON convertion

JSON convertion transforms different characters in a JavaScript compatible unicode notation. For example SOH char will be transformed into \u0001. Every time JSOMON encoder parses a function, actually discarded in every JSON parser I know, it checks if that function is present inside a temporary functions list then add them if it's not present or simply add SOHN information as object key value.

function getObject(name){ return { name:name, show:function(){ alert(this.name); } }; }; var sendMe = [ getObject("JSON"), getObject("JSOMON"), getObject("Duffy") ]; document.write(JSOMON.encode(sendMe));

Above example will show this string:

/*[{"name":"JSON","show":0},{"name":"JSOMON","show":0},{"name":"Duffy","show":0}] [function () {\n alert(this.name);\n}]*/

As You can see, JSOMON optimizes methods informations reducing total result string size. If You need to use global functions or You don't want to send directly function string rappresentation, You can simply use toString method to optimize final JSOMON size.

function show(){ alert(this.name); }; show.toString = function(){ return "show"; }; function getObject(name){ return { name:name, show:show }; }; document.write(JSOMON.encode(getObject("JSOMON"))); // will produce: /*{"name":"JSOMON","show":0} [show]*/

You can even use SOH char inside your functions, objects or lists data, JSOMON will parse everything correctly.

When should I use JSOMON ?

JSOMON tips and tricks

With a new interaction model, JSOMON allows server-side developers to create different objects with different methods. You can add server-side JavaScript generated or stored functions and You can even manage client side using a little JSOMON trick. For example, a user requires one or more JSON data from server, with or without methods, but server-side application should generate a custom function that will be executed on client side when JSOMON.decode has been called. Please look at this simple php example file:

<?php /* server side application example: testme.php */ header('Content-Type: text/javascript'); $output = '/*{"someKey":"someValue"}'; if(!isset($_GET['info'])) $output .= "\n[(function(){alert('please send me required info!')})()]"; $output .= "*/\nŠ 2007 - MyCompany"; exit($output); ?>

and now please look at this client example code:

var xhr = new XMLHttpRequest(), response; xhr.open("get", "testme.php", false); xhr.onreadystatechange = function(){ if(xhr.readyState === 4) response = JSOMON.decode(xhr.responseText); }; xhr.send(null);

As You can see JSOMON has more features than just methods over notation and this is only a simple trick example but I'm sure, You'll find a lot for your custom applications by yourself!

What is next JSOMON goal ?

The best JSOMON implementation will probably be JSOMON-ML (JSOMON Markup Language). Since JSONML is not so lightweight when server need to send inline JavaScript methods too (i.e. onclick, onsomeevent) and it cannot be really compared with XML, JSOMON-ML should be a size killer to send one or more common event handlers inside JSONML layouts.

What about JSOMON compatibility ?

My JSOMON object has been successfully tested with these browsers:

JSOMON is probably compatible with more browsers too and should be even compatible with IE 5.0 if You use a JavaScript normalizer library.

Download JSOMON

Please contact me if You want to add your specific JSOMON implementation, thank You.