Page 1 of 1

.NET Api: JsonMapper (c#)

Posted: 26 Feb 2010, 14:02
by drjoachim
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.

Posted: 28 Feb 2010, 09:05
by ThomasLund
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