Запись 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
+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>