ThomasLund wrote:I just wanted to give a heads up on this little buried gem in the new API.
You can pretty easily send and receive class structures between C# and Java on the backend without having to create intermediate SFSObject structures.
I will soon make a small example project, but here are some of the details on how to get started on this.
First off - read more about this advanced feature here
http://docs2x.smartfoxserver.com/Advanc ... ializationTo get this working in C#, there is one additional nugget of information that you need. In your code you need to add this:
Code: Select all
DefaultSFSDataSerializer.RunningAssembly = Assembly.GetExecutingAssembly();
otherwise the serialization wont work. Also remember (as stated in the linked documentation page) that your class/package names need to match on server and on client side.
Below are some cut'n'pastes from a test case I got. It should serve as example until I get a small project together that you can run.
First up - we make a test class which is what we want to transport
Code: Select all
public class TestObject : SerializableSFSType {
public int intField;
public string strField;
public bool boolField;
public double doubleField;
public NestedObject nestedField;
public ArrayList arrayField;
public Hashtable dictField;
public bool IsEqual(TestObject anotherObject) {
if (this.intField!=anotherObject.intField) return false;
if (this.strField!=anotherObject.strField) return false;
if (this.boolField!=anotherObject.boolField) return false;
if (this.doubleField!=anotherObject.doubleField) return false;
return true;
}
#region SerializableSFSType implementation
public string GetJavaPackageName ()
{
return "";
}
#endregion
}
And here is a usage example where my test code serializes and deserializes the object. Note that here it doesnt actually send it to the server. But inserting a send/receive is trivial.
Code: Select all
[Test]
public void TestClass() {
DefaultSFSDataSerializer serializer = DefaultSFSDataSerializer.Instance;
DefaultSFSDataSerializer.RunningAssembly = Assembly.GetExecutingAssembly();
TestObject testObj = new TestObject();
testObj.intField = 10;
testObj.strField = "test";
testObj.boolField = true;
testObj.doubleField = 1.421;
testObj.nestedField = new NestedObject();
testObj.nestedField.intField = 20;
testObj.arrayField = new ArrayList();
testObj.arrayField.Add((int)1);
testObj.arrayField.Add("testArray");
testObj.dictField = new Hashtable();
testObj.dictField["test1"] = false;
testObj.dictField["test2"] = "testDict";
ISFSObject obj = serializer.Cs2Sfs(testObj);
Assert.IsNotNull(obj);
TestObject receivedTestObj = serializer.Sfs2Cs(obj) as TestObject;
Assert.IsNotNull(receivedTestObj);
Assert.IsTrue(receivedTestObj.IsEqual(testObj));
Assert.IsNotNull(receivedTestObj.nestedField);
Assert.AreEqual(testObj.nestedField.intField, receivedTestObj.nestedField.intField);
Assert.IsNotNull(receivedTestObj.arrayField);
Assert.AreEqual(2, receivedTestObj.arrayField.Count);
Assert.AreEqual(1, (int)receivedTestObj.arrayField[0]);
Assert.AreEqual("testArray", (string)receivedTestObj.arrayField[1]);
Assert.IsNotNull(receivedTestObj.dictField);
Assert.AreEqual(2, receivedTestObj.dictField.Count);
Assert.AreEqual(false, (bool)receivedTestObj.dictField["test1"]);
Assert.AreEqual("testDict", (string)receivedTestObj.dictField["test2"]);
}
Some important notes to get this working:
1. Only non-generic ArrayList and Hashtable work in C#
2. Hashtable can have only use string type as the key
3. The java extension must be in __lib__ folder to allow sending classes from C# (AS3) -> Java
4. C# class needs this: DefaultSFSDataSerializer.RunningAssembly = Assembly.GetExecutingAssembly();
5. Package names need to match in the C# and Java class
Previous requirement of C# class having
  public string GetJavaPackageName ()
   {
    return "";
   }
is not needed anymore