Esempio n. 1
0
//        public static void RunAutofacConfig()
//        {
//            Program.PrintHeader("Serialization reference object - autofac config");
//
//            var builder = new ContainerBuilder();
//            builder.RegisterModule<AutofacExtendedXmlSerializerModule>();
//            builder.RegisterType<PersonConfig>().As<ExtendedXmlSerializerConfig<Person>>().SingleInstance();
//            var containter = builder.Build();
//
//            var serializer = containter.Resolve<IExtendedXmlSerializer>();
//            Run(serializer);
//        }

        static void Run(IExtendedXmlSerializer serializer)
        {
// CreateObject

            Person boss = new Person {
                Id = 1, Name = "John"
            };

            boss.Boss = boss; //himself boss
            Person worker = new Person {
                Id = 2, Name = "Oliver"
            };

            worker.Boss = boss;
            Company obj = new Company
            {
                Employees = new List <Person>
                {
                    worker,
                    boss
                }
            };
// EndCreateObject
            string xml = serializer.Serialize(new XmlWriterSettings {
                Indent = true
            }, obj);

            File.WriteAllText("bin\\ObjectReferenceSamples.xml", xml);
            Console.WriteLine(xml);

            Company obj2 = serializer.Deserialize <Company>(xml);

            Console.WriteLine("Employees count = " + obj2.Employees.Count);
        }
        private static void Run(IExtendedXmlSerializer serializer)
        {
            var boss = new Person {
                Id = 1, Name = "John"
            };

            boss.Boss = boss; //himself boss
            var worker = new Person {
                Id = 2, Name = "Oliver"
            };

            worker.Boss = boss;
            var obj = new Company
            {
                Employees = new List <Person>
                {
                    worker,
                    boss
                }
            };

            var xml = serializer.Serialize(obj);

            Console.WriteLine(xml);

            var obj2 = serializer.Deserialize <Company>(xml);

            Console.WriteLine("Employees count = " + obj2.Employees.Count);
        }
Esempio n. 3
0
//////////////////////////////////////////////////////////////////////////////
/// Autofac configuration
//////////////////////////////////////////////////////////////////////////////
        // ConfigureServices is where you register dependencies. This gets
        // called by the runtime before the Configure method, below.
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            // Add services to the collection.
            services.AddMvc(options =>
            {
                options.RespectBrowserAcceptHeader = true; // false by default

                //Resolve ExtendedXmlSerializer
                IExtendedXmlSerializer serializer = ApplicationContainer.Resolve <IExtendedXmlSerializer>();

                //Add ExtendedXmlSerializer's formatter
                options.OutputFormatters.Add(new ExtendedXmlSerializerOutputFormatter(serializer));
                options.InputFormatters.Add(new ExtendedXmlSerializerInputFormatter(serializer));
            });

            // Create the container builder.
            var builder = new ContainerBuilder();

            // Register dependencies, populate the services from
            // the collection, and build the container. If you want
            // to dispose of the container at the end of the app,
            // be sure to keep a reference to it as a property or field.
            builder.Populate(services);
            builder.RegisterModule <AutofacExtendedXmlSerializerModule>();
            builder.RegisterType <TestClassConfig>().As <ExtendedXmlSerializerConfig <TestClass> >().SingleInstance();
            this.ApplicationContainer = builder.Build();

            // Create the IServiceProvider based on the container.
            return(new AutofacServiceProvider(this.ApplicationContainer));
        }
 public XmlDeviceRepository(Uri uri, IDownloader downloader)
 {
     this.uri        = uri;
     this.downloader = downloader;
     this.serializer = new ConfigurationContainer()
                       .Type <Device>().EnableReferences(x => x.Id)
                       .Create();
 }
 /// <summary>
 /// Initializes a new instance of <see cref="T:Microsoft.AspNetCore.Mvc.Formatters.ExtendedXmlSerializerOutputFormatter" />
 /// </summary>
 /// <param name="serializer">The serializer <see cref="IExtendedXmlSerializer"/></param>
 public ExtendedXmlSerializerOutputFormatter(IExtendedXmlSerializer serializer)
 {
     _serializer = serializer ?? new ExtendedXmlSerializer();
     SupportedEncodings.Add(Encoding.UTF8);
     SupportedEncodings.Add(Encoding.Unicode);
     SupportedMediaTypes.Add(MediaTypeHeaderValues.ApplicationXml);
     SupportedMediaTypes.Add(MediaTypeHeaderValues.TextXml);
 }
        public ExtendedXmlSerializerInputFormatter(IExtendedXmlSerializer serializer)
        {
            _serializer = serializer ?? new ExtendedXmlSerializer();

            SupportedEncodings.Add(UTF8EncodingWithoutBOM);
            SupportedEncodings.Add(UTF16EncodingLittleEndian);
            SupportedMediaTypes.Add(MediaTypeHeaderValues.ApplicationXml);
            SupportedMediaTypes.Add(MediaTypeHeaderValues.TextXml);
        }
