| | | 1 | | |
| | | 2 | | using System.Numerics; |
| | | 3 | | using YamlDotNet.Core; |
| | | 4 | | using YamlDotNet.Core.Events; |
| | | 5 | | using YamlDotNet.Serialization; |
| | | 6 | | |
| | | 7 | | namespace Kestrun.Utilities.Yaml; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// YAML type converter for BigInteger |
| | | 11 | | /// </summary> |
| | | 12 | | public class BigIntegerTypeConverter : IYamlTypeConverter |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Check if the type is BigInteger |
| | | 16 | | /// </summary> |
| | | 17 | | /// <param name="type">The type to check</param> |
| | | 18 | | /// <returns>true if the type is BigInteger; otherwise, false.</returns> |
| | 106 | 19 | | public bool Accepts(Type type) => typeof(BigInteger).IsAssignableFrom(type); |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Read a BigInteger from YAML |
| | | 23 | | /// </summary> |
| | | 24 | | /// <param name="parser">The YAML parser</param> |
| | | 25 | | /// <param name="type">The type to deserialize to</param> |
| | | 26 | | /// <param name="rootDeserializer">The root deserializer</param> |
| | | 27 | | /// <returns>The deserialized BigInteger</returns> |
| | | 28 | | public object ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer) |
| | | 29 | | { |
| | 0 | 30 | | var value = parser.Consume<Scalar>().Value; |
| | 0 | 31 | | var bigNr = BigInteger.Parse(value); |
| | 0 | 32 | | return bigNr; |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Write a BigInteger to YAML |
| | | 37 | | /// </summary> |
| | | 38 | | /// <param name="emitter">The YAML emitter</param> |
| | | 39 | | /// <param name="value">The BigInteger value</param> |
| | | 40 | | /// <param name="type">The type of the value</param> |
| | | 41 | | /// <param name="serializer">The object serializer</param> |
| | | 42 | | public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer) |
| | | 43 | | { |
| | 1 | 44 | | if (value is null) |
| | | 45 | | { |
| | 0 | 46 | | emitter.Emit(new Scalar(AnchorName.Empty, TagName.Empty, "null", ScalarStyle.Plain, true, false)); |
| | 0 | 47 | | return; |
| | | 48 | | } |
| | | 49 | | |
| | 1 | 50 | | var bigNr = (BigInteger)value; |
| | 1 | 51 | | emitter.Emit(new Scalar(AnchorName.Empty, TagName.Empty, bigNr.ToString(), ScalarStyle.Plain, true, false)); |
| | 1 | 52 | | } |
| | | 53 | | } |