Memoirs of a Programmer


uJSON - Micro JSON

Just a little shout out to the programming world...

If you would like to include JSON requests inside of a URL, you'll notice a couple hindering points, including the fact that IE and Firefox (or any browser for that matter) dislikes anything after the ? in the URL to contain quotes, or else it converts all of that using URL encoding, which just makes the url longer and less appealing. So I went along and made a little extension of the well known Services_JSON package which basically converts the JSON string, removing the quotes. The extension has both an encoder and a decoder. Any comments would be great.



<?
/*
This converts a slimmer version of a json into normal json and vice versa

Values must not contain {}[] or ,

Quite useful if you want to have json requests inside of a url

( i.e. http://www.yourdomain.com/index.php?request=[array_element,{object:value}] )
*/
class Micro_Json extends Services_JSON
{
function decodeMini($string)
{
$result = preg_replace('/([^{}:\\[\\],]+)/', '"${1}"', $string);
$result = $this->decode($result);
return $result;
}

function encodeMini($object)
{
$object = $this->optimize($object);
if(count($object) == 0)
return "";
$result = $this->encode($object);
$result = preg_replace('/((?<!\\\\)")/', '', $result);
return $result;
}

private function optimize($object, $reorder = false, $sub_reorder = false)
{
if($reorder)
$new_object = array();

if(isset($object) && is_array($object))
{
foreach ($object as $key => &$value)
{
if(is_array($value) || is_object($value))
$value = optimize($value, $sub_reorder, $sub_reorder);

if(!isset($value) || empty($value) || $value == null || count($value) == 0)
{
unset($object[$key]);
}
else
{
//Attach to new array if reorder is true
if($reorder)
$new_object[] = &$value;
}
}
}

if($reorder)
$object = $new_object;

if(is_object($object) && count(get_object_vars($object)) == 0)
{
$object = null;
}
if(count($object) == 0 || empty($object))
{
$object = null;
}

return $object;
}
}

?>



If you would like to download the file, you can through this link. Just rename the extension to php, and you're done (be sure you have Services_JSON lying around).

I'm not that great at explaining my own stuff, probably because it's all old to me now, but if you have questions, leave a comment or drop me a message at floydroute@gmail.com.

Javascript : A Better setTimeout

Ever wish you could just simply give a function a line of code (that's always the same) and that function instantly comes back after a specified duration, INCLUDING the original arguments you passed in? Up until now, I didn't have a real quick way of doing this. You would have to use setTimeout and a bit of other ugly tactics to make this happen.

Not today. Instead, use a simple workaround that takes in the function's arguments and creates a setTimeout callback. Here's the line of code you would include in your functions :
 
// Include it like this :

function someFunction(string)
{
detach(arguments, 600);
//600 is the milliseconds duration until the function call is processed


alert("someFunction : " + string);
}

//And then just run your code :

someFunction("Hello world");

// and after 600 milliseconds, the alert window will come up from someFunction :
// 'someFunction : Hello world'

... and voila, we've got a simple way of detaching the function call from the main process!

Here is the method to make this happen :

function detach(_arguments, duration)
{
if(typeof(duration) == "undefined")
duration = 0;

if(_arguments[_arguments.length-1] != "detached")
{
var argument_string = "setTimeout(function(){_arguments.callee(";

for(var i = 0; i < _arguments.length; i++)
{
argument_string += "_arguments[" + i + "], ";
}

argument_string += "'detached')}, " + duration + ");";

eval(argument_string);

ignore.fail();
}
}


Use this function in areas where you don't want a function process to halt the user (So they can visit other parts of the web site). Also useful in SyJAX requests, where the transaction with the server may take a while.

Also, notice that I call a non-existant object method('ignore.fail') so that I can fail out the current main process. If anyone knows a better way to break the entire function process trail, please let me know.

Javascript : Non-halting Synchronous Server Function Call Trails

That title is pretty confusing. The main idea that I'm trying to pinpoint is that a client application can make synchronous function call trails (I'll explain later) without disabling the user from accessing the other parts of the application.

The way you do it is to use setTimeout and SyJAX (I'll explain in a bit) in conjunction. SyJAX is basically AJAX but without being split from the function process. Using SyJAX, a request is made and the process waits until the response is received. This is great for when we want a piece of information from the server to be stored in a variable in javascript, and that same function needs to return to another function, which needs to return to yet another function, and so on. The problem with AJAX is that it detaches from this function trail and the trail has to terminate. In a function trail that NEEDS a server response stored to a variable in order to continue SHOULD stay and wait for the server response (otherwise we lose the work that has been made in the trail). So here is where knowing XMLHttpRequest can be synchronous is so crucial. Our function trails can now stay intact as they move forward... but there is still another problem.

What happens if the function trail is sitting there waiting for a server response and the response never comes back? The user is now stuck with a disabled browser where they can't use any other functionality. We get around this by using setTimeout. SetTimeout is special because it gives us the capability of setting a function call up asynchronously. So in essence, we split the initial function call off from the main load and it takes on synchronous function calls from then on using SyJAX. This frees the user from having to wait for the function call trail to end and allows them to use the web site in other areas.

Effectively we have a asynchronous call to a synchronous function trail, instead of a synchronous call to an asynchronous function trail. I'm sure I will find additional problems with the solution as I use it more and more, but having this option definately alleviates some problems I have been faced with.

Brett Garrison
Steel