Esempio n. 7
0
 public GenericSerializer()
 {
     serializer = new ConfigurationContainer()
                  .EnableAllConstructors()
                  .EnableParameterizedContentWithPropertyAssignments()
                  .UseOptimizedNamespaces()
                  .EnableReferences()
                  .UseOptimizedNamespaces()
                  .Create();
 }
Esempio n. 8
0
        public static string GetXmlFromObject(object obj)
        {
            IExtendedXmlSerializer serializer = DefaultXmlSerializer();

            var document = serializer.Serialize(new XmlWriterSettings {
                Indent = true
            },
                                                obj);

            return(document);
        }
Esempio n. 9
0
        private static void Run(IExtendedXmlSerializer serializer)
        {
            var obj = new TestClass("Value");
            var xml = serializer.Serialize(obj);

            Console.WriteLine(xml);

            var obj2 = serializer.Deserialize <TestClass>(xml);

            Console.WriteLine("Obiect PropStr = " + obj2.PropStr);
        }
//        public static void RunAutofacConfig()
//        {
//            Program.PrintHeader("Custom serialization - autofac config");
//
//            var builder = new ContainerBuilder();
//            builder.RegisterModule<AutofacExtendedXmlSerializerModule>();
//            builder.RegisterType<TestClassConfig>().As<ExtendedXmlSerializerConfig<TestClass>>().SingleInstance();
//            var containter = builder.Build();
//
//            var serializer = containter.Resolve<IExtendedXmlSerializer>();
//            Run(serializer);
//        }

        static void Run(IExtendedXmlSerializer serializer)
        {
            TestClass obj = new TestClass("Value", 1);
            string    xml = serializer.Serialize(obj);

            Console.WriteLine(xml);

            TestClass obj2 = serializer.Deserialize <TestClass>(xml);

            Console.WriteLine("Obiect PropStr = " + obj2.PropStr);
            Console.WriteLine("Obiect PropStr = " + obj2.PropInt);
        }
 public UserAppService(IRepository <User> userRepository, IRepository <UserInfo> userInfoRepository, IRepository <Role> roleRepository, IRepository <UserRole> userRoleRepository, IRepository <RolePermission> rolePermissionRepository, IRepository <Permission> permissionRepository, IRepository <Department> departmentReposiotry, IRepository <UserDepartment> userDepartmentReposiotry, ICache cache, IExtendedXmlSerializer serializer)
 {
     _userRepository           = userRepository;
     _userInfoRepository       = userInfoRepository;
     _roleRepository           = roleRepository;
     _userRoleRepository       = userRoleRepository;
     _rolePermissionRepository = rolePermissionRepository;
     _permissionRepository     = permissionRepository;
     _departmentReposiotry     = departmentReposiotry;
     _userDepartmentReposiotry = userDepartmentReposiotry;
     //_redisCache = cache;
     _serializer = serializer;
 }
