doghappy / socket.io-client-csharp

socket.io-client implemention for .NET
MIT License
721 stars 124 forks source link

JsonSerializer Support for On-Calls #314

Open GregorBiswanger opened 1 year ago

GregorBiswanger commented 1 year ago

hey,

the documentation says that you can simply replace the serializer with Newtonsoft. This also works with data that I submit with Newtonsoft Json via the Emit method without any problems. However, with On calls, I still get the data using System.Text.Json. Can I change that too?

By the way. The documentation says that you can replace the serializer as follows:

var jsonSerializer = new NewtonsoftJsonSerializer();
jsonSerializer.OptionsProvider = () => new JsonSerializerSettings
{
    ContractResolver = new DefaultContractResolver
    {
        NamingStrategy = new CamelCaseNamingStrategy()
    }
};
client.JsonSerializer = jsonSerializer;

However, the OptionsProvider property does not exist. I now have the following in my code, is this correct?

var socket = new SocketIO(uri);
var jsonSerializer = new NewtonsoftJsonSerializer(new JsonSerializerSettings
{
    ContractResolver = new CamelCasePropertyNamesContractResolver(),
    NullValueHandling = NullValueHandling.Ignore,
    DefaultValueHandling = DefaultValueHandling.Ignore
});

socket.JsonSerializer = jsonSerializer;
doghappy commented 1 year ago

I now have the following in my code, is this correct?

Yes, that correct.

JsonSerializer Support for On-Calls

It already supported. try:

server:

socket.emit('hello', { username: 'test' }, { province: 'xxx', city: 'abc' });

client:

io.On("hello", res =>
{
    var person = res.GetValue<Person>();
    var address = res.GetValue<Address>(1);
});