public static string PrintToString <T>(this T obj,
                                               Func <PrintingConfig <T>, PrintingConfig <T> > configPrinter)
        {
            var printer = configPrinter(ObjectPrinter.For <T>());

            return(printer.PrintToString(obj));
        }
Example #2
0
        /// <summary>
        /// Serialize object with configuration
        /// </summary>
        /// <typeparam name="T">object type</typeparam>
        /// <param name="obj">this object</param>
        /// <param name="configuratePrinter">function wchich include and configurate serializator</param>
        /// <returns>serializarion in string</returns>
        public static string Serialize <T>(this T obj, Action <PrintingConfig <T> > configuratePrinter)
        {
            var printer = ObjectPrinter.For <T>();

            configuratePrinter(printer);
            return(printer.PrintToString(obj));
        }
Example #3
0
        public static string PrintToString <TOwner>(this TOwner obj,
                                                    Func <IPrintingConfig <TOwner>, IPrintingConfig <TOwner> > config)
        {
            var printer = config(ObjectPrinter.For <TOwner>());

            return(printer.PrintToString(obj));
        }
Example #4
0
        public static void Main()
        {
            var person = new Person {
                Name = "Alex", Age = 20
            };

            var printer = ObjectPrinter.For <Person>()
                          //1. Исключить из сериализации свойства определенного типа
                          .Excluding <Guid>()
                          //2. Указать альтернативный способ сериализации для определенного типа
                          .Printing <int>().Using(i => i.ToString())
                          //3. Для числовых типов указать культуру
                          .Printing <int>().Using(CultureInfo.InvariantCulture)
                          //4. Настроить сериализацию конкретного свойства
                          //5. Настроить обрезание строковых свойств (метод должен быть виден только для строковых свойств)
                          .Printing(p => p.Name).TrimmedToLength(2)
                          //6. Исключить из сериализации конкретного свойства
                          .Excluding(p => p.Age);

            string s1 = printer.PrintToString(person);
            string s2 = person.PrintToString();

            Console.WriteLine(s1);
            Console.WriteLine(s2);
        }
Example #5
0
        public static void Main()
        {
            var person = new Person {
                Name = "Alex", Age = 19, Parent = new Person()
                {
                    Name = "Sanny"
                }
            };

            var printer = ObjectPrinter.For <Person>()
                          //1. Исключить из сериализации свойства определенного типа
                          //.ExcludeType<int>()
                          //2. Указать альтернативный способ сериализации для определенного типа
                          .Printing(t => t.Age).Using(t => t + " yo")
                          .Printing <string>().Using(2);

            //3. Для числовых типов указать культуру
            //.Printing<int>().UsingCulture(new CultureInfo("en"))
            //4. Настроить сериализацию конкретного свойства
            //.Printing(t => t.Age).Using(t => t.ToString())
            //5. Настроить обрезание строковых свойств (метод должен быть виден только для строковых свойств)
            //.Printing<string>().Using(5)
            //6. Исключить из сериализации конкретного свойства
            //.ExcludeProperty(t => t.Age)
            //.ExcludeType<double>()
            //.Printing(t => t.Name).Using(t => t + " is dead");

            Console.Write(printer.PrintToString(person));
        }
Example #6
0
        public void Should_ExcludeByTypeCorrectly()
        {
            var printer = ObjectPrinter.For <Person>()
                          .Excluding <int>();
            var printing = printer.PrintToString(person);
            var expected = "Person" + Environment.NewLine +
                           $"\tId = {person.Id}" + Environment.NewLine +
                           $"\tName = {person.Name}" + Environment.NewLine +
                           $"\tHeight = {person.Height}" + Environment.NewLine;

            printing.Should().BeEquivalentTo(expected);
        }
Example #7
0
        public void Should_TrimStringsCorrectly()
        {
            var printer = ObjectPrinter.For <Person>()
                          .Printing(p => p.Name)
                          .TrimmedToLength(2);
            var printing = printer.PrintToString(person);
            var expected = "Person" + Environment.NewLine +
                           $"\tId = {person.Id}" + Environment.NewLine +
                           $"\tName = Jo" + Environment.NewLine +
                           $"\tHeight = {person.Height}" + Environment.NewLine +
                           $"\tAge = {person.Age}" + Environment.NewLine;

            printing.Should().BeEquivalentTo(expected);
        }
