Files
Electron.NET/ElectronNET.API/SocketIO/NewtonsoftJsonSerializer.cs
2022-07-14 16:53:54 +02:00

57 lines
1.7 KiB
C#

using System;
using Newtonsoft.Json;
using SocketIOClient.JsonSerializer;
using System.Collections.Generic;
namespace SocketIOClient.Newtonsoft.Json
{
public class NewtonsoftJsonSerializer : IJsonSerializer
{
public Func<JsonSerializerSettings> JsonSerializerOptions { get; }
public JsonSerializeResult Serialize(object[] data)
{
var converter = new ByteArrayConverter();
var settings = GetOptions();
settings.Converters.Add(converter);
string json = JsonConvert.SerializeObject(data, settings);
return new JsonSerializeResult
{
Json = json,
Bytes = converter.Bytes
};
}
public T Deserialize<T>(string json)
{
var settings = GetOptions();
return JsonConvert.DeserializeObject<T>(json, settings);
}
public T Deserialize<T>(string json, IList<byte[]> bytes)
{
var converter = new ByteArrayConverter();
converter.Bytes.AddRange(bytes);
var settings = GetOptions();
settings.Converters.Add(converter);
return JsonConvert.DeserializeObject<T>(json, settings);
}
private JsonSerializerSettings GetOptions()
{
JsonSerializerSettings options = null;
if (OptionsProvider != null)
{
options = OptionsProvider();
}
if (options == null)
{
options = new JsonSerializerSettings();
}
return options;
}
public Func<JsonSerializerSettings> OptionsProvider { get; set; }
}
}