Rewrite dictionary to if and using structs

This commit is contained in:
2025-07-10 10:25:40 +03:00
parent 9a5c9de3b0
commit a5a23354f9
7 changed files with 64 additions and 23 deletions
+37 -4
View File
@@ -14,10 +14,43 @@ namespace mROA.Cbor
{
public class CborSerializationToolkit : IContextualSerializationToolKit
{
private Dictionary<Type, IOrdinaryStructureParser> _parsers = new(){{typeof(NetworkMessageHeader), new NetworkMessageHeaderParser()}, {typeof(DefaultCallRequest), new DefaultCallRequestParser()}, {typeof(FinalCommandExecution<object>), new FinalCommandExecutionParser()}, {typeof(FinalCommandExecution), new FinalCommandExecutionResultlessParser()}}; //
private readonly IOrdinaryStructureParser[] _parsers = {
new NetworkMessageHeaderParser(), new DefaultCallRequestParser(), new FinalCommandExecutionParser(),
new FinalCommandExecutionResultlessParser()
};
// private Dictionary<Type, IOrdinaryStructureParser> _parsers = new(){{typeof(NetworkMessageHeader), new NetworkMessageHeaderParser()}, {typeof(DefaultCallRequest), new DefaultCallRequestParser()}, {typeof(FinalCommandExecution<object>), new FinalCommandExecutionParser()}, {typeof(FinalCommandExecution), new FinalCommandExecutionResultlessParser()}}; //
private Dictionary<Type, List<PropertyInfo>> _propertiesCache = new();
public static TimeSpan SerializationTime = TimeSpan.Zero;
private HashSet<int> a;
private bool FindParser(Type t, out IOrdinaryStructureParser parser)
{
if (t == typeof(NetworkMessageHeader))
{
parser = _parsers[0];
return true;
}
if (t == typeof(DefaultCallRequest))
{
parser = _parsers[1];
return true;
}
if (t == typeof(FinalCommandExecution<object>))
{
parser = _parsers[2];
return true;
}
if (t == typeof(FinalCommandExecution))
{
parser = _parsers[3];
return true;
}
parser = null;
return false;
}
public byte[] Serialize(object objectToSerialize, IEndPointContext context)
{
var sw = Stopwatch.StartNew();
@@ -128,7 +161,7 @@ namespace mROA.Cbor
public void WriteData(object? obj, CborWriter writer, IEndPointContext? context)
{
if (obj is not null && _parsers.TryGetValue(obj.GetType(), out var parser))
if (obj is not null && FindParser(obj.GetType(), out var parser))
{
parser.Write(writer, obj, context, this);
return;
@@ -251,7 +284,7 @@ namespace mROA.Cbor
public object? ReadData(CborReader reader, Type? type, IEndPointContext? context)
{
if (type is not null && _parsers.TryGetValue(type, out var parser))
if (type is not null && FindParser(type, out var parser))
{
return parser.Read(reader, context, this);
}
+5 -5
View File
@@ -17,7 +17,7 @@ namespace mROA.Cbor
{
public void Write(CborWriter writer, object value, IEndPointContext context, CborSerializationToolkit serialization)
{
var v = value as NetworkMessageHeader;
var v = (NetworkMessageHeader)value;
writer.WriteStartArray(3);
writer.WriteByteString(v.Id.ToByteArray());
writer.WriteInt32((int)v.MessageType);
@@ -91,10 +91,10 @@ namespace mROA.Cbor
{
public void Write(CborWriter writer, object value, IEndPointContext context, CborSerializationToolkit serialization)
{
var v = value as FinalCommandExecution<object>;
var v = (FinalCommandExecution<object>)value;
writer.WriteStartArray(2);
serialization.WriteData(v.Result, writer, context);
writer.WriteByteString(v.Id.ToByteArray());
serialization.WriteData(v.Result, writer, context);
writer.WriteEndArray();
}
@@ -103,8 +103,8 @@ namespace mROA.Cbor
reader.ReadStartArray();
var result = new FinalCommandExecution<object>
{
Id = new Guid(reader.ReadByteString()),
Result = serialization.ReadData(reader, typeof(object), context),
Id = new Guid(reader.ReadByteString())
};
reader.ReadEndArray();
return result;
@@ -115,7 +115,7 @@ namespace mROA.Cbor
{
public void Write(CborWriter writer, object value, IEndPointContext context, CborSerializationToolkit serialization)
{
var v = value as FinalCommandExecution;
var v = (FinalCommandExecution)value;
writer.WriteStartArray(1);
writer.WriteByteString(v.Id.ToByteArray());
writer.WriteEndArray();