42 lines
1.5 KiB
C#
42 lines
1.5 KiB
C#
using System;
|
|
|
|
using YamlDotNet.Core;
|
|
using YamlDotNet.Core.Events;
|
|
using YamlDotNet.Serialization;
|
|
|
|
namespace Syntriax.Engine.Core.Serialization;
|
|
|
|
public class Projection1DSerializer : IEngineTypeYamlSerializer
|
|
{
|
|
public bool Accepts(Type type) => type == typeof(Projection1D);
|
|
|
|
public object? ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
|
{
|
|
parser.Consume<MappingStart>();
|
|
|
|
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Projection1D.Min)) != 0)
|
|
throw new ArgumentException($"{nameof(Projection1D)} mapping must start with {nameof(Projection1D.Min)}");
|
|
float min = (float)rootDeserializer(typeof(float))!;
|
|
|
|
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Projection1D.Max)) != 0)
|
|
throw new ArgumentException($"{nameof(Projection1D)} mapping must end with {nameof(Projection1D.Max)}");
|
|
float max = (float)rootDeserializer(typeof(float))!;
|
|
|
|
parser.Consume<MappingEnd>();
|
|
|
|
return new Projection1D(min, max);
|
|
}
|
|
|
|
public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
|
|
{
|
|
Projection1D projection1D = (Projection1D)value!;
|
|
|
|
emitter.Emit(new MappingStart());
|
|
emitter.Emit(new Scalar(nameof(Projection1D.Min)));
|
|
serializer(projection1D.Min, typeof(float));
|
|
emitter.Emit(new Scalar(nameof(Projection1D.Max)));
|
|
serializer(projection1D.Max, typeof(float));
|
|
emitter.Emit(new MappingEnd());
|
|
}
|
|
}
|