static void Main(string[] args) { RootDescriptor <Person> rootDesc = GetPersonDescriptor(); var czechRepublic = new Country { Name = "Czech Republic", AreaCode = 420 }; var person = new Person { FirstName = "Pavel", LastName = "Jezek", HomeAddress = new Address { Street = "Patkova", City = "Prague" }, WorkAddress = new Address { Street = "Malostranske namesti", City = "Prague" }, CitizenOf = czechRepublic, MobilePhone = new PhoneNumber { Country = czechRepublic, Number = 123456789 } }; rootDesc.Serialize(Console.Out, person); }
static RootDescriptor <Person> GetPersonDescriptor() { //Address var addressDes = new RootDescriptor <Address>("Address"); addressDes.AddNewDescriptor(new SimpleTypeDescriptor <Address, string>("Street", a => a.Street)); addressDes.AddNewDescriptor(new SimpleTypeDescriptor <Address, string>("City", a => a.City)); //Country var countryDes = new RootDescriptor <Country>("Country"); countryDes.AddNewDescriptor(new SimpleTypeDescriptor <Country, string>("Name", c => c.Name)); countryDes.AddNewDescriptor(new SimpleTypeDescriptor <Country, int>("AreaCode", c => c.AreaCode)); //PhoneNumber var phoneNumberDesc = new RootDescriptor <PhoneNumber>("PhoneNumber"); phoneNumberDesc.AddNewDescriptor(new SimpleTypeDescriptor <PhoneNumber, int>("Number", pn => pn.Number)); phoneNumberDesc.AddNewDescriptor(new ComplexPropertyDescriptor <PhoneNumber, Country>("Country", countryDes, pn => pn.Country)); //Person var personDesc = new RootDescriptor <Person>("Person"); personDesc.AddNewDescriptor(new SimpleTypeDescriptor <Person, string>("FirstName", p => p.FirstName)); personDesc.AddNewDescriptor(new SimpleTypeDescriptor <Person, string>("LastName", p => p.LastName)); personDesc.AddNewDescriptor(new ComplexPropertyDescriptor <Person, Address>("HomeAddress", addressDes, p => p.HomeAddress)); personDesc.AddNewDescriptor(new ComplexPropertyDescriptor <Person, Address>("WorkAddress", addressDes, p => p.WorkAddress)); personDesc.AddNewDescriptor(new ComplexPropertyDescriptor <Person, Country>("CitizenOf", countryDes, p => p.CitizenOf)); personDesc.AddNewDescriptor(new ComplexPropertyDescriptor <Person, PhoneNumber>("MobilePhone", phoneNumberDesc, p => p.MobilePhone)); return(personDesc); }
//Follow a set of methods serving as a configuration for a serializer /// <summary> /// returns a description of Person /// </summary> /// <returns></returns> static RootDescriptor <Person> GetPersonDescriptor() { //Create delegates to functions describing Person fields //functions are to be called from the Serialize function //descritors for each field nested anywhere in Person object is recursivelly created and .Serialize() is called on each descriptor GetStringRepresentation <Person> FirstName = new GetStringRepresentation <Person>((Person person, StringBuilder builder) => { GetStringDescriptor().Serialize(person.FirstName, nameof(person.FirstName), builder); }); GetStringRepresentation <Person> LastName = new GetStringRepresentation <Person>((Person person, StringBuilder builder) => { GetStringDescriptor().Serialize(person.LastName, nameof(person.LastName), builder); }); GetStringRepresentation <Person> HomeAddress = new GetStringRepresentation <Person>((Person person, StringBuilder builder) => { GetAddressDescriptor().Serialize(person.HomeAddress, nameof(person.HomeAddress), builder); }); GetStringRepresentation <Person> WorkAddress = new GetStringRepresentation <Person>((Person person, StringBuilder builder) => { GetAddressDescriptor().Serialize(person.WorkAddress, nameof(person.WorkAddress), builder); }); GetStringRepresentation <Person> CitizenOf = new GetStringRepresentation <Person>((Person person, StringBuilder builder) => { GetCountryDescriptor().Serialize(person.CitizenOf, nameof(person.CitizenOf), builder); }); GetStringRepresentation <Person> MobilePhone = new GetStringRepresentation <Person>((Person person, StringBuilder builder) => { GetPhoneNumberDescriptor().Serialize(person.MobilePhone, nameof(person.MobilePhone), builder); }); //create new person descriptor with coresponding array of delegates RootDescriptor <Person> rootDesc = new RootDescriptor <Person>(new GetStringRepresentation <Person>[] { FirstName, LastName, HomeAddress, WorkAddress, CitizenOf, MobilePhone }); return(rootDesc); //no Person instance is passed directly to GetDescriptor() functions - Descriptors can be created independentally of a particular instance //Person instance is passed to Serialize function when called, then propageted thru corresponding delegates to recursive serialize calls }
static RootDescriptor <Person> GetPersonDescriptor() { string ownName = nameof(Person); RootDescriptor <String> stringDesc = GetStringDescriptor(); RootDescriptor <Int32> int32Desc = GetInt32Descriptor(); RootDescriptor <Address> adressDesc = GetAdressDescriptor(); RootDescriptor <Country> countryDesc = GetCountryDescriptor(); RootDescriptor <PhoneNumber> phoneNumberDesc = GetPhoneNumberDescriptor(); SerializerHelperPointingToVariableValue <Person>[] variables = new SerializerHelperPointingToVariableValue <Person>[] { new SerializerHelperPointingToVariableValue <Person>(FirstName), new SerializerHelperPointingToVariableValue <Person>(LastName), new SerializerHelperPointingToVariableValue <Person>(HomeAddress), new SerializerHelperPointingToVariableValue <Person>(WorkAddress), new SerializerHelperPointingToVariableValue <Person>(CitizenOf), new SerializerHelperPointingToVariableValue <Person>(MobilePhone), }; string FirstName(Person person) => stringDesc.Serialize(nameof(FirstName), person.FirstName); string LastName(Person person) => stringDesc.Serialize(nameof(LastName), person.LastName); string HomeAddress(Person person) => adressDesc.Serialize(nameof(HomeAddress), person.HomeAddress); string WorkAddress(Person person) => adressDesc.Serialize(nameof(WorkAddress), person.WorkAddress); string CitizenOf(Person person) => countryDesc.Serialize(nameof(CitizenOf), person.CitizenOf); string MobilePhone(Person person) => phoneNumberDesc.Serialize(nameof(MobilePhone), person.MobilePhone); return(new RootDescriptor <Person>(ownName, variables)); }
static void Main(string[] args) { RootDescriptor <Person> rootDesc = GetPersonDescriptor(); var czechRepublic = new Country { Name = "Czech Republic", AreaCode = 420 }; var person = new Person { FirstName = "David", LastName = "Napravnik", HomeAddress = new Address { Street = "Vetrna", City = "Predboj" }, WorkAddress = new Address { Street = "U kastanu", City = "Prague" }, CitizenOf = czechRepublic, MobilePhone = new PhoneNumber { Country = czechRepublic, Number = 987654321 } }; rootDesc.Serialize(Console.Out, person); Console.ReadKey(); }
static RootDescriptor <Int32> GetInt32Descriptor() { string ownName = nameof(Int32); SerializerHelperPointingToVariableValue <Int32>[] variables = new SerializerHelperPointingToVariableValue <Int32>[] { }; var rootDesc = new RootDescriptor <Int32>(ownName, variables); return(rootDesc); }
static RootDescriptor <String> GetStringDescriptor() { string ownName = nameof(String); SerializerHelperPointingToVariableValue <String>[] variables = new SerializerHelperPointingToVariableValue <String>[] { }; var rootDesc = new RootDescriptor <String>(ownName, variables); return(rootDesc); }
static RootDescriptor <Address> GetAdressDescriptor() { string ownName = nameof(Address); RootDescriptor <String> stringDesc = GetStringDescriptor(); SerializerHelperPointingToVariableValue <Address>[] variables = new SerializerHelperPointingToVariableValue <Address>[] { new SerializerHelperPointingToVariableValue <Address>(Street), new SerializerHelperPointingToVariableValue <Address>(City), }; string Street(Address address) => stringDesc.Serialize(nameof(Street), address.Street); string City(Address address) => stringDesc.Serialize(nameof(City), address.City); return(new RootDescriptor <Address>(ownName, variables)); }
static RootDescriptor <Country> GetCountryDescriptor() { string ownName = nameof(Country); RootDescriptor <String> stringDesc = GetStringDescriptor(); RootDescriptor <Int32> int32Desc = GetInt32Descriptor(); SerializerHelperPointingToVariableValue <Country>[] variables = new SerializerHelperPointingToVariableValue <Country>[] { new SerializerHelperPointingToVariableValue <Country>(Name), new SerializerHelperPointingToVariableValue <Country>(AreaCode), }; string Name(Country country) => stringDesc.Serialize(nameof(Name), country.Name); string AreaCode(Country country) => int32Desc.Serialize(nameof(AreaCode), country.AreaCode); return(new RootDescriptor <Country>(ownName, variables)); }
static RootDescriptor <PhoneNumber> GetPhoneNumberDescriptor() { string ownName = nameof(PhoneNumber); RootDescriptor <Country> adressDesc = GetCountryDescriptor(); RootDescriptor <Int32> int32Desc = GetInt32Descriptor(); SerializerHelperPointingToVariableValue <PhoneNumber>[] variables = new SerializerHelperPointingToVariableValue <PhoneNumber>[] { new SerializerHelperPointingToVariableValue <PhoneNumber>(Country), new SerializerHelperPointingToVariableValue <PhoneNumber>(Number), }; string Country(PhoneNumber phoneNumber) => adressDesc.Serialize(nameof(Country), phoneNumber.Country); string Number(PhoneNumber phoneNumber) => int32Desc.Serialize(nameof(Number), phoneNumber.Number); return(new RootDescriptor <PhoneNumber>(ownName, variables)); }
static RootDescriptor <Person> GetPersonDescriptor() { var rootDesc = new RootDescriptor <Person>(); return(rootDesc); }