PEAR Services_JSON , Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
powerful0x0
Forum Newbie
Posts: 7
Joined: Fri Apr 06, 2007 12:46 pm

PEAR Services_JSON , Question

Post 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
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

I'm trying to think of why you would want to serialize a function to json
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

It wouldn't be a valid json string if it contained functions.
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post 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);
	}

	
};
powerful0x0
Forum Newbie
Posts: 7
Joined: Fri Apr 06, 2007 12:46 pm

Post 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()
Post Reply