Page 1 of 1

Conversion JAVA List<JSONObject> in Array AS3 failed

Posted: 12 Feb 2012, 21:39
by Hatta
Hi all,
when I try to convert a Collectio <?> JAVA, in this case a List<JSONObject>, in ActionScript Array, the conversion fails and returs me null.

I have a ServerSide JAVA Extension that sends to the client a message with JSONObject format, composed in this manner (I lightened the code and entered fictitious information, the list is long but already it gives me error):

Code: Select all

String username = "pippo";
List<JSONObject> listColor = new ArrayList<JSONObject>();
		
JSONObject color1 = new JSONObject();
color1 .put("description", "black");

listColor.add(color1 );

JSONObject jsonMsg = new JSONObject();
jsonMsg.put("username", username);
jsonMsg.put("listColor", listColor);
When the client receives the message, printing the contents without Array cast, I get this:

Code: Select all

pippo
[{"description":"black"}]
With this code

Code: Select all

private function onExtensionResponse(e:SFSEvent):void{
    var data:Object = e.params.dataObj;
    trace(data.username);
    trace(data.listColor);
}
Now if I would only get the value "descrition" of the first color object, I tried to first cast to Array, but I get null:

With this code

Code: Select all

private function onExtensionResponse(e:SFSEvent):void{
    var data:Object = e.params.dataObj;
    trace(data.username);
    var arrayColor:Array = data.listColor as Array;
    trace(arrayColor);
}

pippo
null
I also tried this type cast Array(data.listColor) but the result is the same. Consequently, attempting to enter the first item:

Code: Select all

 trace(data.username);
    var arrayColor:Array = data.listColor as Array;
    trace(arrayColor[0]); // or arrayColor["descritpion"]


all crashes (obviously because it is null):

Why? How i can convert a Collection JAVA?

Posted: 12 Feb 2012, 21:52
by rjgtav
Hi. Have you tried using a JSONArray instead? More help here.

Posted: 13 Feb 2012, 00:50
by Hatta
Hello and thanks for the answer.
Now I tried to use the structure of JSONArray instead of the List<JSONObject> but nothing, however I get null when I run the cast :(

Posted: 14 Feb 2012, 00:11
by Hatta
I did it! :D
I understand how to convert a Java Collection of elements JsonObject as <JSONObject> List in ActionScript 3 array.

The problem is the type of JSONObject used.
There are two types of JSONObject:
  • org.json.JSONObject
  • net.sf.json.JSONException
Which to use?
The confusion is (IMHO) caused by the method handleRequest present in the interface AbstractExtension.

The signature of the method was defined with the object org.json.JSONObject but a ServerSide Extension (SSE) can send a message to the client using both objects.
In fact there are two sendResponse methods , one which makes use of org.json.JSONObject and the other which makes use of net.sf.json.JSONObject.

I did some testing and I noticed the following behavior.
If the SSE must send the client a single JSONObject, the use of two objects is irrelevant. Example

Code: Select all

------------ JAVA ServerSide Extension ------------

import org.json.JSONObject;
	
public void handleRequest(String cmd, JSONObject params, User user, int fromRoom)
{
	JSONObject obj = new JSONObject();
	obj.put("msg", "hello");
		
	// send message to client
}

	
------------ Client	------------

private function onExtensionResponse(e:SFSEvent):void
{
	var dataObj:Object = e.params.dataObj;
	var msg:String = dataObj.msg;
	trace(msg); //print hello
}
It's ok.

Instead, if the SSE must send the client a JAVA Collection JSONObject (such as List <JSONObject>, as in my case) to cast to Array AS3 is successful, the collection must contain net.sf.json.JSONException.
Vice versa, if you use org.json.JSONObjectin the JAVA collection, cast to Array AS3 will return null (which is what happened to me, see my first post in this thread).

Code: Select all

------------ JAVA ServerSide Extension ------------

import org.json.JSONObject;
	
public void handleRequest(String cmd, JSONObject params, User user, int fromRoom)
{
	// Set Collection List JAVA to send to client
	List<net.sf.json.JSONException> list = new ArrayList<net.sf.json.JSONException>();
	
	net.sf.json.JSONException test = new net.sf.json.JSONException();
	test.put("msg", "hello");
		
	list.add(test);
			
		
	// Send to client
	net.sf.json.JSONException sendToClient = new net.sf.json.JSONException();
	sendToClient.put("list", list);
}

	
------------ Client	------------

private function onExtensionResponse(e:SFSEvent):void
{
	var dataObj:Object = e.params.dataObj;
	var msg:Array = dataObj.list as Array;
	trace(msg[0].msg); //print hello
}
Unfortunately (IMHO) you are forced to use a handeRequest which uses a org.json.JSONObject, but this does not always go well.

At this point I wonder: but it was better to build an interface AbstractExtension that used only and exclusively objects net.sf.json.JSONException?
Or, as done for the method SendResponse, add another method handleRequest so that both items were good?

I hope I was clear, maybe someone can come in handy.
Bye


PS. I post reply e not edit for to up the message

Re: Conversion JAVA List<JSONObject> in Array AS3 failed

Posted: 15 Feb 2012, 18:43
by Lapo
At this point I wonder: but it was better to build an interface AbstractExtension that used only and exclusively objects net.sf.json.JSONException?
Or, as done for the method SendResponse, add another method handleRequest so that both items were good?
The reason is for previous compatibility. If I recall correctly until SFS1.5 it was only available the most basic JSON implementation (org...) then we were able to introduce the new one but we had to keep both in order not to break previous user's code.