BREAKING CHANGE: moved yaml serialization from Engine.Serialization to Engine.Integration
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
|
||||
using Engine.Core;
|
||||
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serializers.Yaml;
|
||||
|
||||
public class TriangleConverter : EngineTypeYamlSerializerBase<Triangle>
|
||||
{
|
||||
public override Triangle Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
parser.Consume<MappingStart>();
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Triangle.A)) != 0)
|
||||
throw new ArgumentException($"{nameof(Triangle)} mapping must start with {nameof(Triangle.A)}");
|
||||
Vector2D a = (Vector2D)rootDeserializer(typeof(Vector2D))!;
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Triangle.B)) != 0)
|
||||
throw new ArgumentException($"{nameof(Triangle)} mapping must have {nameof(Triangle.B)} after {nameof(Triangle.A)}");
|
||||
Vector2D b = (Vector2D)rootDeserializer(typeof(Vector2D))!;
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Triangle.C)) != 0)
|
||||
throw new ArgumentException($"{nameof(Triangle)} mapping must end with {nameof(Triangle.C)}");
|
||||
Vector2D c = (Vector2D)rootDeserializer(typeof(Vector2D))!;
|
||||
|
||||
parser.Consume<MappingEnd>();
|
||||
|
||||
return new Triangle(a, b, c);
|
||||
}
|
||||
|
||||
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
|
||||
{
|
||||
Triangle aabb = (Triangle)value!;
|
||||
|
||||
emitter.Emit(new MappingStart());
|
||||
emitter.Emit(new Scalar(nameof(Triangle.A)));
|
||||
serializer(aabb.A, typeof(Vector2D));
|
||||
emitter.Emit(new Scalar(nameof(Triangle.B)));
|
||||
serializer(aabb.B, typeof(Vector2D));
|
||||
emitter.Emit(new Scalar(nameof(Triangle.C)));
|
||||
serializer(aabb.C, typeof(Vector2D));
|
||||
emitter.Emit(new MappingEnd());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user