Syntriax.Engine/Engine.Serialization/YamlSerializer.cs

28 lines
1.3 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 Vector2DYamlConverter())
.WithTypeConverter(new TransformYamlConverter())
.WithTypeConverter(new BehaviourYamlConverter())
.WithTypeConverter(new GameObjectYamlConverter())
.WithTypeConverter(new StateEnableYamlConverter())
.Build();
private readonly YamlDotNet.Serialization.IDeserializer deserializer = new DeserializerBuilder()
.WithNamingConvention(UnderscoredNamingConvention.Instance)
.WithTypeConverter(new Vector2DYamlConverter())
.WithTypeConverter(new TransformYamlConverter())
.WithTypeConverter(new BehaviourYamlConverter())
.WithTypeConverter(new GameObjectYamlConverter())
.WithTypeConverter(new StateEnableYamlConverter())
.Build();
public string Serialize<T>(T @object) => serializer.Serialize(@object);
public T Deserialize<T>(string serializedString) => deserializer.Deserialize<T>(serializedString);
}