From db042a5201eecf85edd6edf4c120301a97fb566a Mon Sep 17 00:00:00 2001 From: Kostornoj-Dmitrij Date: Wed, 11 Dec 2024 18:54:40 +0500 Subject: [PATCH 01/13] =?UTF-8?q?=D0=91=D0=B0=D0=B7=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=BD=D0=B0=D1=81=D1=82=D1=80=D0=BE=D0=B9=D0=BA=D0=B0?= =?UTF-8?q?=20=D0=BF=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D0=B0,=20=D0=B8=D1=81?= =?UTF-8?q?=D0=BA=D0=BB=D1=8E=D1=87=D0=B8=D0=BB=20=D0=BF=D1=80=D0=B5=D0=B4?= =?UTF-8?q?=D0=BB=D0=BE=D0=B6=D0=B5=D0=BD=D0=BD=D1=8B=D0=B9=20=D1=82=D0=B5?= =?UTF-8?q?=D1=81=D1=82=D0=BE=D0=B2=D1=8B=D0=B9=20=D1=84=D0=B0=D0=B9=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs | 5 +++-- fluent-api.sln | 6 ++++++ fluent-api.sln.DotSettings | 3 +++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs index 4c8b2445..29008705 100644 --- a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs @@ -8,9 +8,10 @@ public class ObjectPrinterAcceptanceTests [Test] public void Demo() { + var person = new Person { Name = "Alex", Age = 19 }; - var printer = ObjectPrinter.For(); + //var printer = ObjectPrinter.For(); //1. Исключить из сериализации свойства определенного типа //2. Указать альтернативный способ сериализации для определенного типа //3. Для числовых типов указать культуру @@ -18,7 +19,7 @@ public void Demo() //5. Настроить обрезание строковых свойств (метод должен быть виден только для строковых свойств) //6. Исключить из сериализации конкретного свойства - string s1 = printer.PrintToString(person); + //string s1 = printer.PrintToString(person); //7. Синтаксический сахар в виде метода расширения, сериализующего по-умолчанию //8. ...с конфигурированием diff --git a/fluent-api.sln b/fluent-api.sln index 69c8db9e..848fd0b9 100644 --- a/fluent-api.sln +++ b/fluent-api.sln @@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentMapping.Tests", "Samp EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Spectacle", "Samples\Spectacle\Spectacle.csproj", "{EFA9335C-411B-4597-B0B6-5438D1AE04C3}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ObjectPrintingTests", "ObjectPrintingTests\ObjectPrintingTests.csproj", "{7E0941D5-44C7-4350-9CD6-5497B411A018}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -35,6 +37,10 @@ Global {EFA9335C-411B-4597-B0B6-5438D1AE04C3}.Debug|Any CPU.Build.0 = Debug|Any CPU {EFA9335C-411B-4597-B0B6-5438D1AE04C3}.Release|Any CPU.ActiveCfg = Release|Any CPU {EFA9335C-411B-4597-B0B6-5438D1AE04C3}.Release|Any CPU.Build.0 = Release|Any CPU + {7E0941D5-44C7-4350-9CD6-5497B411A018}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7E0941D5-44C7-4350-9CD6-5497B411A018}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7E0941D5-44C7-4350-9CD6-5497B411A018}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7E0941D5-44C7-4350-9CD6-5497B411A018}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/fluent-api.sln.DotSettings b/fluent-api.sln.DotSettings index 135b83ec..53fe49b2 100644 --- a/fluent-api.sln.DotSettings +++ b/fluent-api.sln.DotSettings @@ -1,6 +1,9 @@  <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb" /> + <Policy><Descriptor Staticness="Instance" AccessRightKinds="Private" Description="Instance fields (private)"><ElementKinds><Kind Name="FIELD" /><Kind Name="READONLY_FIELD" /></ElementKinds></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="aaBb" /></Policy> + <Policy><Descriptor Staticness="Any" AccessRightKinds="Any" Description="Types and namespaces"><ElementKinds><Kind Name="NAMESPACE" /><Kind Name="CLASS" /><Kind Name="STRUCT" /><Kind Name="ENUM" /><Kind Name="DELEGATE" /></ElementKinds></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="AaBb_AaBb" /></Policy> + True True True Imported 10.10.2016 From 01a6a67499dd09b7a0769fa1e54b84fd430f9bfc Mon Sep 17 00:00:00 2001 From: Kostornoj-Dmitrij Date: Wed, 11 Dec 2024 18:55:11 +0500 Subject: [PATCH 02/13] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BF=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D0=B0=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20=D1=82=D0=B5=D1=81=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ObjectPrintingTests.csproj | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 ObjectPrintingTests/ObjectPrintingTests.csproj diff --git a/ObjectPrintingTests/ObjectPrintingTests.csproj b/ObjectPrintingTests/ObjectPrintingTests.csproj new file mode 100644 index 00000000..242935b9 --- /dev/null +++ b/ObjectPrintingTests/ObjectPrintingTests.csproj @@ -0,0 +1,78 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + ObjectPrinter_Should + + + ObjectPrinter_Should + + + ObjectPrinter_Should + + + ObjectPrinter_Should + + + ObjectPrinter_Should + + + ObjectPrinter_Should + + + ObjectPrinter_Should + + + ObjectPrinter_Should + + + ObjectPrinter_Should + + + ObjectPrinter_Should + + + ObjectPrinter_Should + + + ObjectPrinter_Should + + + ObjectPrinter_Should + + + ObjectPrinter_Should + + + ObjectPrinter_Should + + + ObjectPrinter_Should + + + + From d29703d6fcd7d0b738f2e3cab48d5691f130cb92 Mon Sep 17 00:00:00 2001 From: Kostornoj-Dmitrij Date: Wed, 11 Dec 2024 18:55:43 +0500 Subject: [PATCH 03/13] =?UTF-8?q?=D0=A2=D0=B5=D1=81=D1=82=D1=8B=20+=20?= =?UTF-8?q?=D0=B2=D1=81=D0=BF=D0=BE=D0=BC=D0=BE=D0=B3=D0=B0=D1=82=D0=B5?= =?UTF-8?q?=D0=BB=D1=8C=D0=BD=D1=8B=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81?= =?UTF-8?q?=D1=8B=20=D0=B4=D0=BB=D1=8F=20=D0=BD=D0=B8=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ObjectPrintingTests/FaultyType.cs | 6 + ObjectPrintingTests/NestedObject.cs | 7 + ObjectPrintingTests/ObjectPrinter_Should.cs | 227 ++++++++++++++++++++ ObjectPrintingTests/PersonWithRoots.cs | 11 + ObjectPrintingTests/SimplePerson.cs | 9 + 5 files changed, 260 insertions(+) create mode 100644 ObjectPrintingTests/FaultyType.cs create mode 100644 ObjectPrintingTests/NestedObject.cs create mode 100644 ObjectPrintingTests/ObjectPrinter_Should.cs create mode 100644 ObjectPrintingTests/PersonWithRoots.cs create mode 100644 ObjectPrintingTests/SimplePerson.cs diff --git a/ObjectPrintingTests/FaultyType.cs b/ObjectPrintingTests/FaultyType.cs new file mode 100644 index 00000000..867c63cc --- /dev/null +++ b/ObjectPrintingTests/FaultyType.cs @@ -0,0 +1,6 @@ +namespace ObjectPrintingTests; + +public class FaultyType +{ + public override string ToString() => throw new Exception("Serialization error!"); +} \ No newline at end of file diff --git a/ObjectPrintingTests/NestedObject.cs b/ObjectPrintingTests/NestedObject.cs new file mode 100644 index 00000000..5f0cd21f --- /dev/null +++ b/ObjectPrintingTests/NestedObject.cs @@ -0,0 +1,7 @@ +namespace ObjectPrintingTests; + +public class NestedObject +{ + public string? Name { get; set; } + public NestedObject? Child { get; set; } +} \ No newline at end of file diff --git a/ObjectPrintingTests/ObjectPrinter_Should.cs b/ObjectPrintingTests/ObjectPrinter_Should.cs new file mode 100644 index 00000000..8a7a945e --- /dev/null +++ b/ObjectPrintingTests/ObjectPrinter_Should.cs @@ -0,0 +1,227 @@ +using System.Globalization; +using ObjectPrinting; +using ObjectPrinting.Extensions; + +namespace ObjectPrintingTests; + +[TestFixture] +public class ObjectPrinter_Should +{ + private ObjectPrinter printer; + private static readonly VerifySettings Settings = new(); + + [SetUp] + public void SetUp() + { + printer = new ObjectPrinter(); + Settings.UseDirectory("TestResults"); + } + + [Test] + public Task ObjectPrinter_ShouldHandle_WhenSerialize() + { + var person = new SimplePerson { Name = "Dima", Age = 20 }; + var printedString = printer.Serialize(person); + return Verify(printedString, Settings); + } + + [Test] + public Task ObjectPrinter_ShouldHandle_WhenExcludeProperty() + { + var person = new SimplePerson { Name = "Dima", Age = 20 }; + var printedString = printer.Serialize(person, + c => c.Exclude(x => x.Age)); + return Verify(printedString, Settings); + } + + [Test] + public Task ObjectPrinter_ShouldHandle_WhenExcludeType() + { + var person = new SimplePerson { Name = "Dima", Age = 20 }; + var printedString = printer.Serialize(person, + c => c.Exclude()); + return Verify(printedString, Settings); + } + + [Test] + public Task ObjectPrinter_ShouldHandle_WhenCustomPropertySerialization() + { + var person = new SimplePerson { Name = "Dima", Age = 20 }; + var printedString = printer.Serialize(person, + c => c.SetCustomSerialization(x => x.Name, _ => "Name")); + return Verify(printedString, Settings); + } + + [Test] + public Task ObjectPrinter_ShouldHandle_WhenCustomTypeSerialization() + { + var person = new SimplePerson { Name = "Dima", Age = 20 }; + var printedString = printer.Serialize(person, + c => c.SetCustomSerialization(_ => "Age")); + return Verify(printedString, Settings); + } + + [TestCase("ru-RU", ",")] + [TestCase("en-US", ".")] + public Task ObjectPrinter_ShouldHandle_WhenSetCultureForNumber(string culture, string expectedSeparator) + { + var person = new SimplePerson { Name = "Dima", Age = 20, Height = 180.5 }; + var printedString = printer.Serialize(person, + c => c.SetCulture(new CultureInfo(culture))); + return Verify(printedString, Settings); + } + + [TestCase("ru-RU")] + [TestCase("en-US")] + public Task ObjectPrinter_ShouldFormatDateTime_WhenSetCultureForDate(string culture) + { + var person = new DateTime(2024,12,11); + var printedString = printer.Serialize(person, + c => c.SetCulture(new CultureInfo(culture), "G")); + return Verify(printedString, Settings); + } + + [Test] + public Task ObjectPrinter_ShouldHandle_WhenTrimString() + { + var person = new SimplePerson { Name = "Dima", Age = 20 }; + var printedString = printer.Serialize(person, + c => c.TrimStringsToLength(2)); + return Verify(printedString, Settings); + } + + [Test] + public Task ObjectPrinter_ShouldHandle_WhenLengthIsGreaterThanTrimmed() + { + var person = new SimplePerson { Name = "Dima", Age = 20 }; + var printedString = printer.Serialize(person, + c => c.TrimStringsToLength(10)); + return Verify(printedString, Settings); + } + + [Test] + public Task ObjectPrinter_ShouldHandle_WhenLargeCollectionWithTrim() + { + var largeCollection = Enumerable.Range(1, 1000).ToList(); + var printedString = printer.Serialize(largeCollection, + c => c.TrimStringsToLength(50)); + return Verify(printedString, Settings); + } + + [Test] + public Task ObjectPrinter_ShouldHandle_WhenCircularReference() + { + var person = new PersonWithRoots { Name = "Dima", Age = 20 }; + person.Root = person; + var printedString = printer.Serialize(person); + return Verify(printedString, Settings); + } + + [Test] + public Task ObjectPrinter_ShouldHandle_WhenArray() + { + var first = new SimplePerson { Name = "Dima", Age = 20 }; + var second = new SimplePerson { Name = "Andrey", Height = 190, Age = 45 }; + + SimplePerson[] persons = [first, second]; + var printedString = printer.Serialize(persons); + return Verify(printedString, Settings); + } + + [Test] + public Task ObjectPrinter_ShouldHandle_WhenList() + { + List personList = + [ + new() { Name = "Dima", Age = 20 }, + new() { Name = "Andrey", Age = 45 }, + new() { Name = "Vadim", Age = 10 } + ]; + + var printedString = printer.Serialize(personList); + return Verify(printedString, Settings); + } + + [Test] + public Task ObjectPrinter_ShouldHandle_WhenNestedArray() + { + SimplePerson[] personArray = + [ + new() { Name = "Dima", Age = 20 }, + new() { Name = "Andrey", Age = 45 }, + new() { Name = "Vadim", Age = 10 } + ]; + + var printedString = printer.Serialize(personArray); + return Verify(printedString, Settings); + } + + [Test] + public Task ObjectPrinter_ShouldHandle_WhenDictionary() + { + Dictionary personDictionary = new() + { + { new SimplePerson { Name = "Dima", Age = 20 }, "first"}, + { new SimplePerson { Name = "Andrey", Age = 45 }, "second"}, + { new SimplePerson { Name = "Vadim", Age = 10 }, "third"} + }; + + var printedString = printer.Serialize(personDictionary); + return Verify(printedString, Settings); + } + + [Test] + public Task ObjectPrinter_ShouldHandle_WhenCustomCollectionSerialization() + { + var collection = new List { 1, 2, 3 }; + var printedString = printer.Serialize(collection, + c => c.SetCustomSerialization>(list => string.Join(", ", list))); + return Verify(printedString, Settings); + } + + [Test] + public Task ObjectPrinter_ShouldHandle_WhenDeepNesting() + { + var nested = new NestedObject + { + Name = "Root", + Child = new NestedObject + { + Name = "Child", + Child = new NestedObject { Name = "GrandChild" } + } + }; + + var printedString = printer.Serialize(nested); + return Verify(printedString, Settings); + } + + [Test] + public Task ObjectPrinter_ShouldHandle_WhenFaultyItemInCollection() + { + var collection = new List + { + new SimplePerson { Name = "Dima", Age = 20 }, + new FaultyType(), + new SimplePerson { Name = "Andrey", Age = 45 } + }; + + var printedString = printer.Serialize(collection); + return Verify(printedString, Settings); + } + + [Test] + public Task ObjectPrinter_ShouldHandle_WhenExcessiveNesting() + { + var deeplyNested = new NestedObject { Name = "Level 0" }; + var current = deeplyNested; + for (var i = 1; i <= 100; i++) + { + current.Child = new NestedObject { Name = $"Level {i}" }; + current = current.Child; + } + + var printedString = printer.Serialize(deeplyNested); + return Verify(printedString, Settings); + } +} \ No newline at end of file diff --git a/ObjectPrintingTests/PersonWithRoots.cs b/ObjectPrintingTests/PersonWithRoots.cs new file mode 100644 index 00000000..e9550c5c --- /dev/null +++ b/ObjectPrintingTests/PersonWithRoots.cs @@ -0,0 +1,11 @@ +namespace ObjectPrintingTests; + +public class PersonWithRoots +{ + public Guid Id { get; set; } + public string? Name { get; set; } + public double Height { get; set; } + public int Age { get; set; } + public PersonWithRoots? Root { get; set; } + public List? Childrens { get; set; } +} \ No newline at end of file diff --git a/ObjectPrintingTests/SimplePerson.cs b/ObjectPrintingTests/SimplePerson.cs new file mode 100644 index 00000000..7c4c388a --- /dev/null +++ b/ObjectPrintingTests/SimplePerson.cs @@ -0,0 +1,9 @@ +namespace ObjectPrintingTests; + +public class SimplePerson +{ + public Guid Id { get; set; } + public string? Name { get; init; } + public double Height { get; set; } + public int Age { get; init; } +} \ No newline at end of file From a8e70c9efcdaed2c5bf96bde6b23485ca1695401 Mon Sep 17 00:00:00 2001 From: Kostornoj-Dmitrij Date: Wed, 11 Dec 2024 18:56:44 +0500 Subject: [PATCH 04/13] =?UTF-8?q?=D0=A0=D0=B0=D1=81=D1=88=D0=B8=D1=80?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B8=20=D0=BE=D1=81=D0=BD=D0=BE?= =?UTF-8?q?=D0=B2=D0=BD=D0=BE=D0=B9=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20ObjectPrinter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Extensions/ObjectPrinterExtensions.cs | 14 ++++++++++++++ ObjectPrinting/Interfaces/IObjectPrinter.cs | 6 ++++++ ObjectPrinting/ObjectPrinter.cs | 13 +++++++------ 3 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 ObjectPrinting/Extensions/ObjectPrinterExtensions.cs create mode 100644 ObjectPrinting/Interfaces/IObjectPrinter.cs diff --git a/ObjectPrinting/Extensions/ObjectPrinterExtensions.cs b/ObjectPrinting/Extensions/ObjectPrinterExtensions.cs new file mode 100644 index 00000000..b7bccbb3 --- /dev/null +++ b/ObjectPrinting/Extensions/ObjectPrinterExtensions.cs @@ -0,0 +1,14 @@ +using System; +using ObjectPrinting.Interfaces; + +namespace ObjectPrinting.Extensions; + +public static class ObjectPrinterExtensions +{ + public static string? Serialize(this IObjectPrinter printer, TSerialize obj) => + printer.For().PrintToString(obj); + + public static string? Serialize(this IObjectPrinter printer, TSerialize obj, + Func, ObjectPrinterSettings> config) => + config(printer.For()).PrintToString(obj); +} \ No newline at end of file diff --git a/ObjectPrinting/Interfaces/IObjectPrinter.cs b/ObjectPrinting/Interfaces/IObjectPrinter.cs new file mode 100644 index 00000000..34d0809c --- /dev/null +++ b/ObjectPrinting/Interfaces/IObjectPrinter.cs @@ -0,0 +1,6 @@ +namespace ObjectPrinting.Interfaces; + +public interface IObjectPrinter +{ + ObjectPrinterSettings For(); +} \ No newline at end of file diff --git a/ObjectPrinting/ObjectPrinter.cs b/ObjectPrinting/ObjectPrinter.cs index 3c7867c3..a6b44ba7 100644 --- a/ObjectPrinting/ObjectPrinter.cs +++ b/ObjectPrinting/ObjectPrinter.cs @@ -1,10 +1,11 @@ -namespace ObjectPrinting +using ObjectPrinting.Interfaces; + +namespace ObjectPrinting; + +public class ObjectPrinter : IObjectPrinter { - public class ObjectPrinter + public ObjectPrinterSettings For() { - public static PrintingConfig For() - { - return new PrintingConfig(); - } + return new ObjectPrinterSettings(); } } \ No newline at end of file From a6a6328495c1ff355c42fbfb6f08dacdffcd3804 Mon Sep 17 00:00:00 2001 From: Kostornoj-Dmitrij Date: Wed, 11 Dec 2024 18:57:14 +0500 Subject: [PATCH 05/13] =?UTF-8?q?=D0=9E=D1=81=D0=BD=D0=BE=D0=B2=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D0=B0=20=D1=81?= =?UTF-8?q?=D0=B5=D1=80=D0=B8=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ObjectPrinting/ObjectPrinterSettings.cs | 194 ++++++++++++++++++++++++ ObjectPrinting/PrintingConfig.cs | 41 ----- 2 files changed, 194 insertions(+), 41 deletions(-) create mode 100644 ObjectPrinting/ObjectPrinterSettings.cs delete mode 100644 ObjectPrinting/PrintingConfig.cs diff --git a/ObjectPrinting/ObjectPrinterSettings.cs b/ObjectPrinting/ObjectPrinterSettings.cs new file mode 100644 index 00000000..ef4c7ed0 --- /dev/null +++ b/ObjectPrinting/ObjectPrinterSettings.cs @@ -0,0 +1,194 @@ +using System; +using System.Text; +using System.Reflection; +using System.Collections; +using System.Linq.Expressions; +using System.Collections.Generic; + +namespace ObjectPrinting; + +public class ObjectPrinterSettings +{ + private readonly HashSet finalTypes = + [ + typeof(bool), typeof(byte), typeof(int), typeof(double), typeof(float), typeof(char), typeof(string), + typeof(DateTime), typeof(TimeSpan), typeof(Guid) + ]; + + private readonly HashSet excludedTypes = []; + private readonly HashSet excludedProperties = []; + private readonly HashSet processedObjects = new(ReferenceEqualityComparer.Instance); + private readonly Dictionary typeCulture = []; + private readonly Dictionary> typeSerializers = []; + private readonly Dictionary> propertySerializers = []; + private int? maxStringLength; + private const int MaxCollectionItems = 100; + + public ObjectPrinterSettings Exclude() + { + excludedTypes.Add(typeof(T)); + return this; + } + + public ObjectPrinterSettings Exclude(Expression> propertyExpression) + { + var propertyName = GetPropertyInfo(propertyExpression); + excludedProperties.Add(propertyName); + return this; + } + + public ObjectPrinterSettings SetCustomSerialization(Func serializer) + { + typeSerializers[typeof(T)] = obj => serializer((T)obj); + return this; + } + + public ObjectPrinterSettings SetCustomSerialization(Expression> propertyExpression, Func serializer) + { + var propertyName = GetPropertyInfo(propertyExpression); + propertySerializers[propertyName] = obj => serializer((T)obj); + return this; + } + + public ObjectPrinterSettings SetCulture(IFormatProvider culture, string? format = null) where T : IFormattable + { + typeCulture[typeof(T)] = culture; + if (!string.IsNullOrEmpty(format)) + typeSerializers[typeof(T)] = obj => ((IFormattable)obj).ToString(format, culture); + return this; + } + + public ObjectPrinterSettings TrimStringsToLength(int maxLength) + { + maxStringLength = maxLength; + return this; + } + + public string? PrintToString(TOwner obj) => PrintToString(obj, 0); + + private string? PrintToString(object? obj, int nestingLevel) + { + if (obj == null) + return AppendNewLine("null"); + + if (processedObjects.Contains(obj)) + return AppendNewLine("[Circular Reference]"); + + if (TrySerializeFinalType(obj, out var result)) + return result; + + return TrySerializeCollection(obj, nestingLevel, out var collectionResult) ? collectionResult : SerializeComplexType(obj, nestingLevel); + } + + private bool TrySerializeCollection(object obj, int nestingLevel, out string? collectionResult) + { + if (obj is IEnumerable enumerable) + { + var text = new StringBuilder(); + var pad = new string('\t', nestingLevel + 1); + var count = 0; + + text.AppendLine($"{obj.GetType().Name}:"); + foreach (var item in enumerable) + { + if (count++ >= MaxCollectionItems) + { + text.Append(pad + "... (truncated)"); + break; + } + + text.Append(pad + PrintToString(item, nestingLevel + 1)); + } + + collectionResult = text.ToString(); + return true; + } + + collectionResult = null; + return false; + } + + private bool TrySerializeFinalType(object obj, out string? result) + { + var type = obj.GetType(); + if (finalTypes.Contains(type)) + { + result = (obj switch + { + IFormattable formattable when typeCulture.ContainsKey(type) => + formattable.ToString("G", typeCulture[type]), + string str => Trim(str), + _ => obj.ToString() + })!; + + result = AppendNewLine(result); + return true; + } + + result = null; + return false; + } + + private string Trim(string str) + { + if (maxStringLength.HasValue && str.Length > maxStringLength) + return str[..maxStringLength.Value]; + return str; + } + + private string SerializeComplexType(object obj, int nestingLevel) + { + processedObjects.Add(obj); + try + { + var pad = new string('\t', nestingLevel + 1); + var text = new StringBuilder(); + var type = obj.GetType(); + text.AppendLine(type.Name); + + foreach (var property in type.GetProperties()) + { + if (excludedTypes.Contains(property.PropertyType) || excludedProperties.Contains(property)) + continue; + + var value = property.GetValue(obj); + var serializedValue = SerializeProperty(property, value, nestingLevel + 1); + text.Append(pad + $"{property.Name} = {serializedValue}"); + } + + return text.ToString(); + } + finally + { + processedObjects.Remove(obj); + } + } + + private string? SerializeProperty(PropertyInfo property, object? value, int nestingLevel) + { + try + { + if (propertySerializers.TryGetValue(property, out var propertySerializer)) + return AppendNewLine(propertySerializer(value!)); + + if (typeSerializers.TryGetValue(property.PropertyType, out var typeSerializer)) + return AppendNewLine(typeSerializer(value!)); + + return PrintToString(value, nestingLevel); + } + catch (Exception ex) + { + return AppendNewLine($"[Error serializing {property.Name}: {ex.Message}]"); + } + } + + private static MemberInfo GetPropertyInfo(Expression> propertyExpression) + { + if (propertyExpression.Body is not MemberExpression member) + throw new ArgumentException("Expression must be a property access.", nameof(propertyExpression)); + return member.Member; + } + + private static string AppendNewLine(string? text) => + text != null && text.EndsWith(Environment.NewLine) ? text : text + Environment.NewLine; +} \ No newline at end of file diff --git a/ObjectPrinting/PrintingConfig.cs b/ObjectPrinting/PrintingConfig.cs deleted file mode 100644 index a9e08211..00000000 --- a/ObjectPrinting/PrintingConfig.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System; -using System.Linq; -using System.Text; - -namespace ObjectPrinting -{ - public class PrintingConfig - { - public string PrintToString(TOwner obj) - { - return PrintToString(obj, 0); - } - - private string PrintToString(object obj, int nestingLevel) - { - //TODO apply configurations - if (obj == null) - return "null" + Environment.NewLine; - - var finalTypes = new[] - { - typeof(int), typeof(double), typeof(float), typeof(string), - typeof(DateTime), typeof(TimeSpan) - }; - if (finalTypes.Contains(obj.GetType())) - return obj + Environment.NewLine; - - var identation = new string('\t', nestingLevel + 1); - var sb = new StringBuilder(); - var type = obj.GetType(); - sb.AppendLine(type.Name); - foreach (var propertyInfo in type.GetProperties()) - { - sb.Append(identation + propertyInfo.Name + " = " + - PrintToString(propertyInfo.GetValue(obj), - nestingLevel + 1)); - } - return sb.ToString(); - } - } -} \ No newline at end of file From af4664a7acd3b2ba51214c2cd2626ec98da14800 Mon Sep 17 00:00:00 2001 From: Kostornoj-Dmitrij Date: Thu, 12 Dec 2024 20:51:01 +0500 Subject: [PATCH 06/13] Add verified files because forgot --- .../ObjectPrintingTests.csproj | 84 ++++++++ ...tCultureForDate_culture=en-US.verified.txt | 1 + ...tCultureForDate_culture=ru-RU.verified.txt | 1 + ...rinter_ShouldHandle_WhenArray.verified.txt | 11 + ...dHandle_WhenCircularReference.verified.txt | 7 + ...CustomCollectionSerialization.verified.txt | 4 + ...enCustomPropertySerialization.verified.txt | 5 + ...e_WhenCustomTypeSerialization.verified.txt | 5 + ..._ShouldHandle_WhenDeepNesting.verified.txt | 7 + ...r_ShouldHandle_WhenDictionary.verified.txt | 22 ++ ...ldHandle_WhenExcessiveNesting.verified.txt | 203 ++++++++++++++++++ ...uldHandle_WhenExcludeProperty.verified.txt | 4 + ..._ShouldHandle_WhenExcludeType.verified.txt | 4 + ...le_WhenFaultyItemInCollection.verified.txt | 12 ++ ...e_WhenLargeCollectionWithTrim.verified.txt | 102 +++++++++ ...henLengthIsGreaterThanTrimmed.verified.txt | 5 + ...Printer_ShouldHandle_WhenList.verified.txt | 16 ++ ..._ShouldHandle_WhenNestedArray.verified.txt | 16 ++ ...er_ShouldHandle_WhenSerialize.verified.txt | 5 + ...ure=en-US_expectedSeparator=..verified.txt | 5 + ...ure=ru-RU_expectedSeparator=,.verified.txt | 5 + ...r_ShouldHandle_WhenTrimString.verified.txt | 5 + 22 files changed, 529 insertions(+) create mode 100644 ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldFormatDateTime_WhenSetCultureForDate_culture=en-US.verified.txt create mode 100644 ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldFormatDateTime_WhenSetCultureForDate_culture=ru-RU.verified.txt create mode 100644 ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenArray.verified.txt create mode 100644 ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenCircularReference.verified.txt create mode 100644 ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenCustomCollectionSerialization.verified.txt create mode 100644 ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenCustomPropertySerialization.verified.txt create mode 100644 ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenCustomTypeSerialization.verified.txt create mode 100644 ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenDeepNesting.verified.txt create mode 100644 ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenDictionary.verified.txt create mode 100644 ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenExcessiveNesting.verified.txt create mode 100644 ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenExcludeProperty.verified.txt create mode 100644 ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenExcludeType.verified.txt create mode 100644 ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenFaultyItemInCollection.verified.txt create mode 100644 ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenLargeCollectionWithTrim.verified.txt create mode 100644 ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenLengthIsGreaterThanTrimmed.verified.txt create mode 100644 ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenList.verified.txt create mode 100644 ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenNestedArray.verified.txt create mode 100644 ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenSerialize.verified.txt create mode 100644 ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenSetCultureForNumber_culture=en-US_expectedSeparator=..verified.txt create mode 100644 ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenSetCultureForNumber_culture=ru-RU_expectedSeparator=,.verified.txt create mode 100644 ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenTrimString.verified.txt diff --git a/ObjectPrintingTests/ObjectPrintingTests.csproj b/ObjectPrintingTests/ObjectPrintingTests.csproj index 242935b9..7e9463bc 100644 --- a/ObjectPrintingTests/ObjectPrintingTests.csproj +++ b/ObjectPrintingTests/ObjectPrintingTests.csproj @@ -73,6 +73,90 @@ ObjectPrinter_Should + + ObjectPrinter_Should + ObjectPrinter_Should.cs + + + ObjectPrinter_Should + ObjectPrinter_Should.cs + + + ObjectPrinter_Should + ObjectPrinter_Should.cs + + + ObjectPrinter_Should + ObjectPrinter_Should.cs + + + ObjectPrinter_Should + ObjectPrinter_Should.cs + + + ObjectPrinter_Should + ObjectPrinter_Should.cs + + + ObjectPrinter_Should + ObjectPrinter_Should.cs + + + ObjectPrinter_Should + ObjectPrinter_Should.cs + + + ObjectPrinter_Should + ObjectPrinter_Should.cs + + + ObjectPrinter_Should + ObjectPrinter_Should.cs + + + ObjectPrinter_Should + ObjectPrinter_Should.cs + + + ObjectPrinter_Should + ObjectPrinter_Should.cs + + + ObjectPrinter_Should + ObjectPrinter_Should.cs + + + ObjectPrinter_Should + ObjectPrinter_Should.cs + + + ObjectPrinter_Should + ObjectPrinter_Should.cs + + + ObjectPrinter_Should + ObjectPrinter_Should.cs + + + ObjectPrinter_Should + ObjectPrinter_Should.cs + + + ObjectPrinter_Should + ObjectPrinter_Should.cs + + + ObjectPrinter_Should + ObjectPrinter_Should.cs + + + ObjectPrinter_Should + ObjectPrinter_Should.cs + + + ObjectPrinter_Should + ObjectPrinter_Should.cs + diff --git a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldFormatDateTime_WhenSetCultureForDate_culture=en-US.verified.txt b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldFormatDateTime_WhenSetCultureForDate_culture=en-US.verified.txt new file mode 100644 index 00000000..18441db5 --- /dev/null +++ b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldFormatDateTime_WhenSetCultureForDate_culture=en-US.verified.txt @@ -0,0 +1 @@ +12/11/2024 12:00:00 AM diff --git a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldFormatDateTime_WhenSetCultureForDate_culture=ru-RU.verified.txt b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldFormatDateTime_WhenSetCultureForDate_culture=ru-RU.verified.txt new file mode 100644 index 00000000..32910e86 --- /dev/null +++ b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldFormatDateTime_WhenSetCultureForDate_culture=ru-RU.verified.txt @@ -0,0 +1 @@ +11.12.2024 00:00:00 diff --git a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenArray.verified.txt b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenArray.verified.txt new file mode 100644 index 00000000..478513cf --- /dev/null +++ b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenArray.verified.txt @@ -0,0 +1,11 @@ +SimplePerson[]: + SimplePerson + Id = 00000000-0000-0000-0000-000000000000 + Name = Dima + Height = 0 + Age = 20 + SimplePerson + Id = 00000000-0000-0000-0000-000000000000 + Name = Andrey + Height = 190 + Age = 45 diff --git a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenCircularReference.verified.txt b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenCircularReference.verified.txt new file mode 100644 index 00000000..160ec166 --- /dev/null +++ b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenCircularReference.verified.txt @@ -0,0 +1,7 @@ +PersonWithRoots + Id = 00000000-0000-0000-0000-000000000000 + Name = Dima + Height = 0 + Age = 20 + Root = [Circular Reference] + Childrens = null diff --git a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenCustomCollectionSerialization.verified.txt b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenCustomCollectionSerialization.verified.txt new file mode 100644 index 00000000..1dc8b35e --- /dev/null +++ b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenCustomCollectionSerialization.verified.txt @@ -0,0 +1,4 @@ +List`1: + 1 + 2 + 3 diff --git a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenCustomPropertySerialization.verified.txt b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenCustomPropertySerialization.verified.txt new file mode 100644 index 00000000..a80a2fb6 --- /dev/null +++ b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenCustomPropertySerialization.verified.txt @@ -0,0 +1,5 @@ +SimplePerson + Id = 00000000-0000-0000-0000-000000000000 + Name = Name + Height = 0 + Age = 20 diff --git a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenCustomTypeSerialization.verified.txt b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenCustomTypeSerialization.verified.txt new file mode 100644 index 00000000..86d21fda --- /dev/null +++ b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenCustomTypeSerialization.verified.txt @@ -0,0 +1,5 @@ +SimplePerson + Id = 00000000-0000-0000-0000-000000000000 + Name = Dima + Height = 0 + Age = Age diff --git a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenDeepNesting.verified.txt b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenDeepNesting.verified.txt new file mode 100644 index 00000000..bb0040a5 --- /dev/null +++ b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenDeepNesting.verified.txt @@ -0,0 +1,7 @@ +NestedObject + Name = Root + Child = NestedObject + Name = Child + Child = NestedObject + Name = GrandChild + Child = null diff --git a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenDictionary.verified.txt b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenDictionary.verified.txt new file mode 100644 index 00000000..b810560b --- /dev/null +++ b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenDictionary.verified.txt @@ -0,0 +1,22 @@ +Dictionary`2: + KeyValuePair`2 + Key = SimplePerson + Id = 00000000-0000-0000-0000-000000000000 + Name = Dima + Height = 0 + Age = 20 + Value = first + KeyValuePair`2 + Key = SimplePerson + Id = 00000000-0000-0000-0000-000000000000 + Name = Andrey + Height = 0 + Age = 45 + Value = second + KeyValuePair`2 + Key = SimplePerson + Id = 00000000-0000-0000-0000-000000000000 + Name = Vadim + Height = 0 + Age = 10 + Value = third diff --git a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenExcessiveNesting.verified.txt b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenExcessiveNesting.verified.txt new file mode 100644 index 00000000..311a1ce0 --- /dev/null +++ b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenExcessiveNesting.verified.txt @@ -0,0 +1,203 @@ +NestedObject + Name = Level 0 + Child = NestedObject + Name = Level 1 + Child = NestedObject + Name = Level 2 + Child = NestedObject + Name = Level 3 + Child = NestedObject + Name = Level 4 + Child = NestedObject + Name = Level 5 + Child = NestedObject + Name = Level 6 + Child = NestedObject + Name = Level 7 + Child = NestedObject + Name = Level 8 + Child = NestedObject + Name = Level 9 + Child = NestedObject + Name = Level 10 + Child = NestedObject + Name = Level 11 + Child = NestedObject + Name = Level 12 + Child = NestedObject + Name = Level 13 + Child = NestedObject + Name = Level 14 + Child = NestedObject + Name = Level 15 + Child = NestedObject + Name = Level 16 + Child = NestedObject + Name = Level 17 + Child = NestedObject + Name = Level 18 + Child = NestedObject + Name = Level 19 + Child = NestedObject + Name = Level 20 + Child = NestedObject + Name = Level 21 + Child = NestedObject + Name = Level 22 + Child = NestedObject + Name = Level 23 + Child = NestedObject + Name = Level 24 + Child = NestedObject + Name = Level 25 + Child = NestedObject + Name = Level 26 + Child = NestedObject + Name = Level 27 + Child = NestedObject + Name = Level 28 + Child = NestedObject + Name = Level 29 + Child = NestedObject + Name = Level 30 + Child = NestedObject + Name = Level 31 + Child = NestedObject + Name = Level 32 + Child = NestedObject + Name = Level 33 + Child = NestedObject + Name = Level 34 + Child = NestedObject + Name = Level 35 + Child = NestedObject + Name = Level 36 + Child = NestedObject + Name = Level 37 + Child = NestedObject + Name = Level 38 + Child = NestedObject + Name = Level 39 + Child = NestedObject + Name = Level 40 + Child = NestedObject + Name = Level 41 + Child = NestedObject + Name = Level 42 + Child = NestedObject + Name = Level 43 + Child = NestedObject + Name = Level 44 + Child = NestedObject + Name = Level 45 + Child = NestedObject + Name = Level 46 + Child = NestedObject + Name = Level 47 + Child = NestedObject + Name = Level 48 + Child = NestedObject + Name = Level 49 + Child = NestedObject + Name = Level 50 + Child = NestedObject + Name = Level 51 + Child = NestedObject + Name = Level 52 + Child = NestedObject + Name = Level 53 + Child = NestedObject + Name = Level 54 + Child = NestedObject + Name = Level 55 + Child = NestedObject + Name = Level 56 + Child = NestedObject + Name = Level 57 + Child = NestedObject + Name = Level 58 + Child = NestedObject + Name = Level 59 + Child = NestedObject + Name = Level 60 + Child = NestedObject + Name = Level 61 + Child = NestedObject + Name = Level 62 + Child = NestedObject + Name = Level 63 + Child = NestedObject + Name = Level 64 + Child = NestedObject + Name = Level 65 + Child = NestedObject + Name = Level 66 + Child = NestedObject + Name = Level 67 + Child = NestedObject + Name = Level 68 + Child = NestedObject + Name = Level 69 + Child = NestedObject + Name = Level 70 + Child = NestedObject + Name = Level 71 + Child = NestedObject + Name = Level 72 + Child = NestedObject + Name = Level 73 + Child = NestedObject + Name = Level 74 + Child = NestedObject + Name = Level 75 + Child = NestedObject + Name = Level 76 + Child = NestedObject + Name = Level 77 + Child = NestedObject + Name = Level 78 + Child = NestedObject + Name = Level 79 + Child = NestedObject + Name = Level 80 + Child = NestedObject + Name = Level 81 + Child = NestedObject + Name = Level 82 + Child = NestedObject + Name = Level 83 + Child = NestedObject + Name = Level 84 + Child = NestedObject + Name = Level 85 + Child = NestedObject + Name = Level 86 + Child = NestedObject + Name = Level 87 + Child = NestedObject + Name = Level 88 + Child = NestedObject + Name = Level 89 + Child = NestedObject + Name = Level 90 + Child = NestedObject + Name = Level 91 + Child = NestedObject + Name = Level 92 + Child = NestedObject + Name = Level 93 + Child = NestedObject + Name = Level 94 + Child = NestedObject + Name = Level 95 + Child = NestedObject + Name = Level 96 + Child = NestedObject + Name = Level 97 + Child = NestedObject + Name = Level 98 + Child = NestedObject + Name = Level 99 + Child = NestedObject + Name = Level 100 + Child = null diff --git a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenExcludeProperty.verified.txt b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenExcludeProperty.verified.txt new file mode 100644 index 00000000..8f480f16 --- /dev/null +++ b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenExcludeProperty.verified.txt @@ -0,0 +1,4 @@ +SimplePerson + Id = 00000000-0000-0000-0000-000000000000 + Name = Dima + Height = 0 diff --git a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenExcludeType.verified.txt b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenExcludeType.verified.txt new file mode 100644 index 00000000..8f480f16 --- /dev/null +++ b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenExcludeType.verified.txt @@ -0,0 +1,4 @@ +SimplePerson + Id = 00000000-0000-0000-0000-000000000000 + Name = Dima + Height = 0 diff --git a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenFaultyItemInCollection.verified.txt b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenFaultyItemInCollection.verified.txt new file mode 100644 index 00000000..e364bc34 --- /dev/null +++ b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenFaultyItemInCollection.verified.txt @@ -0,0 +1,12 @@ +List`1: + SimplePerson + Id = 00000000-0000-0000-0000-000000000000 + Name = Dima + Height = 0 + Age = 20 + FaultyType + SimplePerson + Id = 00000000-0000-0000-0000-000000000000 + Name = Andrey + Height = 0 + Age = 45 diff --git a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenLargeCollectionWithTrim.verified.txt b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenLargeCollectionWithTrim.verified.txt new file mode 100644 index 00000000..becf4b1f --- /dev/null +++ b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenLargeCollectionWithTrim.verified.txt @@ -0,0 +1,102 @@ +List`1: + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20 + 21 + 22 + 23 + 24 + 25 + 26 + 27 + 28 + 29 + 30 + 31 + 32 + 33 + 34 + 35 + 36 + 37 + 38 + 39 + 40 + 41 + 42 + 43 + 44 + 45 + 46 + 47 + 48 + 49 + 50 + 51 + 52 + 53 + 54 + 55 + 56 + 57 + 58 + 59 + 60 + 61 + 62 + 63 + 64 + 65 + 66 + 67 + 68 + 69 + 70 + 71 + 72 + 73 + 74 + 75 + 76 + 77 + 78 + 79 + 80 + 81 + 82 + 83 + 84 + 85 + 86 + 87 + 88 + 89 + 90 + 91 + 92 + 93 + 94 + 95 + 96 + 97 + 98 + 99 + 100 + ... (truncated) \ No newline at end of file diff --git a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenLengthIsGreaterThanTrimmed.verified.txt b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenLengthIsGreaterThanTrimmed.verified.txt new file mode 100644 index 00000000..0c871a19 --- /dev/null +++ b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenLengthIsGreaterThanTrimmed.verified.txt @@ -0,0 +1,5 @@ +SimplePerson + Id = 00000000-0000-0000-0000-000000000000 + Name = Dima + Height = 0 + Age = 20 diff --git a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenList.verified.txt b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenList.verified.txt new file mode 100644 index 00000000..2f950a8f --- /dev/null +++ b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenList.verified.txt @@ -0,0 +1,16 @@ +List`1: + SimplePerson + Id = 00000000-0000-0000-0000-000000000000 + Name = Dima + Height = 0 + Age = 20 + SimplePerson + Id = 00000000-0000-0000-0000-000000000000 + Name = Andrey + Height = 0 + Age = 45 + SimplePerson + Id = 00000000-0000-0000-0000-000000000000 + Name = Vadim + Height = 0 + Age = 10 diff --git a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenNestedArray.verified.txt b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenNestedArray.verified.txt new file mode 100644 index 00000000..81d66dfc --- /dev/null +++ b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenNestedArray.verified.txt @@ -0,0 +1,16 @@ +SimplePerson[]: + SimplePerson + Id = 00000000-0000-0000-0000-000000000000 + Name = Dima + Height = 0 + Age = 20 + SimplePerson + Id = 00000000-0000-0000-0000-000000000000 + Name = Andrey + Height = 0 + Age = 45 + SimplePerson + Id = 00000000-0000-0000-0000-000000000000 + Name = Vadim + Height = 0 + Age = 10 diff --git a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenSerialize.verified.txt b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenSerialize.verified.txt new file mode 100644 index 00000000..0c871a19 --- /dev/null +++ b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenSerialize.verified.txt @@ -0,0 +1,5 @@ +SimplePerson + Id = 00000000-0000-0000-0000-000000000000 + Name = Dima + Height = 0 + Age = 20 diff --git a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenSetCultureForNumber_culture=en-US_expectedSeparator=..verified.txt b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenSetCultureForNumber_culture=en-US_expectedSeparator=..verified.txt new file mode 100644 index 00000000..889ce416 --- /dev/null +++ b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenSetCultureForNumber_culture=en-US_expectedSeparator=..verified.txt @@ -0,0 +1,5 @@ +SimplePerson + Id = 00000000-0000-0000-0000-000000000000 + Name = Dima + Height = 180.5 + Age = 20 diff --git a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenSetCultureForNumber_culture=ru-RU_expectedSeparator=,.verified.txt b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenSetCultureForNumber_culture=ru-RU_expectedSeparator=,.verified.txt new file mode 100644 index 00000000..a8f58039 --- /dev/null +++ b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenSetCultureForNumber_culture=ru-RU_expectedSeparator=,.verified.txt @@ -0,0 +1,5 @@ +SimplePerson + Id = 00000000-0000-0000-0000-000000000000 + Name = Dima + Height = 180,5 + Age = 20 diff --git a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenTrimString.verified.txt b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenTrimString.verified.txt new file mode 100644 index 00000000..8346b41f --- /dev/null +++ b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenTrimString.verified.txt @@ -0,0 +1,5 @@ +SimplePerson + Id = 00000000-0000-0000-0000-000000000000 + Name = Di + Height = 0 + Age = 20 From 6e5b2f75c70c33f41d0f45516d97e892c5ff1d6e Mon Sep 17 00:00:00 2001 From: Kostornoj-Dmitrij Date: Fri, 13 Dec 2024 13:36:24 +0500 Subject: [PATCH 07/13] =?UTF-8?q?=D0=A3=D0=B4=D0=B0=D0=BB=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BF=D0=B0=D0=BF=D0=BA=D1=83=20=D1=82=D0=B5=D1=81=D1=82=D0=BE?= =?UTF-8?q?=D0=B2=20=D0=B8=20Solved=20=D0=B8=D0=B7=20ObjectOrinting=20?= =?UTF-8?q?=D0=B8=20=D0=BD=D0=B5=D0=BD=D1=83=D0=B6=D0=BD=D1=8B=D0=B5=20?= =?UTF-8?q?=D0=B7=D0=B0=D0=B2=D0=B8=D1=81=D0=B8=D0=BC=D0=BE=D1=81=D1=82?= =?UTF-8?q?=D0=B8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ObjectPrinting/ObjectPrinting.csproj | 8 +----- .../Tests/ObjectPrinterAcceptanceTests.cs | 28 ------------------- ObjectPrinting/Tests/Person.cs | 12 -------- 3 files changed, 1 insertion(+), 47 deletions(-) delete mode 100644 ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs delete mode 100644 ObjectPrinting/Tests/Person.cs diff --git a/ObjectPrinting/ObjectPrinting.csproj b/ObjectPrinting/ObjectPrinting.csproj index c5db392f..11ec0e44 100644 --- a/ObjectPrinting/ObjectPrinting.csproj +++ b/ObjectPrinting/ObjectPrinting.csproj @@ -3,10 +3,4 @@ net8.0 enable - - - - - - - + \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs deleted file mode 100644 index 29008705..00000000 --- a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs +++ /dev/null @@ -1,28 +0,0 @@ -using NUnit.Framework; - -namespace ObjectPrinting.Tests -{ - [TestFixture] - public class ObjectPrinterAcceptanceTests - { - [Test] - public void Demo() - { - - var person = new Person { Name = "Alex", Age = 19 }; - - //var printer = ObjectPrinter.For(); - //1. Исключить из сериализации свойства определенного типа - //2. Указать альтернативный способ сериализации для определенного типа - //3. Для числовых типов указать культуру - //4. Настроить сериализацию конкретного свойства - //5. Настроить обрезание строковых свойств (метод должен быть виден только для строковых свойств) - //6. Исключить из сериализации конкретного свойства - - //string s1 = printer.PrintToString(person); - - //7. Синтаксический сахар в виде метода расширения, сериализующего по-умолчанию - //8. ...с конфигурированием - } - } -} \ No newline at end of file diff --git a/ObjectPrinting/Tests/Person.cs b/ObjectPrinting/Tests/Person.cs deleted file mode 100644 index f9555955..00000000 --- a/ObjectPrinting/Tests/Person.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; - -namespace ObjectPrinting.Tests -{ - public class Person - { - public Guid Id { get; set; } - public string Name { get; set; } - public double Height { get; set; } - public int Age { get; set; } - } -} \ No newline at end of file From 4136bfafb1450333d0248883a105de4bddd1510f Mon Sep 17 00:00:00 2001 From: Kostornoj-Dmitrij Date: Fri, 13 Dec 2024 13:37:07 +0500 Subject: [PATCH 08/13] =?UTF-8?q?=D0=A3=D0=B4=D0=B0=D0=BB=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BF=D0=B0=D0=BF=D0=BA=D1=83=20Solved=20=D0=B8=D0=B7=20Object?= =?UTF-8?q?Printing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ObjectPrinting/Solved/ObjectExtensions.cs | 10 --- ObjectPrinting/Solved/ObjectPrinter.cs | 10 --- ObjectPrinting/Solved/PrintingConfig.cs | 62 ------------------- .../Solved/PropertyPrintingConfig.cs | 32 ---------- .../PropertyPrintingConfigExtensions.cs | 18 ------ .../Tests/ObjectPrinterAcceptanceTests.cs | 40 ------------ ObjectPrinting/Solved/Tests/Person.cs | 12 ---- 7 files changed, 184 deletions(-) delete mode 100644 ObjectPrinting/Solved/ObjectExtensions.cs delete mode 100644 ObjectPrinting/Solved/ObjectPrinter.cs delete mode 100644 ObjectPrinting/Solved/PrintingConfig.cs delete mode 100644 ObjectPrinting/Solved/PropertyPrintingConfig.cs delete mode 100644 ObjectPrinting/Solved/PropertyPrintingConfigExtensions.cs delete mode 100644 ObjectPrinting/Solved/Tests/ObjectPrinterAcceptanceTests.cs delete mode 100644 ObjectPrinting/Solved/Tests/Person.cs diff --git a/ObjectPrinting/Solved/ObjectExtensions.cs b/ObjectPrinting/Solved/ObjectExtensions.cs deleted file mode 100644 index b0c94553..00000000 --- a/ObjectPrinting/Solved/ObjectExtensions.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace ObjectPrinting.Solved -{ - public static class ObjectExtensions - { - public static string PrintToString(this T obj) - { - return ObjectPrinter.For().PrintToString(obj); - } - } -} \ No newline at end of file diff --git a/ObjectPrinting/Solved/ObjectPrinter.cs b/ObjectPrinting/Solved/ObjectPrinter.cs deleted file mode 100644 index 540ee769..00000000 --- a/ObjectPrinting/Solved/ObjectPrinter.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace ObjectPrinting.Solved -{ - public class ObjectPrinter - { - public static PrintingConfig For() - { - return new PrintingConfig(); - } - } -} \ No newline at end of file diff --git a/ObjectPrinting/Solved/PrintingConfig.cs b/ObjectPrinting/Solved/PrintingConfig.cs deleted file mode 100644 index 0ec5aeb2..00000000 --- a/ObjectPrinting/Solved/PrintingConfig.cs +++ /dev/null @@ -1,62 +0,0 @@ -using System; -using System.Linq; -using System.Linq.Expressions; -using System.Text; - -namespace ObjectPrinting.Solved -{ - public class PrintingConfig - { - public PropertyPrintingConfig Printing() - { - return new PropertyPrintingConfig(this); - } - - public PropertyPrintingConfig Printing(Expression> memberSelector) - { - return new PropertyPrintingConfig(this); - } - - public PrintingConfig Excluding(Expression> memberSelector) - { - return this; - } - - internal PrintingConfig Excluding() - { - return this; - } - - public string PrintToString(TOwner obj) - { - return PrintToString(obj, 0); - } - - private string PrintToString(object obj, int nestingLevel) - { - //TODO apply configurations - if (obj == null) - return "null" + Environment.NewLine; - - var finalTypes = new[] - { - typeof(int), typeof(double), typeof(float), typeof(string), - typeof(DateTime), typeof(TimeSpan) - }; - if (finalTypes.Contains(obj.GetType())) - return obj + Environment.NewLine; - - var identation = new string('\t', nestingLevel + 1); - var sb = new StringBuilder(); - var type = obj.GetType(); - sb.AppendLine(type.Name); - foreach (var propertyInfo in type.GetProperties()) - { - sb.Append(identation + propertyInfo.Name + " = " + - PrintToString(propertyInfo.GetValue(obj), - nestingLevel + 1)); - } - return sb.ToString(); - } - } -} \ No newline at end of file diff --git a/ObjectPrinting/Solved/PropertyPrintingConfig.cs b/ObjectPrinting/Solved/PropertyPrintingConfig.cs deleted file mode 100644 index a509697d..00000000 --- a/ObjectPrinting/Solved/PropertyPrintingConfig.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System; -using System.Globalization; - -namespace ObjectPrinting.Solved -{ - public class PropertyPrintingConfig : IPropertyPrintingConfig - { - private readonly PrintingConfig printingConfig; - - public PropertyPrintingConfig(PrintingConfig printingConfig) - { - this.printingConfig = printingConfig; - } - - public PrintingConfig Using(Func print) - { - return printingConfig; - } - - public PrintingConfig Using(CultureInfo culture) - { - return printingConfig; - } - - PrintingConfig IPropertyPrintingConfig.ParentConfig => printingConfig; - } - - public interface IPropertyPrintingConfig - { - PrintingConfig ParentConfig { get; } - } -} \ No newline at end of file diff --git a/ObjectPrinting/Solved/PropertyPrintingConfigExtensions.cs b/ObjectPrinting/Solved/PropertyPrintingConfigExtensions.cs deleted file mode 100644 index dd392239..00000000 --- a/ObjectPrinting/Solved/PropertyPrintingConfigExtensions.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; - -namespace ObjectPrinting.Solved -{ - public static class PropertyPrintingConfigExtensions - { - public static string PrintToString(this T obj, Func, PrintingConfig> config) - { - return config(ObjectPrinter.For()).PrintToString(obj); - } - - public static PrintingConfig TrimmedToLength(this PropertyPrintingConfig propConfig, int maxLen) - { - return ((IPropertyPrintingConfig)propConfig).ParentConfig; - } - - } -} \ No newline at end of file diff --git a/ObjectPrinting/Solved/Tests/ObjectPrinterAcceptanceTests.cs b/ObjectPrinting/Solved/Tests/ObjectPrinterAcceptanceTests.cs deleted file mode 100644 index ac52d5ee..00000000 --- a/ObjectPrinting/Solved/Tests/ObjectPrinterAcceptanceTests.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using System.Globalization; -using NUnit.Framework; - -namespace ObjectPrinting.Solved.Tests -{ - [TestFixture] - public class ObjectPrinterAcceptanceTests - { - [Test] - public void Demo() - { - var person = new Person { Name = "Alex", Age = 19 }; - - var printer = ObjectPrinter.For() - //1. Исключить из сериализации свойства определенного типа - .Excluding() - //2. Указать альтернативный способ сериализации для определенного типа - .Printing().Using(i => i.ToString("X")) - //3. Для числовых типов указать культуру - .Printing().Using(CultureInfo.InvariantCulture) - //4. Настроить сериализацию конкретного свойства - //5. Настроить обрезание строковых свойств (метод должен быть виден только для строковых свойств) - .Printing(p => p.Name).TrimmedToLength(10) - //6. Исключить из сериализации конкретного свойства - .Excluding(p => p.Age); - - string s1 = printer.PrintToString(person); - - //7. Синтаксический сахар в виде метода расширения, сериализующего по-умолчанию - string s2 = person.PrintToString(); - - //8. ...с конфигурированием - string s3 = person.PrintToString(s => s.Excluding(p => p.Age)); - Console.WriteLine(s1); - Console.WriteLine(s2); - Console.WriteLine(s3); - } - } -} \ No newline at end of file diff --git a/ObjectPrinting/Solved/Tests/Person.cs b/ObjectPrinting/Solved/Tests/Person.cs deleted file mode 100644 index 858ebbf8..00000000 --- a/ObjectPrinting/Solved/Tests/Person.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; - -namespace ObjectPrinting.Solved.Tests -{ - public class Person - { - public Guid Id { get; set; } - public string Name { get; set; } - public double Height { get; set; } - public int Age { get; set; } - } -} \ No newline at end of file From d6049406821d20929087fb27b91fae857606d4c2 Mon Sep 17 00:00:00 2001 From: Kostornoj-Dmitrij Date: Fri, 13 Dec 2024 13:50:41 +0500 Subject: [PATCH 09/13] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D0=B0=D0=BD=D1=82=D1=83?= =?UTF-8?q?=20=D0=B4=D0=BB=D1=8F=20=D1=80=D0=B0=D0=B7=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=B8=D1=82=D0=B5=D0=BB=D1=8F,=20=D0=B8=D0=BD=D1=82=D0=B5?= =?UTF-8?q?=D1=80=D0=BF=D0=BE=D0=BB=D1=8F=D1=86=D0=B8=D1=8E=20=D0=B2=D0=BC?= =?UTF-8?q?=D0=B5=D1=81=D1=82=D0=BE=20=D0=BA=D0=BE=D0=BD=D0=BA=D0=B0=D1=82?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=D1=86=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ObjectPrinting/ObjectPrinterSettings.cs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/ObjectPrinting/ObjectPrinterSettings.cs b/ObjectPrinting/ObjectPrinterSettings.cs index ef4c7ed0..bfa9e051 100644 --- a/ObjectPrinting/ObjectPrinterSettings.cs +++ b/ObjectPrinting/ObjectPrinterSettings.cs @@ -23,6 +23,7 @@ public class ObjectPrinterSettings private readonly Dictionary> propertySerializers = []; private int? maxStringLength; private const int MaxCollectionItems = 100; + private const char IndentChar = '\t'; public ObjectPrinterSettings Exclude() { @@ -85,7 +86,7 @@ private bool TrySerializeCollection(object obj, int nestingLevel, out string? co if (obj is IEnumerable enumerable) { var text = new StringBuilder(); - var pad = new string('\t', nestingLevel + 1); + var pad = new string(IndentChar, nestingLevel + 1); var count = 0; text.AppendLine($"{obj.GetType().Name}:"); @@ -93,11 +94,11 @@ private bool TrySerializeCollection(object obj, int nestingLevel, out string? co { if (count++ >= MaxCollectionItems) { - text.Append(pad + "... (truncated)"); + text.Append($"{pad}... (truncated)"); break; } - text.Append(pad + PrintToString(item, nestingLevel + 1)); + text.Append($"{pad}{PrintToString(item, nestingLevel + 1)}"); } collectionResult = text.ToString(); @@ -141,7 +142,7 @@ private string SerializeComplexType(object obj, int nestingLevel) processedObjects.Add(obj); try { - var pad = new string('\t', nestingLevel + 1); + var pad = new string(IndentChar, nestingLevel + 1); var text = new StringBuilder(); var type = obj.GetType(); text.AppendLine(type.Name); @@ -153,7 +154,7 @@ private string SerializeComplexType(object obj, int nestingLevel) var value = property.GetValue(obj); var serializedValue = SerializeProperty(property, value, nestingLevel + 1); - text.Append(pad + $"{property.Name} = {serializedValue}"); + text.Append($"{pad}{property.Name} = {serializedValue}"); } return text.ToString(); From c0dcf83d6e32b542d3d03650f54ede3bc394674a Mon Sep 17 00:00:00 2001 From: Kostornoj-Dmitrij Date: Fri, 13 Dec 2024 17:00:39 +0500 Subject: [PATCH 10/13] =?UTF-8?q?=D0=A0=D0=B0=D0=B7=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=B8=D0=BB=20=D0=BF=D0=BE=D0=B4=D0=B3=D0=BE=D1=82=D0=BE=D0=B2?= =?UTF-8?q?=D0=BA=D1=83=20=D0=BA=20=D1=82=D0=B5=D1=81=D1=82=D0=B0=D0=BC=20?= =?UTF-8?q?=D0=B8=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B8.=20?= =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20=D1=82=D0=B5=D1=81?= =?UTF-8?q?=D1=82=D1=8B=20=D0=BD=D0=B0=20=D0=B8=D1=81=D0=BA=D0=BB=D1=8E?= =?UTF-8?q?=D1=87=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ObjectPrintingTests/ObjectPrinter_Should.cs | 68 ++++++++++++++++----- 1 file changed, 54 insertions(+), 14 deletions(-) diff --git a/ObjectPrintingTests/ObjectPrinter_Should.cs b/ObjectPrintingTests/ObjectPrinter_Should.cs index 8a7a945e..e90a8276 100644 --- a/ObjectPrintingTests/ObjectPrinter_Should.cs +++ b/ObjectPrintingTests/ObjectPrinter_Should.cs @@ -21,7 +21,9 @@ public void SetUp() public Task ObjectPrinter_ShouldHandle_WhenSerialize() { var person = new SimplePerson { Name = "Dima", Age = 20 }; + var printedString = printer.Serialize(person); + return Verify(printedString, Settings); } @@ -29,8 +31,10 @@ public Task ObjectPrinter_ShouldHandle_WhenSerialize() public Task ObjectPrinter_ShouldHandle_WhenExcludeProperty() { var person = new SimplePerson { Name = "Dima", Age = 20 }; + var printedString = printer.Serialize(person, c => c.Exclude(x => x.Age)); + return Verify(printedString, Settings); } @@ -38,8 +42,10 @@ public Task ObjectPrinter_ShouldHandle_WhenExcludeProperty() public Task ObjectPrinter_ShouldHandle_WhenExcludeType() { var person = new SimplePerson { Name = "Dima", Age = 20 }; + var printedString = printer.Serialize(person, c => c.Exclude()); + return Verify(printedString, Settings); } @@ -47,8 +53,10 @@ public Task ObjectPrinter_ShouldHandle_WhenExcludeType() public Task ObjectPrinter_ShouldHandle_WhenCustomPropertySerialization() { var person = new SimplePerson { Name = "Dima", Age = 20 }; + var printedString = printer.Serialize(person, c => c.SetCustomSerialization(x => x.Name, _ => "Name")); + return Verify(printedString, Settings); } @@ -56,8 +64,10 @@ public Task ObjectPrinter_ShouldHandle_WhenCustomPropertySerialization() public Task ObjectPrinter_ShouldHandle_WhenCustomTypeSerialization() { var person = new SimplePerson { Name = "Dima", Age = 20 }; + var printedString = printer.Serialize(person, c => c.SetCustomSerialization(_ => "Age")); + return Verify(printedString, Settings); } @@ -66,8 +76,10 @@ public Task ObjectPrinter_ShouldHandle_WhenCustomTypeSerialization() public Task ObjectPrinter_ShouldHandle_WhenSetCultureForNumber(string culture, string expectedSeparator) { var person = new SimplePerson { Name = "Dima", Age = 20, Height = 180.5 }; + var printedString = printer.Serialize(person, c => c.SetCulture(new CultureInfo(culture))); + return Verify(printedString, Settings); } @@ -76,8 +88,10 @@ public Task ObjectPrinter_ShouldHandle_WhenSetCultureForNumber(string culture, s public Task ObjectPrinter_ShouldFormatDateTime_WhenSetCultureForDate(string culture) { var person = new DateTime(2024,12,11); + var printedString = printer.Serialize(person, c => c.SetCulture(new CultureInfo(culture), "G")); + return Verify(printedString, Settings); } @@ -85,8 +99,10 @@ public Task ObjectPrinter_ShouldFormatDateTime_WhenSetCultureForDate(string cult public Task ObjectPrinter_ShouldHandle_WhenTrimString() { var person = new SimplePerson { Name = "Dima", Age = 20 }; + var printedString = printer.Serialize(person, c => c.TrimStringsToLength(2)); + return Verify(printedString, Settings); } @@ -94,8 +110,10 @@ public Task ObjectPrinter_ShouldHandle_WhenTrimString() public Task ObjectPrinter_ShouldHandle_WhenLengthIsGreaterThanTrimmed() { var person = new SimplePerson { Name = "Dima", Age = 20 }; + var printedString = printer.Serialize(person, c => c.TrimStringsToLength(10)); + return Verify(printedString, Settings); } @@ -103,8 +121,10 @@ public Task ObjectPrinter_ShouldHandle_WhenLengthIsGreaterThanTrimmed() public Task ObjectPrinter_ShouldHandle_WhenLargeCollectionWithTrim() { var largeCollection = Enumerable.Range(1, 1000).ToList(); + var printedString = printer.Serialize(largeCollection, c => c.TrimStringsToLength(50)); + return Verify(printedString, Settings); } @@ -113,7 +133,9 @@ public Task ObjectPrinter_ShouldHandle_WhenCircularReference() { var person = new PersonWithRoots { Name = "Dima", Age = 20 }; person.Root = person; + var printedString = printer.Serialize(person); + return Verify(printedString, Settings); } @@ -122,9 +144,10 @@ public Task ObjectPrinter_ShouldHandle_WhenArray() { var first = new SimplePerson { Name = "Dima", Age = 20 }; var second = new SimplePerson { Name = "Andrey", Height = 190, Age = 45 }; - SimplePerson[] persons = [first, second]; + var printedString = printer.Serialize(persons); + return Verify(printedString, Settings); } @@ -139,6 +162,7 @@ public Task ObjectPrinter_ShouldHandle_WhenList() ]; var printedString = printer.Serialize(personList); + return Verify(printedString, Settings); } @@ -153,6 +177,7 @@ public Task ObjectPrinter_ShouldHandle_WhenNestedArray() ]; var printedString = printer.Serialize(personArray); + return Verify(printedString, Settings); } @@ -167,6 +192,7 @@ public Task ObjectPrinter_ShouldHandle_WhenDictionary() }; var printedString = printer.Serialize(personDictionary); + return Verify(printedString, Settings); } @@ -174,8 +200,10 @@ public Task ObjectPrinter_ShouldHandle_WhenDictionary() public Task ObjectPrinter_ShouldHandle_WhenCustomCollectionSerialization() { var collection = new List { 1, 2, 3 }; + var printedString = printer.Serialize(collection, c => c.SetCustomSerialization>(list => string.Join(", ", list))); + return Verify(printedString, Settings); } @@ -193,20 +221,7 @@ public Task ObjectPrinter_ShouldHandle_WhenDeepNesting() }; var printedString = printer.Serialize(nested); - return Verify(printedString, Settings); - } - [Test] - public Task ObjectPrinter_ShouldHandle_WhenFaultyItemInCollection() - { - var collection = new List - { - new SimplePerson { Name = "Dima", Age = 20 }, - new FaultyType(), - new SimplePerson { Name = "Andrey", Age = 45 } - }; - - var printedString = printer.Serialize(collection); return Verify(printedString, Settings); } @@ -222,6 +237,31 @@ public Task ObjectPrinter_ShouldHandle_WhenExcessiveNesting() } var printedString = printer.Serialize(deeplyNested); + + return Verify(printedString, Settings); + } + + [Test] + public Task ObjectPrinter_ShouldHandle_WhenCustomPropertySerializationFails() + { + var person = new SimplePerson { Name = "Dima", Age = 20 }; + var m = new FaultyType(); + + var printedString = printer.Serialize(person, + c => c.SetCustomSerialization(x => x.Name, _ => m.ToString())); + + return Verify(printedString, Settings); + } + + [Test] + public Task ObjectPrinter_ShouldHandle_WhenCustomTypeSerializationFails() + { + var person = new SimplePerson { Name = "Dima", Age = 20 }; + var m = new FaultyType(); + + var printedString = printer.Serialize(person, + c => c.SetCustomSerialization(_ => m.ToString())); + return Verify(printedString, Settings); } } \ No newline at end of file From b3fe66568a5c0cdb8c4a69b63e9f41bc9aeb5bda Mon Sep 17 00:00:00 2001 From: Kostornoj-Dmitrij Date: Fri, 13 Dec 2024 17:03:07 +0500 Subject: [PATCH 11/13] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20verified=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...rinter_ShouldHandle_WhenArray.verified.txt | 6 +- ...CustomCollectionSerialization.verified.txt | 8 +- ...tomPropertySerializationFails.verified.txt | 5 + ...nCustomTypeSerializationFails.verified.txt | 5 + ...r_ShouldHandle_WhenDictionary.verified.txt | 8 +- ...e_WhenLargeCollectionWithTrim.verified.txt | 202 +++++++++--------- ...Printer_ShouldHandle_WhenList.verified.txt | 8 +- ..._ShouldHandle_WhenNestedArray.verified.txt | 8 +- 8 files changed, 130 insertions(+), 120 deletions(-) create mode 100644 ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenCustomPropertySerializationFails.verified.txt create mode 100644 ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenCustomTypeSerializationFails.verified.txt diff --git a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenArray.verified.txt b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenArray.verified.txt index 478513cf..1f8608d7 100644 --- a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenArray.verified.txt +++ b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenArray.verified.txt @@ -1,10 +1,10 @@ -SimplePerson[]: - SimplePerson +(SimplePerson[]): + (SimplePerson):SimplePerson Id = 00000000-0000-0000-0000-000000000000 Name = Dima Height = 0 Age = 20 - SimplePerson + (SimplePerson):SimplePerson Id = 00000000-0000-0000-0000-000000000000 Name = Andrey Height = 190 diff --git a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenCustomCollectionSerialization.verified.txt b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenCustomCollectionSerialization.verified.txt index 1dc8b35e..36a8c945 100644 --- a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenCustomCollectionSerialization.verified.txt +++ b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenCustomCollectionSerialization.verified.txt @@ -1,4 +1,4 @@ -List`1: - 1 - 2 - 3 +(List`1): + (Int32):1 + (Int32):2 + (Int32):3 diff --git a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenCustomPropertySerializationFails.verified.txt b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenCustomPropertySerializationFails.verified.txt new file mode 100644 index 00000000..c4e0a5ae --- /dev/null +++ b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenCustomPropertySerializationFails.verified.txt @@ -0,0 +1,5 @@ +SimplePerson + Id = 00000000-0000-0000-0000-000000000000 + Name = [Error serializing property 'Name']: Serialization error! + Height = 0 + Age = 20 diff --git a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenCustomTypeSerializationFails.verified.txt b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenCustomTypeSerializationFails.verified.txt new file mode 100644 index 00000000..c7997ff8 --- /dev/null +++ b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenCustomTypeSerializationFails.verified.txt @@ -0,0 +1,5 @@ +SimplePerson + Id = 00000000-0000-0000-0000-000000000000 + Name = [Error serializing type 'String']: Serialization error! + Height = 0 + Age = 20 diff --git a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenDictionary.verified.txt b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenDictionary.verified.txt index b810560b..efa8f507 100644 --- a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenDictionary.verified.txt +++ b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenDictionary.verified.txt @@ -1,19 +1,19 @@ -Dictionary`2: - KeyValuePair`2 +(Dictionary`2): + (KeyValuePair`2):KeyValuePair`2 Key = SimplePerson Id = 00000000-0000-0000-0000-000000000000 Name = Dima Height = 0 Age = 20 Value = first - KeyValuePair`2 + (KeyValuePair`2):KeyValuePair`2 Key = SimplePerson Id = 00000000-0000-0000-0000-000000000000 Name = Andrey Height = 0 Age = 45 Value = second - KeyValuePair`2 + (KeyValuePair`2):KeyValuePair`2 Key = SimplePerson Id = 00000000-0000-0000-0000-000000000000 Name = Vadim diff --git a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenLargeCollectionWithTrim.verified.txt b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenLargeCollectionWithTrim.verified.txt index becf4b1f..44214797 100644 --- a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenLargeCollectionWithTrim.verified.txt +++ b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenLargeCollectionWithTrim.verified.txt @@ -1,102 +1,102 @@ -List`1: - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10 - 11 - 12 - 13 - 14 - 15 - 16 - 17 - 18 - 19 - 20 - 21 - 22 - 23 - 24 - 25 - 26 - 27 - 28 - 29 - 30 - 31 - 32 - 33 - 34 - 35 - 36 - 37 - 38 - 39 - 40 - 41 - 42 - 43 - 44 - 45 - 46 - 47 - 48 - 49 - 50 - 51 - 52 - 53 - 54 - 55 - 56 - 57 - 58 - 59 - 60 - 61 - 62 - 63 - 64 - 65 - 66 - 67 - 68 - 69 - 70 - 71 - 72 - 73 - 74 - 75 - 76 - 77 - 78 - 79 - 80 - 81 - 82 - 83 - 84 - 85 - 86 - 87 - 88 - 89 - 90 - 91 - 92 - 93 - 94 - 95 - 96 - 97 - 98 - 99 - 100 +(List`1): + (Int32):1 + (Int32):2 + (Int32):3 + (Int32):4 + (Int32):5 + (Int32):6 + (Int32):7 + (Int32):8 + (Int32):9 + (Int32):10 + (Int32):11 + (Int32):12 + (Int32):13 + (Int32):14 + (Int32):15 + (Int32):16 + (Int32):17 + (Int32):18 + (Int32):19 + (Int32):20 + (Int32):21 + (Int32):22 + (Int32):23 + (Int32):24 + (Int32):25 + (Int32):26 + (Int32):27 + (Int32):28 + (Int32):29 + (Int32):30 + (Int32):31 + (Int32):32 + (Int32):33 + (Int32):34 + (Int32):35 + (Int32):36 + (Int32):37 + (Int32):38 + (Int32):39 + (Int32):40 + (Int32):41 + (Int32):42 + (Int32):43 + (Int32):44 + (Int32):45 + (Int32):46 + (Int32):47 + (Int32):48 + (Int32):49 + (Int32):50 + (Int32):51 + (Int32):52 + (Int32):53 + (Int32):54 + (Int32):55 + (Int32):56 + (Int32):57 + (Int32):58 + (Int32):59 + (Int32):60 + (Int32):61 + (Int32):62 + (Int32):63 + (Int32):64 + (Int32):65 + (Int32):66 + (Int32):67 + (Int32):68 + (Int32):69 + (Int32):70 + (Int32):71 + (Int32):72 + (Int32):73 + (Int32):74 + (Int32):75 + (Int32):76 + (Int32):77 + (Int32):78 + (Int32):79 + (Int32):80 + (Int32):81 + (Int32):82 + (Int32):83 + (Int32):84 + (Int32):85 + (Int32):86 + (Int32):87 + (Int32):88 + (Int32):89 + (Int32):90 + (Int32):91 + (Int32):92 + (Int32):93 + (Int32):94 + (Int32):95 + (Int32):96 + (Int32):97 + (Int32):98 + (Int32):99 + (Int32):100 ... (truncated) \ No newline at end of file diff --git a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenList.verified.txt b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenList.verified.txt index 2f950a8f..ad94b8cc 100644 --- a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenList.verified.txt +++ b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenList.verified.txt @@ -1,15 +1,15 @@ -List`1: - SimplePerson +(List`1): + (SimplePerson):SimplePerson Id = 00000000-0000-0000-0000-000000000000 Name = Dima Height = 0 Age = 20 - SimplePerson + (SimplePerson):SimplePerson Id = 00000000-0000-0000-0000-000000000000 Name = Andrey Height = 0 Age = 45 - SimplePerson + (SimplePerson):SimplePerson Id = 00000000-0000-0000-0000-000000000000 Name = Vadim Height = 0 diff --git a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenNestedArray.verified.txt b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenNestedArray.verified.txt index 81d66dfc..d4597b8a 100644 --- a/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenNestedArray.verified.txt +++ b/ObjectPrintingTests/TestVerifiedFiles/ObjectPrinter_Should.ObjectPrinter_ShouldHandle_WhenNestedArray.verified.txt @@ -1,15 +1,15 @@ -SimplePerson[]: - SimplePerson +(SimplePerson[]): + (SimplePerson):SimplePerson Id = 00000000-0000-0000-0000-000000000000 Name = Dima Height = 0 Age = 20 - SimplePerson + (SimplePerson):SimplePerson Id = 00000000-0000-0000-0000-000000000000 Name = Andrey Height = 0 Age = 45 - SimplePerson + (SimplePerson):SimplePerson Id = 00000000-0000-0000-0000-000000000000 Name = Vadim Height = 0 From 2972683ebc8ecc87d21e370d94f29b026381ed5d Mon Sep 17 00:00:00 2001 From: Kostornoj-Dmitrij Date: Fri, 13 Dec 2024 17:10:10 +0500 Subject: [PATCH 12/13] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BE=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=BA=D1=83?= =?UTF-8?q?=20=D0=B8=D1=81=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD=D0=B8=D0=B9?= =?UTF-8?q?,=20=D1=81=D0=B5=D1=80=D0=B8=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8E=20=D1=82=D0=B8=D0=BF=D0=BE=D0=B2=20=D0=BE?= =?UTF-8?q?=D0=B1=D1=8A=D0=B5=D0=BA=D1=82=D0=BE=D0=B2=20=D0=BA=D0=BE=D0=BB?= =?UTF-8?q?=D0=BB=D0=B5=D0=BA=D1=86=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ObjectPrinting/ObjectPrinterSettings.cs | 50 ++++++++++++++++--------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/ObjectPrinting/ObjectPrinterSettings.cs b/ObjectPrinting/ObjectPrinterSettings.cs index bfa9e051..31e6ea3d 100644 --- a/ObjectPrinting/ObjectPrinterSettings.cs +++ b/ObjectPrinting/ObjectPrinterSettings.cs @@ -40,14 +40,34 @@ public ObjectPrinterSettings Exclude(Expression> prop public ObjectPrinterSettings SetCustomSerialization(Func serializer) { - typeSerializers[typeof(T)] = obj => serializer((T)obj); + typeSerializers[typeof(T)] = obj => + { + try + { + return serializer((T)obj); + } + catch (Exception ex) + { + return $"[Error serializing type '{typeof(T).Name}']: {ex.Message}"; + } + }; return this; } public ObjectPrinterSettings SetCustomSerialization(Expression> propertyExpression, Func serializer) { var propertyName = GetPropertyInfo(propertyExpression); - propertySerializers[propertyName] = obj => serializer((T)obj); + propertySerializers[propertyName] = obj => + { + try + { + return serializer((T)obj); + } + catch (Exception ex) + { + return $"[Error serializing property '{propertyName.Name}']: {ex.Message}"; + } + }; return this; } @@ -89,7 +109,7 @@ private bool TrySerializeCollection(object obj, int nestingLevel, out string? co var pad = new string(IndentChar, nestingLevel + 1); var count = 0; - text.AppendLine($"{obj.GetType().Name}:"); + text.AppendLine($"({obj.GetType().Name}):"); foreach (var item in enumerable) { if (count++ >= MaxCollectionItems) @@ -98,7 +118,9 @@ private bool TrySerializeCollection(object obj, int nestingLevel, out string? co break; } - text.Append($"{pad}{PrintToString(item, nestingLevel + 1)}"); + var elementType = item?.GetType().Name ?? "unknown"; + + text.Append($"{pad}({elementType}):{PrintToString(item, nestingLevel + 1)}"); } collectionResult = text.ToString(); @@ -125,7 +147,6 @@ IFormattable formattable when typeCulture.ContainsKey(type) => result = AppendNewLine(result); return true; } - result = null; return false; } @@ -167,20 +188,15 @@ private string SerializeComplexType(object obj, int nestingLevel) private string? SerializeProperty(PropertyInfo property, object? value, int nestingLevel) { - try - { - if (propertySerializers.TryGetValue(property, out var propertySerializer)) - return AppendNewLine(propertySerializer(value!)); - if (typeSerializers.TryGetValue(property.PropertyType, out var typeSerializer)) - return AppendNewLine(typeSerializer(value!)); + if (propertySerializers.TryGetValue(property, out var propertySerializer)) + return AppendNewLine(propertySerializer(value!)); - return PrintToString(value, nestingLevel); - } - catch (Exception ex) - { - return AppendNewLine($"[Error serializing {property.Name}: {ex.Message}]"); - } + if (typeSerializers.TryGetValue(property.PropertyType, out var typeSerializer)) + return AppendNewLine(typeSerializer(value!)); + + return PrintToString(value, nestingLevel); + } private static MemberInfo GetPropertyInfo(Expression> propertyExpression) From 5212f0eea199c96a9c63c63da6ae4a9c9dd1336b Mon Sep 17 00:00:00 2001 From: Kostornoj-Dmitrij Date: Fri, 13 Dec 2024 17:10:44 +0500 Subject: [PATCH 13/13] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BA=D0=BE=D0=BD=D0=BA=D0=B0=D1=82=D0=B5=D0=BD=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8E=20=D0=BD=D0=B0=20=D0=B8=D1=81=D0=BF=D0=BE?= =?UTF-8?q?=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20String?= =?UTF-8?q?Builder?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ObjectPrinting/ObjectPrinterSettings.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/ObjectPrinting/ObjectPrinterSettings.cs b/ObjectPrinting/ObjectPrinterSettings.cs index 31e6ea3d..02b4bf0e 100644 --- a/ObjectPrinting/ObjectPrinterSettings.cs +++ b/ObjectPrinting/ObjectPrinterSettings.cs @@ -206,6 +206,14 @@ private static MemberInfo GetPropertyInfo(Expression> propert return member.Member; } - private static string AppendNewLine(string? text) => - text != null && text.EndsWith(Environment.NewLine) ? text : text + Environment.NewLine; + private static string AppendNewLine(string? text) + { + if (text != null && text.EndsWith(Environment.NewLine)) + return text; + + var result = new StringBuilder(text); + result.Append(Environment.NewLine); + + return result.ToString(); + } } \ No newline at end of file