Esempio n. 12
0
        void VerifyComposite()
        {
            IConfigurationContainer container = ConfiguredContainer.New <ComplexProfile>();

            IExtendedXmlSerializer serializer = container.UseAutoFormatting()
                                                .EnableImplicitTyping(typeof(Subject))
                                                .UseOptimizedNamespaces()
                                                .Create();
            var instance = new Subject {
                Message = "Hello World!", Number = 123
            };
            string document = serializer.Serialize(instance);

            document.Should()
            .Be(@"<?xml version=""1.0"" encoding=""utf-8""?><Issue282Tests_Profiles-Subject NewNumber=""123"" NewMessage=""Hello World!"" />");
        }
Esempio n. 13
0
        private static void Run(IExtendedXmlSerializer serializer)
        {
            var list = new List <Person>
            {
                new Person {
                    Name = "John", Password = "******"
                },
                new Person {
                    Name = "Oliver", Password = "******"
                }
            };

            var xml = serializer.Serialize(list);

            Console.WriteLine(xml);

            var obj2 = serializer.Deserialize <List <Person> >(xml);

            Console.WriteLine("Employees count = " + obj2.Count + " - passwords " + string.Join(", ", obj2.Select(p => p.Password)));
        }
Esempio n. 14
0
        private static void Run(IExtendedXmlSerializer serializer)
        {
            var xml =
                @"<?xml version=""1.0"" encoding=""utf-8""?>
<TestClass type=""ExtendedXmlSerialization.Samples.MigrationMap.TestClass"">
<Id>1</Id>
<Type>Type</Type>
</TestClass>";

            Console.WriteLine(xml);
            var obj = serializer.Deserialize <TestClass>(xml);

            Console.WriteLine("Obiect Id = " + obj.Id);
            Console.WriteLine("Obiect Name = " + obj.Name);
            Console.WriteLine("Obiect Value = " + obj.Value);

            Console.WriteLine("Serialization to new version");
            var xml2 = serializer.Serialize(obj);

            Console.WriteLine(xml2);
        }
Esempio n. 15
0
//        public static void RunAutofacConfig()
//        {
//            Program.PrintHeader("Deserialization old version of xml - autofac config");
//
//            var builder = new ContainerBuilder();
//            builder.RegisterModule<AutofacExtendedXmlSerializerModule>();
//            builder.RegisterType<TestClassConfig>().As<ExtendedXmlSerializerConfig<TestClass>>().SingleInstance();
//            var containter = builder.Build();
//
//            var serializer = containter.Resolve<IExtendedXmlSerializer>();
//            Run(serializer);
//        }

        static void Run(IExtendedXmlSerializer serializer)
        {
            string xml =
                @"<?xml version=""1.0"" encoding=""utf-8""?>
<TestClass xmlns=""clr-namespace:ExtendedXmlSerializer.Samples.MigrationMap;assembly=ExtendedXmlSerializer.Samples"">
<Id>1</Id>
<Type>Type</Type>
</TestClass>";

            Console.WriteLine(xml);
            TestClass obj = serializer.Deserialize <TestClass>(xml);

            Console.WriteLine("Obiect Id = " + obj.Id);
            Console.WriteLine("Obiect Name = " + obj.Name);
            Console.WriteLine("Obiect Value = " + obj.Value);

            Console.WriteLine("Serialization to new version");
            string xml2 = serializer.Serialize(new XmlWriterSettings {
                Indent = true
            }, obj);

            File.WriteAllText("bin\\XmlLastVersion.xml", xml2);
            Console.WriteLine(xml2);
        }
Esempio n. 16
0
 /// <summary>
 /// Serialization convenience method to serialize the provided instance into the provided destination
 /// <see cref="TextWriter"/>.
 /// </summary>
 /// <param name="this">The serializer to use for serialization.</param>
 /// <param name="writer">The destination writer.</param>
 /// <param name="instance">The instance to serialize.</param>
 public static void Serialize(this IExtendedXmlSerializer @this, TextWriter writer, object instance)
 => @this.Serialize(XmlWriterFactory.Default, writer, instance);
Esempio n. 17
0
 static string Serialize(this IExtendedXmlSerializer @this, IXmlWriterFactory factory, Func <Stream> stream,
                         object instance)
 => new InstanceFormatter(@this, factory, stream).Get(instance);
