I did it!

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