JSON Libraries

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

Moderators: Lapo, Bax

Post Reply
mugambs
Posts: 32
Joined: 11 Dec 2008, 12:05
Location: United Kingdom

JSON Libraries

Post by mugambs »

Hi,

I'm a bit confused about which JSONObject I should be using. I have 2 JSON related JAR's in my classpath json.jar and json-lib-2.1-jdk1.5.jar.

According to the API docs, the handleRequest() method takes an org.json.JSONObject, but the sendResponse method gives me the option of sending back either an org.json.JSONObject, or a net.sf.json.JSONObject.

I'm sure there's a good reason for this. I just want to clarify which I should use / what the best practice is

Thanks.

Mo
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

Hi there,
the good reason is that at the time when JSON was first introduces in SmartFoxServer only one library was available for Java. It was org.json.*

The library works ok but it's not fantastic, it lacks some features and has some annoying quirks like having to use a try/catch block for getting data etc... Later in version 1.5.x we added also the (at that time) new and much better net.sf.* library which has a few advantages.

In order not to break compatibility with the previous extensions we didn't touch the handleRequest method but we added an overloaded version of the sendResponse that supports those classes.

You're free to choose the that you prefer
Lapo
--
gotoAndPlay()
...addicted to flash games
mugambs
Posts: 32
Joined: 11 Dec 2008, 12:05
Location: United Kingdom

Post by mugambs »

Thanks,

I realised the problem with catching exceptions each time you put data into the JSONObject, and so went with net.sf.json.JSONObject for the response.

Will you be overloading the handleRequest method at any point?

Also, do you know if the Unity / C# API works with both JSON libraries? Maybe thats a question for another forum...
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

Will you be overloading the handleRequest method at any point?
Yes, it is very likely.
Also, do you know if the Unity / C# API works with both JSON libraries? Maybe thats a question for another forum...
Yes, all protocols are supported
Lapo
--
gotoAndPlay()
...addicted to flash games
Nebulous
Posts: 13
Joined: 30 Jul 2009, 21:18

Post by Nebulous »

A note about the json.jar file included with SFS PRO 1.6.6... it doesn't seem to be compatible with the current org.json.* source code available at json.org. Just to get access to some of the missing methods like JSONObject.append, I built my own json.jar using that code and I ended up with these exceptions when I send a JSON request to my extension:

12:12:33.478 - [ INFO ] > { DATA IN } : {"t":"xt","b":{"c":"myExtCommand","p":{},"r":1,"x":"myExt"}}
12:12:33.478 - [ WARNING ] > Generic Exception: java.lang.NoSuchMethodError: org.json.JSONObject.getData()Ljava/uti
l/HashMap;
java.lang.NoSuchMethodError: org.json.JSONObject.getData()Ljava/util/HashMap;
at it.gotoandplay.smartfoxserver.SmartFoxServer.dispatchEvent(SmartFoxServer.java:1183)
at it.gotoandplay.smartfoxserver.SmartFoxServer.readIncomingMessages(SmartFoxServer.java:1060)
at it.gotoandplay.smartfoxserver.EventReader.run(EventReader.java:32)
at java.lang.Thread.run(Thread.java:619)

So for whatever reason, the getData method that dispatchEvent calls isn't in the latest json.org source code. I just thought I should mention that. Oh well, I'll probably just switch to using net.sf.* eventually anyway...
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

The two libraries are not compatible, of course but they can be used together.
Please read here:
http://forums.smartfoxserver.com/viewtopic.php?t=5123
Lapo
--
gotoAndPlay()
...addicted to flash games
Nebulous
Posts: 13
Joined: 30 Jul 2009, 21:18

Post by Nebulous »

Wait a minute, did you just post a link back to this same thread? :D

Anyway, I ended up just changing our code to use net.sf.json. Until you add an overload for handleRequest, we can just do the conversion ourselves. In case anyone was wondering, here's our code for that:

Code: Select all

import net.sf.json.*;
public void handleRequest(String cmd, org.json.JSONObject j, User u, int fromRoom)
{	
	JSONObject jso = new JSONObject();			
	for(Iterator it = j.keys(); it.hasNext(); )
	{
		String key = (String) it.next();
		try {
			jso.put(key, j.get(key));
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	// Code to parse jso and handle the request goes here...
}
Thanks Lapo!
zoobr
Posts: 1
Joined: 14 Sep 2009, 14:02

Post by zoobr »

And how about using sendXtMessage from java client api?
When I tried to use net.sf.json.JSONObject on Java client, it cannot resolve this metod. Is it possiple to use net.sf from Java client?
Or I can use only org.json.JSONObject (including the server side)?
Post Reply