Hints on JSON Object use in java

Post here your questions about Actionscript and Java server side extensions development.

Moderators: Lapo, Bax

Post Reply
nig3d
Posts: 164
Joined: 02 Jul 2008, 09:42

Hints on JSON Object use in java

Post by nig3d »

Hi,

in one of our extension, this time written in Java, we have this member:

Code: Select all

 public static ConcurrentHashMap<String, Map<String, Object>> userList; 
Then we want to send the userList to the client using this code:

Code: Select all

  jso.put("usersList", userList);
            LinkedList recipients = new LinkedList();
            recipients.add(user.getChannel());
            sendResponse(jso, fromRoom, user, recipients); 
Is it supposed to work? I have the doubts that I can't serialize to the client a Map Object, am I correct? How can we know the limits of the serialization? Do we must be sure that AS can recnognize the objects we want to use?
Thank you for your help.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

I would simply suggest to print the content of the jso with jso.toString() or similar. It should have a method that dumps() the actual json data. This way you can double check if it's legal.

If at runtime it doesn't fire any errors I think it should work.
And it should result in an object inside another object, from an AS3 perspective
Lapo
--
gotoAndPlay()
...addicted to flash games
nig3d
Posts: 164
Joined: 02 Jul 2008, 09:42

Post by nig3d »

The problem in my opinion is the Map. Java of course knows the Map class and JSON java object can serialize it, like this:

usersList={01-22={host=01, name=pippo323, rooms=[]}, 01-21={host=01,
> name=fatfatpig, rooms=[]}}

while the java hash is in practice an associative array, what about the Map?
How the AS object see the "01-22" map value?
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

while the java hash is in practice an associative array, what about the Map?
I am not getting the question.
In java a Map represents a key-value pair based collection... there are many names for this same thing in other languages: associative array, dictionary etc...

HashTable, HashMap, ConcurrentHashMap etc... are all the same type of key-value pair based collections with slightly different characteristics.
The "hash" thing simply describes a technique (called "hashing") for speeding up the research of items in the Map

Did you succeed serializing the ConcurrentHashMap<String, Map<String, Object>> ?
Does it work?
Lapo
--
gotoAndPlay()
...addicted to flash games
nig3d
Posts: 164
Joined: 02 Jul 2008, 09:42

Post by nig3d »

Hello,

I don't know Java, but if it works like the C++ stl a map can't be translated in an AS Object, at most it can be a Dictionary.

Anyway this is what one of my colleague reported:

after a deep examination I found that there is a problem in the json.jar package bundled with SmartFoxServer;
this library is loaded by the server but it's a old (maybe bugged software) version that lacks some functionality;
this package doesn't conform to the JSON RFC 4627 in the fact that isn't able to serialize very complex structured data;
the RFC 4627 requires that all objects keys must be double quoted ( mainly key => string values ) but the current library stops at the first level of a structure without descending deeply;
I suppose that a wrong formatted json object is the cause of client flash API decoding errors;

After some test, I've compiled the most recent ( latest version is @version 2008-09-18 ) json java source and I've built a new package that I've updated on Rome and Singapore servers;

the old extension response was:
{"userList":"{01-3389={host=01, name=mirmor, rooms=[2568]}, 02-3514={host=02, name=kikkosa, rooms=[2860]}}"}

and the new extension response is:
{"userList":{"01-3389":{"host":"01","name":"mirmor","rooms":[2568]},"02-3514":{"host":"02","name":"kikkosa","rooms":[2860]}}}

but the situation seems more complex and problematic and I've rollbacked all:

SFS logs give errors on it.gotoandplay.smartfoxserver.SmartFoxServer.dispatchEvent method line 1190: java.lang.NoSuchMethodError.

it.gotoandplay.smartfoxserver.SmartFoxServer not only uses the old library but calls a JSONObject method, "getData()",
( it should be: HashMap jso = (new JSONObject(msg)).getData();) that doesn't exist anymore in the latest library (not library upgrading is probably due to that) and the source code and the api doc confirm my sad discovery.

SFS json.jar package CAN'T be used for complex structured data exchange.


Anyway, beside the things reported, I still can't understand how complex objects can be mapped into AS object.
I'm intereted to understand.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

