feat: type container added back for field/property serialization
This commit is contained in:
parent
fb363970fc
commit
d102c5471d
@ -0,0 +1,72 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
using Syntriax.Engine.Core.Factory;
|
||||
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Syntriax.Engine.Core.Serialization;
|
||||
|
||||
public class SerializedClassConverter : IEngineTypeYamlConverter
|
||||
{
|
||||
public bool Accepts(Type type) => type == typeof(SerializedClass);
|
||||
|
||||
public object? ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
SerializedClass serializedClass = new();
|
||||
|
||||
parser.Consume<MappingStart>();
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(SerializedClass.Type)) != 0)
|
||||
throw new();
|
||||
serializedClass.Type = parser.Consume<Scalar>().Value;
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(SerializedClass.Public)) != 0)
|
||||
throw new();
|
||||
Dictionary<string, TypeContainer> publicDictionary = (Dictionary<string, TypeContainer>)rootDeserializer(typeof(Dictionary<string, TypeContainer>))!;
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(SerializedClass.Private)) != 0)
|
||||
throw new();
|
||||
Dictionary<string, TypeContainer> privateDictionary = (Dictionary<string, TypeContainer>)rootDeserializer(typeof(Dictionary<string, TypeContainer>))!;
|
||||
|
||||
parser.Consume<MappingEnd>();
|
||||
|
||||
foreach ((string key, TypeContainer typeContainer) in publicDictionary)
|
||||
serializedClass.Public.Add(key, Serializer.Deserialize(typeContainer.Value!.ToString()!, TypeFactory.GetType(typeContainer.Type)));
|
||||
|
||||
foreach ((string key, TypeContainer typeContainer) in privateDictionary)
|
||||
serializedClass.Private.Add(key, Serializer.Deserialize(typeContainer.Value!.ToString()!, TypeFactory.GetType(typeContainer.Type)));
|
||||
|
||||
return serializedClass;
|
||||
}
|
||||
|
||||
public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
|
||||
{
|
||||
SerializedClass serializedClass = (SerializedClass)value!;
|
||||
|
||||
Dictionary<string, TypeContainer> publics = [];
|
||||
Dictionary<string, TypeContainer> privates = [];
|
||||
|
||||
foreach ((string key, object? @object) in serializedClass.Public.Where(v => !v.GetType().HasAttribute<IgnoreSerializationAttribute>()))
|
||||
publics.Add(key, new TypeContainer(@object));
|
||||
|
||||
foreach ((string key, object? @object) in serializedClass.Private.Where(v => !v.GetType().HasAttribute<IgnoreSerializationAttribute>()))
|
||||
privates.Add(key, new TypeContainer(@object));
|
||||
|
||||
emitter.Emit(new MappingStart());
|
||||
|
||||
emitter.Emit(new Scalar(nameof(SerializedClass.Type)));
|
||||
emitter.Emit(new Scalar(serializedClass.Type));
|
||||
|
||||
emitter.Emit(new Scalar(nameof(SerializedClass.Public)));
|
||||
serializer(publics);
|
||||
|
||||
emitter.Emit(new Scalar(nameof(SerializedClass.Private)));
|
||||
serializer(privates);
|
||||
|
||||
emitter.Emit(new MappingEnd());
|
||||
}
|
||||
}
|
13
Engine.Core/Serialization/Converters/TypeContainer.cs
Normal file
13
Engine.Core/Serialization/Converters/TypeContainer.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using System;
|
||||
|
||||
namespace Syntriax.Engine.Core.Serialization;
|
||||
|
||||
public class TypeContainer
|
||||
{
|
||||
public object? Value { get; set; } = string.Empty;
|
||||
public string Type { get; set; } = string.Empty;
|
||||
|
||||
public TypeContainer() { }
|
||||
public TypeContainer(Type type) { Type = type.FullName ?? string.Empty; }
|
||||
public TypeContainer(object? value) { Value = value; Type = value?.GetType().FullName ?? string.Empty; }
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
using System;
|
||||
|
||||
using Syntriax.Engine.Core.Factory;
|
||||
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Syntriax.Engine.Core.Serialization;
|
||||
|
||||
public class TypeContainerConverter : IEngineTypeYamlConverter
|
||||
{
|
||||
public bool Accepts(Type type) => type == typeof(TypeContainer);
|
||||
|
||||
public object? ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
parser.Consume<MappingStart>();
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(TypeContainer.Type)) != 0)
|
||||
throw new ArgumentException($"{nameof(TypeContainer)} mapping must start with {nameof(TypeContainer.Type)}");
|
||||
string typeFullName = parser.Consume<Scalar>().Value;
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(TypeContainer.Value)) != 0)
|
||||
throw new ArgumentException($"{nameof(TypeContainer)} mapping must end with {nameof(TypeContainer.Type)}");
|
||||
|
||||
object? value = rootDeserializer(TypeFactory.GetType(typeFullName));
|
||||
|
||||
parser.Consume<MappingEnd>();
|
||||
|
||||
return new TypeContainer() { Type = typeFullName, Value = value };
|
||||
}
|
||||
|
||||
public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
|
||||
{
|
||||
TypeContainer? typeContainer = (TypeContainer)value!;
|
||||
|
||||
emitter.Emit(new MappingStart());
|
||||
|
||||
emitter.Emit(new Scalar(nameof(TypeContainer.Type)));
|
||||
emitter.Emit(new Scalar(typeContainer.Type));
|
||||
|
||||
emitter.Emit(new Scalar(nameof(TypeContainer.Value)));
|
||||
serializer(typeContainer.Value, TypeFactory.GetType(typeContainer.Type));
|
||||
|
||||
emitter.Emit(new MappingEnd());
|
||||
}
|
||||
}
|
@ -51,6 +51,11 @@ public static class Serializer
|
||||
return deserializer.Deserialize(yaml)!;
|
||||
}
|
||||
|
||||
public static object Deserialize(string yaml, Type type)
|
||||
{
|
||||
return deserializer.Deserialize(yaml, type)!;
|
||||
}
|
||||
|
||||
public static T Deserialize<T>(string yaml)
|
||||
{
|
||||
return deserializer.Deserialize<T>(yaml);
|
||||
|
Loading…
x
Reference in New Issue
Block a user