using System; using System.Collections.Generic; using YamlDotNet.Core; using YamlDotNet.Core.Events; using YamlDotNet.Serialization; namespace Syntriax.Engine.Core.Serialization; public class Shape2DSerializer : IEngineTypeYamlSerializer { public bool Accepts(Type type) => type == typeof(Shape2D); public object? ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer) { parser.Consume(); if (parser.Consume().Value.CompareTo(nameof(Shape2D.Vertices)) != 0) throw new ArgumentException($"{nameof(Shape2D)} mapping have a {nameof(Shape2D.Vertices)}"); parser.Consume(); List vertices = []; while (!parser.TryConsume(out _)) { Vector2D vertex = (Vector2D)rootDeserializer(typeof(Vector2D))!; vertices.Add(vertex); } parser.Consume(); return new Shape2D(vertices); } public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer) { Shape2D shape2D = (Shape2D)value!; emitter.Emit(new MappingStart()); emitter.Emit(new Scalar(nameof(Shape2D.Vertices))); emitter.Emit(new SequenceStart(anchor: null, tag: null, isImplicit: false, style: SequenceStyle.Block)); foreach (Vector2D vertex in shape2D.Vertices) serializer(vertex, typeof(Vector2D)); emitter.Emit(new SequenceEnd()); emitter.Emit(new MappingEnd()); } }