public void LongValue() { var serializer = new CSharpObjectSerializer(); var code = serializer.Serialize(789L); Assert.AreEqual(@"var instance = 789L;", code.Instance); }
public void InterceptTest() { var list = GetSimpleList(); var serializer = new CSharpObjectSerializer(); serializer.Converters.Add(new NameConverter()); var code = serializer.Serialize(list).Initialiser; Assert.AreEqual(@"new List<Person> { new Person { Name = ""Paulina (age: 24)"", Age = 24, Addresses = new List<Address> { new Address { Street = ""My house"", PostCode = ""AB1 C23"" }, new Address { Street = ""My other house"", PostCode = ""DE2 F34"" } } }, new Person { Name = ""Josh (age: 33)"", Age = 33, Addresses = new List<Address> { new Address { Street = ""Big Place"", PostCode = ""XY4 Z56"" } } }, new Person { Name = ""Bob (age: 33)"", Age = 33 } }", code); }
public void InterceptNullTest() { var person = new Person(); person.Age = 47; var serializer = new CSharpObjectSerializer(); serializer.Converters.Add(new NameConverter()); var code = serializer.Serialize(person).Initialiser; Assert.AreEqual(@"new Person { Name = "" (age: 47)"", Age = 47, Addresses = new List<Address>() }", code); }
static void Main(string[] args) { var list = new List <Person>(); var person1 = new Person("Paulina", 24, new DateTimeOffset(1993, 7, 10, 12, 33, 22, new TimeSpan())); var person2 = new Person("Josh", 33, new DateTimeOffset(1984, 7, 2, 1, 15, 5, new TimeSpan())); var person3 = new Person("Bob", 33, new DateTimeOffset(1988, 10, 11, 18, 47, 12, new TimeSpan())); person3.FavouritePerson = person2; person1.FavouritePerson = person1; person1.Addresses.Add(new Address("My house", "AB1 C23")); person1.Addresses.Add(new Address("My other house", "DE2 F34")); person2.Addresses.Add(new Address("Big Place", "XY4 Z56")); person2.Addresses.Add(new Address("Another Place", null)); person3.Addresses = null; list.Add(person1); list.Add(person2); list.Add(person3); //var options = new CSharpSerializeToClassParameters("InMemoryDb"); var options = new CSharpSerializerParameters("MyClass"); options.AllowObjectInitializer = false; var serializer = new CSharpObjectSerializer(options); options.IgnoreConditions.Add(new IgnoreCondition((o, info) => { if (info.Name == nameof(Person.IgnoreThis)) { return(true); } return(false); })); var code = serializer.Serialize(list); Console.WriteLine(code.Class); File.WriteAllText(@"d:\code\temp-formatted.cs", code.Class); }
public void TestComments() { var list = GetSimpleList(); var options = new CSharpSerializerParameters(); options.DescriptionFormatter.SetForType <List <Person> >( (list2, args) => { return("Hello"); }); var serializer = new CSharpObjectSerializer(options); options.BeforeInstanceComment = "Before instance comment"; options.BeforeInitialiserComment = "Before initialiser comment"; options.AfterInstanceComment = "After instance comment"; options.AfterInitialiserComment = "After initialiser comment"; var code = serializer.Serialize(list).Class; Assert.AreEqual(@"using Brandless.ObjectSerializer.Tests.Models; using System; using System.Collections.Generic; public class SerializedObject { public List<Person>GetData() { // Before instance comment var instance = /* Before initialiser comment */ new List<Person> { new Person { Name = ""Paulina"", Age = 24, Addresses = new List<Address> { new Address { Street = ""My house"", PostCode = ""AB1 C23"" }, new Address { Street = ""My other house"", PostCode = ""DE2 F34"" } } }, new Person { Name = ""Josh"", Age = 33, Addresses = new List<Address> { new Address { Street = ""Big Place"", PostCode = ""XY4 Z56"" } } }, new Person { Name = ""Bob"", Age = 33 } } /* After initialiser comment */ ; // After instance comment return instance; } }", code); }