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