Forum C# how to send object by Message?

how to send object by Message?

Postby Soulghai » June 10th, 2014, 2:27 pm

Hello.
My english is really bad.
On the server side I create DatabaseObject and try send it to client.
Code: Select all
DatabaseObject _dbObj = new DatabaseObject();
_dbObj.Set("field1", 1);
_dbObj.Set("field2", "value2");
player.Send("Data", _dbObj);  //send data to client


But nothing happens. If I try send Int, String.. or other simply types all works well.
Any ideas =)? Can I send to client DatabaseObject or DatabaseArray or some Class object...

Thanks =)
Soulghai
 
Posts: 2
Joined: April 3rd, 2014, 9:55 pm

Re: how to send object by Message?

Postby frank84 » June 10th, 2014, 4:48 pm

I don't think there is a way. Usually when I want to send over a dictionary of data from C# to the client, I copy it over to a Hashtable and then encode it to a JSON string using MiniJSON (https://www.dropbox.com/s/moxct258vrennmh/MiniJSON.cs)

Hashtable myHashtable= new Hashtable();
myHashtable["field1"] = myObject.GetInt("field1");
myHashtable["field2"] = myObject.GetString("field2");
MiniJSON json = new MiniJSON();
string jsonString = json.jsonEncode(myHashtable);

I'm sure there is a way to iterate over the DatabaseObject's keys and add them automatically to the Hashtable as well, but I usually only want to send over specific things.
frank84
 
Posts: 27
Joined: November 23rd, 2013, 2:09 am

Re: how to send object by Message?

Postby Soulghai » June 10th, 2014, 7:35 pm

Can i serialize Class with this?

Code: Select all
public class MyClass
{
     public int a;
     public string b;
}

MyClass _class = new MyClass();

Hashtable myHashtable= new Hashtable();
myHashtable["Class"] = _class;
MiniJSON json = new MiniJSON();
string jsonString = json.jsonEncode(myHashtable);


How can I do something like this?
Soulghai
 
Posts: 2
Joined: April 3rd, 2014, 9:55 pm

Re: how to send object by Message?

Postby frank84 » June 10th, 2014, 8:25 pm

No, this is a very basic json encoder. You can pass it Hashtable, ArrayList, or Dictionary<string,string>, which themselves can contain Hashtable, ArrayList, Dictionary<string, string>, Array, string, char, bool, and null. All numbers get converted to doubles.
frank84
 
Posts: 27
Joined: November 23rd, 2013, 2:09 am


Return to C#