mirror of
https://github.com/ElectronNET/Electron.NET.git
synced 2026-07-13 12:18:08 +00:00
Import SocketIO code
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace SocketIOClient.JsonSerializer
|
||||
{
|
||||
class ByteArrayConverter : JsonConverter<byte[]>
|
||||
{
|
||||
public ByteArrayConverter()
|
||||
{
|
||||
Bytes = new List<byte[]>();
|
||||
}
|
||||
|
||||
|
||||
public List<byte[]> Bytes { get; }
|
||||
|
||||
public override byte[] Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
byte[] bytes = null;
|
||||
if (reader.TokenType == JsonTokenType.StartObject)
|
||||
{
|
||||
reader.Read();
|
||||
if (reader.TokenType == JsonTokenType.PropertyName && reader.GetString() == "_placeholder")
|
||||
{
|
||||
reader.Read();
|
||||
if (reader.TokenType == JsonTokenType.True && reader.GetBoolean())
|
||||
{
|
||||
reader.Read();
|
||||
if (reader.TokenType == JsonTokenType.PropertyName && reader.GetString() == "num")
|
||||
{
|
||||
reader.Read();
|
||||
int num = reader.GetInt32();
|
||||
bytes = Bytes[num];
|
||||
reader.Read();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, byte[] value, JsonSerializerOptions options)
|
||||
{
|
||||
Bytes.Add(value);
|
||||
writer.WriteStartObject();
|
||||
writer.WritePropertyName("_placeholder");
|
||||
writer.WriteBooleanValue(true);
|
||||
writer.WritePropertyName("num");
|
||||
writer.WriteNumberValue(Bytes.Count - 1);
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
}
|
||||
}
|
||||
11
ElectronNET.API/SocketIO/JsonSerializer/IJsonSerializer.cs
Normal file
11
ElectronNET.API/SocketIO/JsonSerializer/IJsonSerializer.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SocketIOClient.JsonSerializer
|
||||
{
|
||||
public interface IJsonSerializer
|
||||
{
|
||||
JsonSerializeResult Serialize(object[] data);
|
||||
T Deserialize<T>(string json);
|
||||
T Deserialize<T>(string json, IList<byte[]> incomingBytes);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SocketIOClient.JsonSerializer
|
||||
{
|
||||
public class JsonSerializeResult
|
||||
{
|
||||
public string Json { get; set; }
|
||||
public IList<byte[]> Bytes { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace SocketIOClient.JsonSerializer
|
||||
{
|
||||
public class SystemTextJsonSerializer : IJsonSerializer
|
||||
{
|
||||
public JsonSerializeResult Serialize(object[] data)
|
||||
{
|
||||
var converter = new ByteArrayConverter();
|
||||
var options = GetOptions();
|
||||
options.Converters.Add(converter);
|
||||
string json = System.Text.Json.JsonSerializer.Serialize(data, options);
|
||||
return new JsonSerializeResult
|
||||
{
|
||||
Json = json,
|
||||
Bytes = converter.Bytes
|
||||
};
|
||||
}
|
||||
|
||||
public T Deserialize<T>(string json)
|
||||
{
|
||||
var options = GetOptions();
|
||||
return System.Text.Json.JsonSerializer.Deserialize<T>(json, options);
|
||||
}
|
||||
|
||||
public T Deserialize<T>(string json, IList<byte[]> bytes)
|
||||
{
|
||||
var options = GetOptions();
|
||||
var converter = new ByteArrayConverter();
|
||||
options.Converters.Add(converter);
|
||||
converter.Bytes.AddRange(bytes);
|
||||
return System.Text.Json.JsonSerializer.Deserialize<T>(json, options);
|
||||
}
|
||||
|
||||
private JsonSerializerOptions GetOptions()
|
||||
{
|
||||
JsonSerializerOptions options = null;
|
||||
if (OptionsProvider != null)
|
||||
{
|
||||
options = OptionsProvider();
|
||||
}
|
||||
if (options == null)
|
||||
{
|
||||
options = new JsonSerializerOptions();
|
||||
}
|
||||
return options;
|
||||
}
|
||||
|
||||
public Func<JsonSerializerOptions> OptionsProvider { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user