.NET Api: JsonMapper (c#)

Post here all your questions related with SmartFoxServer .Net/Unity3D API

Moderators: Lapo, Bax

drjoachim
Posts: 1
Joined: 26 Feb 2010, 13:47
Contact:

.NET Api: JsonMapper (c#)

Postby drjoachim » 26 Feb 2010, 14:02

Today i was trying to send data from the serverextension to a client with Json. On the client side I have the following code:

Code: Select all

 public void OnExtensionResponse(object data, string type)
    {
       
        // Handle XML responses
        if (type == SmartFoxClient.XTMSG_TYPE_XML)
        {
         SFSObject responseData = (SFSObject)data;
       
            // TODO: check command and perform required actions
        }

        // Handle RAW responses
        else if (type == SmartFoxClient.XTMSG_TYPE_STR)
        {
            string responseData = (string)data;
            // TODO: check command and perform required actions
        }
        else if (type == SmartFoxClient.XTMSG_TYPE_JSON)
        {
            JsonData responseData = (JsonData)data;
            SimpGridNode test = JsonMapper.ToObject<SimpGridNode>(responseData["nodes"].ToJson());
            Debug.Log(test.column);
         
        }


       
    }


And I kept getting the following error:

Code: Select all

Error   3   The non-generic method 'LitJson.JsonMapper.ToObject(LitJson.JsonReader)' cannot be used with type arguments   C:\xxx\LoginGUI.cs   219   44   Unity


although in the documentation of the litJson library (http://litjson.sourceforge.net/doc/manual.html#quickstart.mapping) it is mentioned that this should be possible.

So I downloaded the sourcecode of the LitJson and the sources of the SmartFoxClient and added the following lines to JsonMapper in SmartFoxClient:

Code: Select all

public static T ToObject<T>(JsonReader reader)
        {
            return (T)ReadValue(typeof(T), reader);
        }

        public static T ToObject<T>(TextReader reader)
        {
            JsonReader json_reader = new JsonReader(reader);

            return (T)ReadValue(typeof(T), json_reader);
        }

        public static T ToObject<T>(string json)
        {
            JsonReader reader = new JsonReader(json);

            return (T)ReadValue(typeof(T), reader);
        }


After building and importing the dll into my .net application the problem was solved.

Now I wondered if this is a bug in the SmartFoxClient API or if i should use another method?

Thx in advance and sorry for my poor English.
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 28 Feb 2010, 09:05

The mono versions that Unity (and especially Unity iPhone) uses do not allow for much generics usage.

So we had to rewrite and limit some things compared to the original json library, as to be able to have the same code run on all Unity platforms.

So that might be what you are having an issue with, more than a bug. But I dont claim the API to be bugree - so you never know :-)

/Thomas

Return to “.Net / Unity3D API”

Who is online

Users browsing this forum: Google [Bot] and 1 guest