socket.io-client-csharp
socket.io-client-csharp copied to clipboard
JsonSerializer Support for On-Calls
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;
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);
});