mirror of
https://github.com/ElectronNET/Electron.NET.git
synced 2026-02-14 05:34:48 +00:00
57 lines
1.7 KiB
C#
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; }
|
|
}
|
|
}
|