About the long post of your colleague:
1. could you specify which languge is used on the server side? Java? Actionscript?

2.
Anyway, beside the things reported, I still can't understand how complex objects can be mapped into AS object.
I'm intereted to understand.
I don't know exactly what are you referring to when you say "complex object" :)
You can't map a Java class to an Actionscript class for obvious reasons. But you can send structured data from Java using Maps and Lists and obtain an equivalent structure in Actionscript that uses Objects and Arrays.

So in other words:
Java Map ==> JSON ==> Actionscript Object
Java List ==> JSON ==> Actionscript Array

By mixing/nesting Maps & Lists (java) or Objects & Arrays (AS) you can exchange quite complicated game data.

Makes sense?
Lapo
--
gotoAndPlay()
...addicted to flash games
nig3d
Posts: 164
Joined: 02 Jul 2008, 09:42

Post by nig3d »

errr, I wrote the code in the first post :)

anyway, since I'm very kind I will copy and paste it (joking ;) )

Code: Select all

public static ConcurrentHashMap<String, Map<String, Object>> userList;
I suggest you to read again what my colleague reported to me, also to know if he was right.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

It was not clear who was doing what :) So I guess you posted the code instead of your colleague and then reported what he said ... whatever :D

In Java we have updated the JSON support since 1.5.9 or so with an alternative library that should conform the standards.
Here's the library --> http://json-lib.sourceforge.net/

The AbstractExtension class that you extend exposes the following JSON related methods:

Code: Select all

public void sendResponse(JSONObject jso, int fromRoom, User sender, LinkedList recipients)

Code: Select all

public void sendResponse(net.sf.json.JSONObject jso, int fromRoom, User sender, LinkedList recipients)
The 2nd version takes ad the JSON object argument from the new library, so you can use that instead which should help you with the proper serialization.

Let me know if it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
LEAn
Posts: 16
Joined: 21 Aug 2008, 11:01

Post by LEAn »

We are getting the same

Code: Select all

 Generic Exception: java.lang.NoSuchMethodError: org.json.JSONObject.getData()Ljava/util/HashMap;
java.lang.NoSuchMethodError: org.json.JSONObject.getData()Ljava/util/HashMap;
        at it.gotoandplay.smartfoxserver.SmartFoxServer.dispatchEvent(SmartFoxServer.java:1150)
        at it.gotoandplay.smartfoxserver.SmartFoxServer.readIncomingMessages(SmartFoxServer.java:1027)
        at it.gotoandplay.smartfoxserver.EventReader.run(EventReader.java:32)
        at java.lang.Thread.run(Unknown Source)
and not on send message from server, but on receive it from client. We could not use net.sf.json.JSONObject in this case tom improve situation.

It there any thoughts?
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

Which server version is used?
Could you show the client side debug of the request that causes this exception?

Thanks
Lapo
--
gotoAndPlay()
...addicted to flash games
LEAn
Posts: 16
Joined: 21 Aug 2008, 11:01

Post by LEAn »

Lapo wrote:Which server version is used?
Could you show the client side debug of the request that causes this exception?

Thanks
Server is 1.6.5.
Here is message to server, which causes exception.

Code: Select all

{"t":"xt","b":{"x":"RouletteExtension","r":58,"p":{"bets":[{"t":"straightUp","m":2,"a":["19"]},{"t":"split","m":1,"a":["13","14"]},{"t":"straightUp","m":1,"a":["26"]},{"t":"straightUp","m":1,"a":["31"]},{"t":"straightUp","m":1,"a":["14"]},{"t":"square","m":1,"a":["25","26","28","29"]}]},"c":"RouletteSpin"}}
What do you mean by "client side debug"?
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

What do you mean by "client side debug"?
The debug flag of the SmartFoxClient class, when turned on it will log debugging information in the Flash IDE console or Flex console.

The json expression you specified in the post is valid and correct, and I am able to parse it with the org.json.JSONObject class used in SmartFoxServer.

I think I would need to see both the code sending the data and the relative client debug informations.
Lapo
--
gotoAndPlay()
...addicted to flash games
LEAn
Posts: 16
Joined: 21 Aug 2008, 11:01

Post by LEAn »

Seems I've found source of the problem. It was another lib with org.json used in another component and copied to SFS.
Thanks for help!
Post Reply