Page 1 of 1

PEAR Services_JSON , Question

Posted: Sat Apr 07, 2007 6:29 pm
by powerful0x0
Hello,

im looking forward for good JSON to PHP and PHP to JSON class...

It doesnot seems i have too many options at PHP4.

i tried Pear Json and it seems good ...
http://pear.php.net/pepr/pepr-proposal-show.php?id=198

just one thing i cant understand, how do i pass a real javascript function through PEAR json encoder/decorder.

the class works good for any object or array/strings but im not sure how to pass a function and if it's possible.

Code: Select all

$value = array("foo" => "aaa", "bar", array(1, 2, "baz"), array(3, array(4)));

$obj1->car1->color = "yea";
$obj1->car2->color = $value;


echo($json->encode($obj1));
What i also want is:

Code: Select all

$obj1->car2->color = function(){

javascript stuff.
};
I know it wouldnot be this way.. but how can i do through php? i want to pass real javascript function through the json object.

Is it possible or i have to create my own engine at this case?

Also if you have any good replacement for the PEAR class. please advice.

Sorry if i'm not so clear, hope you got the main idea.

Thanks,
Samer

Posted: Sat Apr 07, 2007 7:31 pm
by aaronhall
I'm trying to think of why you would want to serialize a function to json

Posted: Sun Apr 08, 2007 8:10 am
by Weirdan
It wouldn't be a valid json string if it contained functions.

Posted: Sun Apr 08, 2007 11:13 am
by Buddha443556
Weirdan wrote:It wouldn't be a valid json string if it contained functions.
Wierdan, correct. However, there's nothing stopping anyone from doing it either which is why no one should trust external sources of Json. In jQuery, a Json string is evaluate like this:

Code: Select all

eval( "data = " + data );
So sending something like this:

Code: Select all

echo json_encode($output)."; alert('Hello World')";
... and you get a Javascript Alert box popping up.
just one thing i cant understand, how do i pass a real javascript function through PEAR json encoder/decorder.
I usually send an object that when processed calls various Javascript functions against various elements of the page which makes for a very flexible display refresh routine.

Code: Select all

function refreshDisplay(data) {

	for (var i in data) {
		if(data[i].action == 'html') {
			$('#' + i).html(data[i].content);
		}
		if(data[i].action == 'html-scrollto') {
			$('#' + i).html(data[i].content);
			$('#' + i).ScrollTo(0,'none');
		}
		if(data[i].state == 'show') {
			$('#' + i).show();
		}
		if(data[i].state == 'hide'){
			$('#' + i).hide();
		}
		bindContent(i);
	}

	
};

Posted: Sun Apr 08, 2007 12:06 pm
by powerful0x0
First of all thanks for all of you.

Yes, i agree this is not a correctly valid json string as stated on the json.org but as Buddha443556, there is nothing stopping us from doing that way, because simply eval function evalulate the json string no matter how it looks as long as it's valid string..

So you can send functions, complete js objects and classes (holding anything you want) through json.
I usually send an object that when processed calls various Javascript functions against various elements of the page which makes for a very flexible display refresh routine.
This is exactly what i'm trying to do... i just wanted to give myself more control to post a complete objects/classes incase i don't want to play much at the javascript files....

i don't like the idea of going through js files/php files etc...

I think in this case i can use smarty template engine to render functions. this gives more control over what you can do with a json response.

following Buddha443556 example... you can also add new routine:

case > data.action == 'run'

then it would excute the function attached with this object.

data.excute()