Page 1 of 1

JSON protocol data problem

Posted: 30 Aug 2007, 07:30
by darnpunk
I am trying to convert all my extensions from using default XML protocol to JSON protocol.

However, I have some minor issues that I need to clarify before I go on a full scale editing to all my extensions. Currently I have an extension calling data from a db this way.

Code: Select all

-- Server extensions
var names = []

var sql = "getNames";
var queryRes = dbase.executeQuery(sql);
if (queryRes != null) {
	if (queryRes.size() > 0) {
		for (var i = 0; i < queryRes.size(); i++) {
			var nameList = [];
			nameList[0] = tempRow.getItem("id"); // returns correct id
			nameList[1] = tempRow.getItem("name"); // returns name
			names.push(nameList);
		}
	}
}

var res = {}
res._cmd = "nameList";
res.namelist = names;
trace(res.namelist) // returns correct values on server trace
_server.sendResponse(res, -1, null, [user], "json");

---- Client
case "nameList" :
		var nameList = resObj.namelist;
		trace("nameList: " + nameList); 
		// this returns blank (not undefined values but blank) using json protocol but using xml it returns correctly
		break;
When I receive the data on client side, everything is blank. However if I change to XML protocol, it returns correctly.

I then tried changing:

Code: Select all

FROM 

nameList[0] = tempRow.getItem("id"); // returns correct id
nameList[1] = tempRow.getItem("name"); // returns name

TO

nameList[0] = String(tempRow.getItem("id")); // returns correct id
nameList[1] = String(tempRow.getItem("name")); // returns name
and it works...

Is this the problem with JSON protocol? Do I have to convert all data to a type, if not the protocol will actually convert it to blank? A little bit confusing here... cos I thought JSON and XML protocol is almost the same.

Regards,
darnpunk

Posted: 05 Sep 2007, 09:23
by darnpunk
Anyone?

Posted: 06 Sep 2007, 05:24
by piser
Try this:

Code: Select all

_server.sendResponse(res, fromRoom, user, [user], _server.PROTOCOL_JSON)

Posted: 06 Sep 2007, 06:38
by darnpunk
I tried with that... No luck still :?

Posted: 06 Sep 2007, 07:14
by Lapo
could you copy / paste the response received in JSON format from the server?

Posted: 06 Sep 2007, 07:40
by darnpunk

Code: Select all

-- Server extensions
var names = []

var sql = "getNames";
var queryRes = dbase.executeQuery(sql);
if (queryRes != null) {
   if (queryRes.size() > 0) {
      for (var i = 0; i < queryRes.size(); i++) {
         var nameList = [];
         nameList[0] = tempRow.getItem("id"); // returns correct id
         nameList[1] = tempRow.getItem("name"); // returns name
         names.push(nameList);
      }
   }
}

var res = {}
res._cmd = "nameList";
res.namelist = names;
trace("res.namelist: " + res.namelist) // returns correct values on server trace
_server.sendResponse(res, -1, null, [user], "json");

-- Client side
if (type == "json") {
	switch (resObj._cmd) {
	case "nameList" :
		trace("nameList (json)!");
		var nameList = resObj.namelist;
		trace("nameList: " + nameList);
		for (var i in nameList) {
			trace("Name: " + nameList[i] + " typeof > " + typeof nameList[i]);
		}
		break;
	}
}
trace output(server):
res.namelist : 1, darnpunk, 2, tester

trace output(client):

nameList (json)!
nameList: ,,,,
Name: ,, typeof > object
Name: ,, typeof > object

Is this what you want Lapo?

Posted: 06 Sep 2007, 08:13
by patso
I don't know if this have to do something with the problem but:

when you try to send array or null to the server instead of object(actually as far as I know array is object in js/as) when you using JSON an exception is fired on server side - that because the java expect JSONObject but array are JSONArrays and null is JSONNull. I think if I use xml there is no problem - i make this test a 2-3 months ago so I'm not completely sure now.

Just in case to make sure that this time I'm clear:
if I try to send ["a", "b"] to the server with JSON this cause error. If I try to send {params:["a", "b"]} this works fine with JSON too.

Posted: 10 Sep 2007, 09:38
by darnpunk
Hmmm... I tried sending an array using the jsonsample.as in smartfoxserver examples but it seems to be working.

Code: Select all

var obj = {}
obj.name = "Arsyad";
obj.age = 22;

function init()
{
	retro_new[1] = obj.name;
	retro_new[2] = obj.age;

}
When I retrieved the values, there is no problem. Strangely, it seems to be something to do with the object returned from database?

Maybe the json parser does not recognize the data type returned by the database manager? But again, the typeof tempRow.getItem("name") returns an object type, which the json parser recognizes. Strange :? but interesting!

Regards,
darnpunk

Posted: 11 Sep 2007, 05:31
by darnpunk

Code: Select all

nameList[0] = tempRow.getItem("id");
nameList[1] = tempRow.getItem("name");
tempRow.getItem("id"); // typeof = object

I did a trace in function __convertToJSON(obj) for mainLib.as and found out that the values passed by database is an object type always.

If I were to do it:

Code: Select all

var myTempRow = {} // object
myTempRow = "darnpunk"
nameList[0] = myTempRow;
The trace will now return the value as a String type. As for the earlier example, getting values from database will always return an object type that the parser seems to mishandle. I believe the key issue here is the data returned by the database manager which the json parser seems to be misreading. Someone correct me if I am wrong
:wink:

Regards,
darnpunk