Problem when creating an object from json.

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

matiasgil
Posts: 1
Joined: 06 Feb 2018, 18:57

Problem when creating an object from json.

Postby matiasgil » 06 Feb 2018, 19:07

Hello,
i´m Matias and im new at the forum, right now i´m working on a unity app that uses smartfox as it´s main server service (it uses rest for other stuff).

The thing is that i'm working on a test platform that can test different situations or features without the need of the server. For that purpose i have some .json files that have mocked data, so we can create the sfsobject from them and then pass them to the correct actor that works with that response. The problem is that for some reason everything works perfectly when the object comes from the server, but when it is created with SFSObject.NewFromJsonData(json) method and when i try to retrieve the parameters from that sfsobject i get this error "NotSupportedException: SFSObjectLite doesn't support class serialization".
I have a generic method that gets parameters from sfsobject in a safe way, but i use GetClass method in it to keep it generic, and somehow if i use the type based getters from sfsobject there is no problem with it.
So my question is, is there a way i can use GetClass method to get different types from a SFSObject created with SFSObject.NewFromJsonData(json) method?

Just in case here is my generic parametter getter:

Code: Select all

public static bool TrySafeGetParameter<T>(this SFSObject @params, string paramName, out T obj)
        {
            if (@params.ContainsKey(paramName))
            {
                if (@params.IsNull(paramName))
                {
                    obj = default(T);
                    return false;
                }
                else
                {                       }
                    object tempObj = @params.GetClass(paramName);
                    if (tempObj is T)
                    {
                        obj = (T)tempObj;
                        return true;
                    }
                    else
                    {
                        obj = default(T);
                        return false;
                    }
                }
            }
            else
            {
                obj = default(T);
                return false;
            }
        }
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Problem when creating an object from json.

Postby Lapo » 07 Feb 2018, 09:11

Hi,
matiasgil wrote:The thing is that i'm working on a test platform that can test different situations or features without the need of the server. For that purpose i have some .json files that have mocked data, so we can create the sfsobject from them and then pass them to the correct actor that works with that response.


I am not sure I fully understand the use case here. You're attempting to test the client without a server connection, by mocking SFSObjects from json files... but the client API have their event pipeline that can't be bypassed.

Maybe you can simulate Extension responses by creating the SFSObject somewhere else and pretending the server sent it, but all of the other system events (chat, user enter, user leave, room/user vars...) are not that simple to mock.

As regards this error:
"NotSupportedException: SFSObjectLite doesn't support class serialization".

keep in mind that JSON doesn't support the full range of types that the SFS2X protocol can describe. JSON introduces ambiguity between numeric types (decimal vs non decimal) and doesn't support class serialization, which is described here:
http://docs2x.smartfoxserver.com/Advanc ... ialization

Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 69 guests