Запись Cbor

This commit is contained in:
2025-02-24 12:48:31 +03:00
parent 79aaad9226
commit 14c74c5775
7 changed files with 199 additions and 4 deletions
+3 -4
View File
@@ -10,9 +10,8 @@ namespace mROA.Benchmark
{ {
static void Main(string[] args) static void Main(string[] args)
{ {
Console.WriteLine("Hello, World!"); // Console.WriteLine("Hello, World!");
var summary = BenchmarkRunner.Run<CollectionsSpeed>(); // var summary = BenchmarkRunner.Run<CollectionsSpeed>();
} }
} }
@@ -26,7 +25,7 @@ namespace mROA.Benchmark
public CollectionsSpeed() public CollectionsSpeed()
{ {
_array = Enumerable.Range(0, N).ToArray(); _array = Enumerable.Range(0, N).ToArray();
_immutable = [.._array]; // _immutable = [.._array];
} }
[Benchmark] [Benchmark]
+144
View File
@@ -0,0 +1,144 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Formats.Cbor;
using System.Linq;
using mROA.Abstract;
using mROA.Implementation;
namespace mROA.Cbor
{
public class CborSerializaitonToolkit : IContextualSerializationToolKit
{
public byte[] Serialize<T>(T objectToSerialize, IEndPointContext context)
{
return Serialize(objectToSerialize, typeof(T), context);
}
public byte[] Serialize(object objectToSerialize, Type type, IEndPointContext context)
{
}
public T Deserialize<T>(byte[] rawData, IEndPointContext context)
{
return (T)Deserialize(rawData, typeof(T), context);
}
public object? Deserialize(byte[] rawData, Type type, IEndPointContext context)
{
return Deserialize(rawData.AsSpan(), type, context);
}
public T Deserialize<T>(Span<byte> rawData, IEndPointContext context)
{
return (T)Deserialize(rawData, typeof(T), context);
}
public object? Deserialize(Span<byte> rawData, Type type, IEndPointContext context)
{
return null;
}
public T Cast<T>(object nonCasted, IEndPointContext context)
{
return (T)Cast(nonCasted, typeof(T), context);
}
public object Cast(object nonCasted, Type type, IEndPointContext context)
{
return null;
}
private void WriteData(object? obj, CborWriter writer)
{
switch (obj)
{
case int i:
writer.WriteInt32(i);
break;
case long l:
writer.WriteInt64(l);
break;
case float f:
writer.WriteSingle(f);
break;
case double d:
writer.WriteDouble(d);
break;
case decimal dec:
writer.WriteDecimal(dec);
break;
case bool b:
writer.WriteBoolean(b);
break;
case string s:
writer.WriteTextString(s);
break;
case null:
writer.WriteNull();
break;
case uint ui:
writer.WriteUInt32(ui);
break;
case ulong ul:
writer.WriteUInt64(ul);
break;
case DateTimeOffset dto:
writer.WriteDateTimeOffset(dto);
break;
case byte[] bytes:
writer.WriteByteString(bytes);
break;
case IDictionary dictionary:
WriteDictionary(dictionary, writer);
break;
case IEnumerable enumerable:
WriteEnumerable(enumerable, writer);
break;
case SharedObject sharedObject:
break;
default:
WriteObject(obj, writer);
break;
}
}
private void WriteEnumerable(IEnumerable enumerable, CborWriter writer)
{
List<object?> list = new List<object?>();
foreach (var element in enumerable)
list.Add(element);
writer.WriteStartArray(list.Count);
foreach (var element in list)
WriteData(element, writer);
writer.WriteEndArray();
}
private void WriteDictionary(IDictionary dictionary, CborWriter writer)
{
writer.WriteStartMap(dictionary.Count);
var keysEnumerator = dictionary.Keys.GetEnumerator();
var valuesEnumerator = dictionary.Values.GetEnumerator();
for (int i = 0; i < dictionary.Count; i++)
{
keysEnumerator.MoveNext();
valuesEnumerator.MoveNext();
WriteData(keysEnumerator.Current, writer);
WriteData(valuesEnumerator.Current, writer);
}
writer.WriteEndMap();
}
private void WriteObject(object obj, CborWriter writer)
{
var type = obj.GetType();
var properties = type.GetProperties();
var values = properties.Select(property => property.GetValue(obj));
WriteEnumerable(values, writer);
}
}
}
@@ -0,0 +1,17 @@
using System;
using mROA.Abstract;
namespace mROA.Cbor
{
public interface IContextualSerializationToolKit
{
byte[] Serialize<T>(T objectToSerialize, IEndPointContext context);
byte[] Serialize(object objectToSerialize, Type type, IEndPointContext context);
T Deserialize<T>(byte[] rawData, IEndPointContext context);
object? Deserialize(byte[] rawData, Type type, IEndPointContext context);
T Deserialize<T>(Span<byte> rawData, IEndPointContext context);
object? Deserialize(Span<byte> rawData, Type type, IEndPointContext context);
T Cast<T>(object nonCasted, IEndPointContext context);
object Cast(object nonCasted, Type type, IEndPointContext context);
}
}
+16
View File
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\mROA\mROA.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.Formats.Cbor" Version="9.0.2" />
</ItemGroup>
</Project>
+6
View File
@@ -19,6 +19,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.Frontend", "Example
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mROA.Benchmark", "mROA.Benchmark\mROA.Benchmark.csproj", "{6868F42B-E30D-4040-AD4A-BC2A2E76D03A}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mROA.Benchmark", "mROA.Benchmark\mROA.Benchmark.csproj", "{6868F42B-E30D-4040-AD4A-BC2A2E76D03A}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mROA.Cbor", "mROA.Cbor\mROA.Cbor.csproj", "{6211E4EA-13FD-4EB1-8E6A-C0173DD0784A}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@@ -53,6 +55,10 @@ Global
{6868F42B-E30D-4040-AD4A-BC2A2E76D03A}.Debug|Any CPU.Build.0 = Debug|Any CPU {6868F42B-E30D-4040-AD4A-BC2A2E76D03A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6868F42B-E30D-4040-AD4A-BC2A2E76D03A}.Release|Any CPU.ActiveCfg = Release|Any CPU {6868F42B-E30D-4040-AD4A-BC2A2E76D03A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6868F42B-E30D-4040-AD4A-BC2A2E76D03A}.Release|Any CPU.Build.0 = Release|Any CPU {6868F42B-E30D-4040-AD4A-BC2A2E76D03A}.Release|Any CPU.Build.0 = Release|Any CPU
{6211E4EA-13FD-4EB1-8E6A-C0173DD0784A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6211E4EA-13FD-4EB1-8E6A-C0173DD0784A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6211E4EA-13FD-4EB1-8E6A-C0173DD0784A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6211E4EA-13FD-4EB1-8E6A-C0173DD0784A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
+9
View File
@@ -0,0 +1,9 @@
namespace mROA.Abstract
{
public interface IEndPointContext
{
IContextRepository RealRepository { get; }
IContextRepository RemoteRepository { get; }
int HostId { get; }
}
}
+4
View File
@@ -32,6 +32,10 @@ namespace mROA.Implementation
} }
public class SharedObject : SharedObject<object>
{
}
public class SharedObject<T> where T : notnull public class SharedObject<T> where T : notnull
{ {
private IContextRepository GetDefaultContextRepository() => private IContextRepository GetDefaultContextRepository() =>