diff --git a/src/Meziantou.Framework.HumanReadableSerializer/ConverterListExtensions.cs b/src/Meziantou.Framework.HumanReadableSerializer/ConverterListExtensions.cs new file mode 100644 index 000000000..4cb403e1d --- /dev/null +++ b/src/Meziantou.Framework.HumanReadableSerializer/ConverterListExtensions.cs @@ -0,0 +1,26 @@ +namespace Meziantou.Framework.HumanReadable; +public static class ConverterListExtensions +{ + public static void Add(this IList converters, Func convert) + { + converters.Add(new FuncConverter((value, options) => convert(value))); + } + + public static void Add(this IList converters, Func convert) + { + converters.Add(new FuncConverter(convert)); + } + + private sealed class FuncConverter : HumanReadableConverter + { + private readonly Func _converter; + + public FuncConverter(Func converter) => _converter = converter; + + protected override void WriteValue(HumanReadableTextWriter writer, T? value, HumanReadableSerializerOptions options) + { + var str = _converter(value, options); + writer.WriteValue(str); + } + } +} diff --git a/src/Meziantou.Framework.HumanReadableSerializer/Meziantou.Framework.HumanReadableSerializer.csproj b/src/Meziantou.Framework.HumanReadableSerializer/Meziantou.Framework.HumanReadableSerializer.csproj index caa18b568..3c22c2aff 100644 --- a/src/Meziantou.Framework.HumanReadableSerializer/Meziantou.Framework.HumanReadableSerializer.csproj +++ b/src/Meziantou.Framework.HumanReadableSerializer/Meziantou.Framework.HumanReadableSerializer.csproj @@ -6,7 +6,7 @@ enable enable One-way serializer to a human readable format - 1.0.16 + 1.0.17 true diff --git a/src/Meziantou.Framework.InlineSnapshotTesting/Meziantou.Framework.InlineSnapshotTesting.csproj b/src/Meziantou.Framework.InlineSnapshotTesting/Meziantou.Framework.InlineSnapshotTesting.csproj index fb877774d..3330032ed 100644 --- a/src/Meziantou.Framework.InlineSnapshotTesting/Meziantou.Framework.InlineSnapshotTesting.csproj +++ b/src/Meziantou.Framework.InlineSnapshotTesting/Meziantou.Framework.InlineSnapshotTesting.csproj @@ -7,7 +7,7 @@ Enables verification of objects using inline snapshots $(DefineConstants);DEBUG_TaskDialogPrompt - 1.0.25 + 1.0.26 $(NoWarn);NU5100 diff --git a/tests/Meziantou.Framework.HumanReadableSerializer.Tests/SerializerTests.cs b/tests/Meziantou.Framework.HumanReadableSerializer.Tests/SerializerTests.cs index c5fafc425..4ff8a4d52 100644 --- a/tests/Meziantou.Framework.HumanReadableSerializer.Tests/SerializerTests.cs +++ b/tests/Meziantou.Framework.HumanReadableSerializer.Tests/SerializerTests.cs @@ -930,7 +930,7 @@ public void SerializeObjectGraph() [Fact] public void Double() => AssertSerialization(-5.30d, "-5.3"); - + [Fact] public void Double_NaN() => AssertSerialization(double.NaN, "NaN"); @@ -1037,7 +1037,7 @@ public void StringWriter() [Fact] public void MethodInfo_dynamic_ValueTuple_Parameter() => AssertSerialization(typeof(Methods).GetMethod(nameof(Methods.ValueTupleDynamic)), "Meziantou.Framework.HumanReadable.Tests.SerializerTests+Methods.ValueTupleDynamic((dynamic, System.Int32) value)"); - + [Fact] public void MethodInfo_dynamic_Nested_ValueTuple_Parameter() => AssertSerialization(typeof(Methods).GetMethod(nameof(Methods.ValueTupleNestedDynamic)), "Meziantou.Framework.HumanReadable.Tests.SerializerTests+Methods.ValueTupleNestedDynamic((dynamic A, (System.Int32 B, dynamic C) D) value)"); @@ -2161,6 +2161,14 @@ public void IgnoreMemberWithType() }); } + [Fact] + public void FuncConverter() + { + var options = new HumanReadableSerializerOptions(); + options.Converters.Add(value => value.ToString("G17", CultureInfo.InvariantCulture)); + AssertSerialization(-5.30d, options, "-5.2999999999999998"); + } + private sealed class PropThrowAnException { public int A => throw new NotSupportedException();