Esempio n. 18
0
 /// <summary>
 /// Serialization convenience method to serialize the provided instance into a string along with the provided
 /// destination <see cref="Stream"/> while using the settings configured in the provided
 /// <see cref="XmlWriterSettings"/>.
 /// </summary>
 /// <param name="this">The serializer to use for serialization.</param>
 /// <param name="settings">The writer settings for handling the xml writer used create the resulting Xml.</param>
 /// <param name="stream">The destination stream.</param>
 /// <param name="instance">The instance to serialize.</param>
 /// <returns>A string that represents the provided instance in Xml format.</returns>
 public static string Serialize(this IExtendedXmlSerializer @this, XmlWriterSettings settings, Stream stream,
                                object instance)
 => @this.Serialize(new XmlWriterFactory(settings), stream.Self, instance);
Esempio n. 19
0
 /// <summary>
 /// Serialization convenience method to serialize the provided instance into a string along with the provided
 /// destination <see cref="Stream"/>.
 /// </summary>
 /// <param name="this">The serializer to use for serialization.</param>
 /// <param name="stream">The destination stream.</param>
 /// <param name="instance">The instance to serialize.</param>
 /// <returns>A string that represents the provided instance in Xml format.</returns>
 public static string Serialize(this IExtendedXmlSerializer @this, Stream stream, object instance)
 => @this.Serialize(XmlWriterFactory.Default, stream.Self, instance);
Esempio n. 20
0
 /// <summary>
 /// Serialization convenience method to serialize the provided instance into a string with the provided
 /// <see cref="XmlWriterSettings"/>.
 /// </summary>
 /// <param name="this">The serializer to use for serialization.</param>
 /// <param name="settings">The writer settings for handling the xml writer used create the resulting Xml.</param>
 /// <param name="instance">The instance to serialize.</param>
 /// <returns>A string that represents the provided instance in Xml format.</returns>
 public static string Serialize(this IExtendedXmlSerializer @this, XmlWriterSettings settings, object instance)
 => @this.Serialize(new XmlWriterFactory(CloseSettings.Default.Get(settings)), New, instance);
Esempio n. 21
0
 /// <summary>
 /// Specifies an existing reference to use as target for deserialization of values.  Read values during
 /// deserialization will be read and assigned into the provided target instance. NOTICE: Be sure to call
 /// <see cref="AllowTargetInstances"/> when configuring the container before using this method.
 /// </summary>
 /// <typeparam name="T">The instance type.</typeparam>
 /// <param name="this">The serializer</param>
 /// <param name="instance">The instance to deserialize</param>
 /// <returns>A deserialization context that will assign values to the provided instance.</returns>
 /// <seealso href="https://github.com/ExtendedXmlSerializer/home/issues/230" />
 public static ReferencedDeserializationContext <T> UsingTarget <T>(this IExtendedXmlSerializer @this, T instance)
     where T : class
 => new ReferencedDeserializationContext <T>(@this, instance);
Esempio n. 22
0
 public static T Deserialize <T>(this IExtendedXmlSerializer @this, T existing, string data) where T : class
 => @this.UsingTarget(existing)
 .Deserialize(data);
Esempio n. 23
0
 /// <summary>
 /// Serialization convenience method to serialize the provided instance into the provided destination
 /// <see cref="TextWriter"/> while using the settings configured in the provided <see cref="XmlWriterSettings"/>.
 /// </summary>
 /// <param name="this">The serializer to use for serialization.</param>
 /// <param name="settings">The writer settings for handling the xml writer used create the resulting Xml.</param>
 /// <param name="writer">The destination writer.</param>
 /// <param name="instance">The instance to serialize.</param>
 public static void Serialize(this IExtendedXmlSerializer @this, XmlWriterSettings settings, TextWriter writer,
                              object instance)
 => @this.Serialize(new XmlWriterFactory(settings), writer, instance);