Example #8
0
        public void Should_SerializeByPropertyCorrectly()
        {
            var printer = ObjectPrinter.For <Person>()
                          .Printing(p => p.Name)
                          .Using(name => name.ToLower());
            var printing = printer.PrintToString(person);
            var expected = "Person" + Environment.NewLine +
                           $"\tId = {person.Id}" + Environment.NewLine +
                           $"\tName = {person.Name.ToLower()}" + Environment.NewLine +
                           $"\tHeight = {person.Height}" + Environment.NewLine +
                           $"\tAge = {person.Age}" + Environment.NewLine;

            printing.Should().BeEquivalentTo(expected);
        }
Example #9
0
        public void Should_ChangeCultureInfoCorrectly()
        {
            var printer = ObjectPrinter.For <Person>()
                          .Printing <double>()
                          .Using(CultureInfo.InstalledUICulture);
            var printing = printer.PrintToString(person);
            var expected = "Person" + Environment.NewLine +
                           $"\tId = {person.Id}" + Environment.NewLine +
                           $"\tName = {person.Name}" + Environment.NewLine +
                           $"\tHeight = {person.Height.ToString(CultureInfo.InstalledUICulture)}" +
                           Environment.NewLine +
                           $"\tAge = {person.Age}" + Environment.NewLine;

            printing.Should().BeEquivalentTo(expected);
        }
Example #10
0
 public static string PrintToString <T>(this T obj, Func <PrintingConfig <T>, PrintingConfig <T> > func)
 {
     return(func(ObjectPrinter.For <T>()).GetStringRepresentation(obj));
 }
Example #11
0
 public static string GetDefaultSerializ(this Person person)
 {
     return(ObjectPrinter.For <Person>().PrintToString(person));
 }
Example #12
0
 public static string PrintToString <T>(this T printedObject)
 {
     return(ObjectPrinter.For <T>().PrintToString(printedObject));
 }
Example #13
0
 public static string PrintToString <TOwner>
     (this TOwner obj, Func <PrintingConfig <TOwner>, PrintingConfig <TOwner> > printConfig)
 {
     return(printConfig(ObjectPrinter.For <TOwner>()).PrintToString(obj));
 }
Example #14
0
        /// <summary>
        /// Serialize object
        /// </summary>
        /// <typeparam name="T">type</typeparam>
        /// <param name="obj">this object</param>
        /// <returns>serializarion in string</returns>
        public static string Serialize <T>(this T obj)
        {
            var printer = ObjectPrinter.For <T>();

            return(printer.PrintToString(obj));
        }
 public static string PrintToString <T>(this T obj, Func <PrintingConfig <T>, PrintingConfig <T> > configFunc) => configFunc(ObjectPrinter.For <T>()).PrintToString(obj);
Example #16
0
 public static string PrintToString <T>(this T serializableObject)
 {
     return(ObjectPrinter.For <object>().PrintToString(serializableObject));
 }
Example #17
0
 public static string PrintToString <T>(this T obj)
 {
     return(ObjectPrinter <T> .Should().Build().PrintToString(obj));
 }
Example #18
0
 public static string PrintToString <T>(this T obj)
 {
     return(ObjectPrinter.For <T>().GetStringRepresentation(obj));
 }
Example #19
0
 public static string PrintToString <T>(this T obj)
 {
     return(ObjectPrinter.For <T>().PrintToString(obj));
 }
 public static string PrintToString <T>(this T obj) => ObjectPrinter.For <T>().PrintToString(obj);
 public static string PrintToString <T>(this T obj, Func <PrintingConfig <T>, PrintingConfig <T> > config)
 {
     return(config(ObjectPrinter.For <T>()).PrintToString(obj));
 }
Example #22
0
 public static string PrintToString <T>(this T serializableObject,
                                        Func <PrintingConfig <T>, PrintingConfig <T> > configurationFunction)
 {
     return(configurationFunction(ObjectPrinter.For <T>()).PrintToString(serializableObject));
 }