Syntriax.Engine/Engine.Serialization/YamlSerializer.cs

36 lines
1.8 KiB
C#

using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
namespace Engine.Serialization;
public class YamlSerializer : ISerializer
{
private readonly YamlDotNet.Serialization.ISerializer serializer = new SerializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
// .WithTypeConverter(new BehaviourYamlConverter())
// .WithTypeConverter(new BehaviourControllerYamlConverter())
// .WithTypeConverter(new BehaviourDTOListConverter())
// .WithTypeConverter(new GameManagerYamlConverter())
// .WithTypeConverter(new GameObjectYamlConverter())
// .WithTypeConverter(new GameObjectDTOListConverter())
// .WithTypeConverter(new StateEnableYamlConverter())
// .WithTypeConverter(new TransformYamlConverter())
.WithTypeConverter(new Vector2DYamlConverter())
.Build();
private readonly YamlDotNet.Serialization.IDeserializer deserializer = new DeserializerBuilder()
.WithNamingConvention(UnderscoredNamingConvention.Instance)
// .WithTypeConverter(new BehaviourYamlConverter())
// .WithTypeConverter(new BehaviourControllerYamlConverter())
// .WithTypeConverter(new BehaviourDTOListConverter())
// .WithTypeConverter(new GameManagerYamlConverter())
// .WithTypeConverter(new GameObjectYamlConverter())
// .WithTypeConverter(new GameObjectDTOListConverter())
// .WithTypeConverter(new StateEnableYamlConverter())
// .WithTypeConverter(new TransformYamlConverter())
.WithTypeConverter(new Vector2DYamlConverter())
.Build();
public string Serialize<T>(T @object) => serializer.Serialize(@object);
public T Deserialize<T>(string serializedString) => deserializer.Deserialize<T>(serializedString);
}