Esempio n. 24
0
 /// <summary>
 /// Deserialization convenience method to deserialize a document found within the provided string into an instance of
 /// the requested instance type, using the provided <see cref="XmlReaderSettings"/>.
 /// </summary>
 /// <typeparam name="T">The requested instance type.</typeparam>
 /// <param name="this">The serializer to create the requested instance.</param>
 /// <param name="settings">The reader settings for handling the xml reader used create the instance.</param>
 /// <param name="data">A text representation of an Xml document.</param>
 /// <returns>An instance of the requested type.</returns>
 public static T Deserialize <T>(this IExtendedXmlSerializer @this, XmlReaderSettings settings, string data)
 => @this.Deserialize <T>(settings, new MemoryStream(Encoding.UTF8.GetBytes(data)));
Esempio n. 25
0
 static void Serialize(this IExtendedXmlSerializer @this, IXmlWriterFactory factory, TextWriter writer,
                       object instance)
 => @this.Serialize(factory.Get(writer), instance);
Esempio n. 26
0
 /// <summary>
 /// Deserialization convenience method to deserialize a document found within the provided <see cref="TextReader"/>
 /// into an instance of the requested instance type, using the default reader settings found at
 /// <see cref="ExtensionModel.Xml.Defaults.ReaderSettings"/>.
 /// </summary>
 /// <typeparam name="T">The requested instance type.</typeparam>
 /// <param name="this">The serializer to create the requested instance.</param>
 /// <param name="reader">The reader containing the necessary data to deserialize the object of requested type.</param>
 /// <returns>An instance of the requested type.</returns>
 public static T Deserialize <T>(this IExtendedXmlSerializer @this, TextReader reader)
 => @this.Deserialize <T>(ExtensionModel.Xml.Defaults.ReaderSettings, reader);
Esempio n. 27
0
 public static T Deserialize <T>(this IExtendedXmlSerializer @this, T existing, XmlReaderSettings settings,
                                 Stream stream)
     where T : class
 => @this.UsingTarget(existing).Deserialize(settings, stream);
Esempio n. 28
0
 /// <summary>
 /// Deserialization convenience method to deserialize a document found within the provided <see cref="TextReader"/>
 /// into an instance of the requested instance type, using the provided <see cref="XmlReaderSettings"/>.
 /// </summary>
 /// <typeparam name="T">The requested instance type.</typeparam>
 /// <param name="this">The serializer to create the requested instance.</param>
 /// <param name="settings">The reader settings for handling the xml reader used create the instance.</param>
 /// <param name="reader">The reader containing the necessary data to deserialize the object of requested type.</param>
 /// <returns>An instance of the requested type.</returns>
 public static T Deserialize <T>(this IExtendedXmlSerializer @this, XmlReaderSettings settings, TextReader reader)
 => @this.Deserialize(new XmlReaderFactory(settings, settings.NameTable.Context()).Get(reader)).AsValid <T>();
Esempio n. 29
0
 /// <summary>
 /// Deserialization convenience method to deserialize a document found within the provided <see cref="Stream"/> into
 /// an instance of the requested instance type, using the default reader settings found at
 /// <see cref="ExtensionModel.Xml.Defaults.ReaderSettings"/>.
 /// </summary>
 /// <typeparam name="T">The requested instance type.</typeparam>
 /// <param name="this">The serializer to create the requested instance.</param>
 /// <param name="stream">The stream containing the necessary data to deserialize the object of requested type.</param>
 /// <returns>An instance of the requested type.</returns>
 public static T Deserialize <T>(this IExtendedXmlSerializer @this, Stream stream)
 => @this.Deserialize <T>(ExtensionModel.Xml.Defaults.ReaderSettings, stream);
Esempio n. 30
0
 /// <summary>
 /// Serialization convenience method to serialize the provided instance into a string.
 /// </summary>
 /// <param name="this">The serializer to use for serialization.</param>
 /// <param name="instance">The instance to serialize.</param>
 /// <returns>A string that represents the provided instance in Xml format.</returns>
 public static string Serialize(this IExtendedXmlSerializer @this, object instance)
 => @this.Serialize(WriterFactory, New, instance);