Sign In Register

How can we help you today?

Start a new topic
Solved

GSData class in Unity SDK has some faulty getter methods

Three getter methods in the GSData class in the Unity SDK are making an incorrect check against the "double" type before returning a value:


GetFloat:

public float? GetFloat (string name)
{
	if (this._data.ContainsKey (name) && this._data.get_Item (name) is double)
	{
		return new float? (Convert.ToSingle ((double)this._data.get_Item (name)));
	}
	return default(float?);
}

 

GetInt:

public int? GetInt (string name)
{
	if (this._data.ContainsKey (name) && this._data.get_Item (name) is double)
	{
		return new int? (Convert.ToInt32 ((double)this._data.get_Item (name)));
	}
	return default(int?);
}

 

GetLong:

public long? GetLong (string name)
{
	if (this._data.ContainsKey (name) && this._data.get_Item (name) is double)
	{
		return new long? (Convert.ToInt64 ((double)this._data.get_Item (name)));
	}
	return default(long?);
}

 

It appears all three were just copy-and-pasted from the GetDouble method. Unfortunately, due to that "is double" check in the if statement, all three of these getters now return a null value instead of returning the value you put there via one of the AddNumber() overloads.


Also, I noticed the GetNumber(string name) method simply forwards the call to GetLong(). For most users, I doubt this will behave as expected since they may have added an int via the AddNumber() overload, and they might very well expect an int to be returned when calling GetNumber(). But instead, it will return null. I think the GetNumber(string name) method should be removed to avoid confusion.


Hi Luke


For a bit of background, all numbers are doubles in JSON and this is why it's always checking for double. It's assuming the data it's getting has been created from a response.. 


You're correct in spotting this does not work if you set a value and then try to get it back. I'll get the team to take a look at this to make sure it works in both scenarios.


Gabriel

Hi Gabriel,


That makes sense. I was coming at it from the GSRequestData point of view, and that's where all this unexpected behavior occurs.


At any rate, it's an easy work around for me since all the data are available in the GSData.BaseData dictionary anyway.


Thanks for the quick response!


-Luke

Login to post a comment