42 lines
1.5 KiB
C#
42 lines
1.5 KiB
C#
using System;
|
|
|
|
using Syntriax.Engine.Core;
|
|
|
|
using YamlDotNet.Core;
|
|
using YamlDotNet.Core.Events;
|
|
using YamlDotNet.Serialization;
|
|
|
|
namespace Syntriax.Engine.Serializers.Yaml;
|
|
|
|
public class Line2DEquationConverter : EngineTypeYamlSerializerBase<Line2DEquation>
|
|
{
|
|
public override Line2DEquation Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
|
{
|
|
parser.Consume<MappingStart>();
|
|
|
|
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Line2DEquation.Slope)) != 0)
|
|
throw new ArgumentException($"{nameof(Line2DEquation)} mapping must start with {nameof(Line2DEquation.Slope)}");
|
|
float slope = (float)rootDeserializer(typeof(float))!;
|
|
|
|
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Line2DEquation.OffsetY)) != 0)
|
|
throw new ArgumentException($"{nameof(Line2DEquation)} mapping must end with {nameof(Line2DEquation.OffsetY)}");
|
|
float offset = (float)rootDeserializer(typeof(float))!;
|
|
|
|
parser.Consume<MappingEnd>();
|
|
|
|
return new Line2DEquation(slope, offset);
|
|
}
|
|
|
|
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
|
|
{
|
|
Line2DEquation line2DEquation = (Line2DEquation)value!;
|
|
|
|
emitter.Emit(new MappingStart());
|
|
emitter.Emit(new Scalar(nameof(Line2DEquation.Slope)));
|
|
serializer(line2DEquation.Slope, typeof(float));
|
|
emitter.Emit(new Scalar(nameof(Line2DEquation.OffsetY)));
|
|
serializer(line2DEquation.OffsetY, typeof(float));
|
|
emitter.Emit(new MappingEnd());
|
|
}
|
|
}
|