SFSObject problem getting any numeric values in C#

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

Moderators: Lapo, Bax

caseyryan
Posts: 6
Joined: 19 Sep 2018, 12:45

SFSObject problem getting any numeric values in C#

Postby caseyryan » 30 Sep 2018, 15:10

Hi!
I'm pretty sure it's a bug of a C# client but probably I'm missing out on something. Anyway
I'm sending an SFSObject containing some numeric values (long to be exact) from Java side to my C# client.
On the client side I want to get these values but I can't because I'm always getting this exception
InvalidCastException: Specified cast is not valid.
Sfs2X.Entities.Data.SFSObject.GetValue[T] (System.String key) (at <290a162672eb4fae90f7a112700c4182>:0)
Sfs2X.Entities.Data.SFSObject.GetLong (System.String key) (at <290a162672eb4fae90f7a112700c4182>:0)

Here's the values of the object at breakpoint.
http://prntscr.com/l0hdmj
If you look at screenshot you'll see it knows that the number is long but what it does later is wrong because it tries to cast a string value to long in a generic method like this:

Code: Select all

private T GetValue<T>(string key)
    {
      if (!this.dataHolder.ContainsKey(key))
        return default (T);
      return (T) this.dataHolder[key].Data;
    }

Which, of course cannot be performed in C#. I wanted to use a standad approch and use Convert.ToInt64 on a string value, but I can't get the string value neither necause it know the value is numeric.
For now, the only solution I see is to send these values as strings from the java backend and convert them from strings on a client
User avatar
Lapo
Site Admin
Posts: 23027
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: SFSObject problem getting any numeric values in C#

Postby Lapo » 01 Oct 2018, 07:33

Hi,
can you show us the code on the Java side sending the object and the client side that receives it?

Also please specify the server version in use and the version of the client API.

Thanks
Lapo
--
gotoAndPlay()
...addicted to flash games
caseyryan
Posts: 6
Joined: 19 Sep 2018, 12:45

Re: SFSObject problem getting any numeric values in C#

Postby caseyryan » 06 Oct 2018, 17:47

Hi!
I'm using all latest versions
I've found what was the problem, actually. And it's not totally caused by smartfox (only partially). The problem is that I'm using Mongo's BSON documents and their toJson() method
Here's the Java code sending the responce

Code: Select all

// mongoUser is a bson document
ISFSObject userDataResponse = SFSObject.newFromJsonData(mongoUser.toJson());
send(CustomResponses.USER_DATA_CHANGED, userDataResponse, user);


Bson creates some custome types after calling toJson() method, so they must be deserialized on client side using BSON as well, but SmartFox tries to deserialize them its own way. Of course it cannot be performed, that's why everything crashes. I think some way to get raw JSON string from SFS would be nice, is there any?
User avatar
Lapo
Site Admin
Posts: 23027
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: SFSObject problem getting any numeric values in C#

Postby Lapo » 08 Oct 2018, 07:40

Hi,
there can be several issues when converting from and to JSON due to the fact that Javascript types are less granular than those supported by SmartFox. In other words Javascript doesn't support lots of specific numeric types such as Float, Short, Byte, Long etc... They all get converted to a generic Number.

For instance if you have a big Long value it will probably loose precision because JS treats long as double and doesn't support full 64 bit values.

If you don't want to incur in all these pitfalls I'd suggest to create a serializer that converts SFSObject types directly to Bson and viceversa. Using JSON as a middleman is likely to introduce unexpected issues, that might be difficult to track down.

Hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 135 guests