public T Assert <T>(T instance, string expected)
        {
            var data = _serializer.Serialize(instance);

            data?.Replace("\r\n", string.Empty)
            .Replace("\n", string.Empty)
            .Should()
            .Be(expected?.Replace("\r\n", string.Empty)
                .Replace("\n", string.Empty));
            var result = _serializer.Deserialize <T>(data);

            return(result);
        }
        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);
        }
示例#3
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 object ReadValue(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
        {
            var contentHeaders = content?.Headers;

            if (contentHeaders != null && contentHeaders.ContentLength == 0)
            {
                return(GetDefaultValueForType(type));
            }

            var encoding = SelectCharacterEncoding(contentHeaders);

            try
            {
                StreamReader reader = new StreamReader(readStream, encoding);
                string       text   = reader.ReadToEnd();
                return(_serializer.Deserialize(text, type));
            }
            catch (Exception ex)
            {
                if (formatterLogger == null)
                {
                    throw;
                }

                formatterLogger.LogError(string.Empty, ex);

                return(GetDefaultValueForType(type));
            }
        }
 public async Task <DeployerStore> Get()
 {
     using (var stream = await downloader.GetStream(uri.ToString()))
     {
         var deserialize = serializer.Deserialize(XmlReader.Create(stream));
         var store       = (DeployerStore)deserialize;
         return(store);
     }
 }
示例#6
0
 public object DeserializationClassWithPrimitive()
 {
     using (var stream = new MemoryStream(_data))
     {
         using (var reader = _readerFactory.Get(stream))
         {
             return(_serializer.Deserialize(reader));
         }
     }
 }
示例#7
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);
        }
示例#9
0
 public static Dictionary <string, string> Read(string file)
 {
     if (File.Exists(file))
     {
         using (StreamReader sr = new StreamReader(file))
         {
             var text = sr.ReadToEnd();
             return(serializer.Deserialize <Dictionary <string, string> >(text));
         }
     }
     return(null);
 }
        public Task <RESTResult> GetPermissionByPagingAsync(Guid currentUserId, int pageIndex, int pageSize)
        {
            RESTResult result = new RESTResult {
                Code = RESTStatus.Success
            };
            var permissionLists = _permissionRepository.GetAllList(item => item.IsDeleted.Equals(false)).Skip(pageSize * (pageIndex - 1)).Take(pageSize);
            List <PermissionDto> permissionDOTList = new List <PermissionDto>();

            foreach (var item in permissionLists)
            {
                var permissionDTO = Mapper.Map <Permission, PermissionDto>(item);
                permissionDTO.PermissionData = string.IsNullOrEmpty(item.DataXml) ? null : _serializer.Deserialize <PageMenu>(item.DataXml);
                permissionDOTList.Add(permissionDTO);
            }
            result.Data = permissionDOTList;
            return(Task.FromResult(result));
        }
        /// <inheritdoc />
        public override Task <InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (encoding == null)
            {
                throw new ArgumentNullException(nameof(encoding));
            }

            StreamReader reader = new StreamReader(context.HttpContext.Request.Body, encoding);
            string       text   = reader.ReadToEnd();

            object model = _serializer.Deserialize(text, context.ModelType);

            return(InputFormatterResult.SuccessAsync(model));
        }
示例#12
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)));
        }
示例#13
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);
        }
示例#14
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);
        }
示例#15
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);
示例#16
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);
示例#17
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)));
示例#18
0
 /// <summary>
 /// Deserialization convenience method to deserialize a document found within the provided string into an instance of
 /// the requested instance type, using reader settings that will close the stream once the process is complete.
 /// </summary>
 /// <typeparam name="T">The requested instance type.</typeparam>
 /// <param name="this">The serializer to create the requested 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, string data)
 => @this.Deserialize <T>(ExtensionModel.Xml.Defaults.CloseRead, data);
示例#19
0
 /// <summary>
 /// Call AllowExistingInstances when creating ConfigurationContainer to use this method.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="this"></param>
 /// <param name="existing"></param>
 /// <param name="stream"></param>
 /// <returns></returns>
 public static T Deserialize <T>(this IExtendedXmlSerializer @this, T existing, Stream stream)
     where T : class
 => @this.Deserialize(existing, Defaults.ReaderSettings, stream);
示例#20
0
 /// <summary>
 /// Call AllowExistingInstances when creating ConfigurationContainer to use this method.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="this"></param>
 /// <param name="existing"></param>
 /// <param name="settings"></param>
 /// <param name="stream"></param>
 /// <returns></returns>
 public static T Deserialize <T>(this IExtendedXmlSerializer @this, T existing, XmlReaderSettings settings,
                                 Stream stream)
     where T : class
 => @this.Deserialize(existing, new XmlReaderFactory(settings, settings.NameTable.Context()), stream);
示例#21
0
 static T Deserialize <T>(this IExtendedXmlSerializer @this, IXmlReaderFactory factory, TextReader reader)
 => @this.Deserialize(factory.Get(reader))
 .AsValid <T>();
示例#22
0
 /// <summary>
 /// Call AllowExistingInstances when creating ConfigurationContainer to use this method.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="this"></param>
 /// <param name="existing"></param>
 /// <param name="settings"></param>
 /// <param name="data"></param>
 /// <returns></returns>
 public static T Deserialize <T>(this IExtendedXmlSerializer @this, T existing, XmlReaderSettings settings,
                                 string data)
     where T : class
 => @this.Deserialize(existing, settings, new MemoryStream(Encoding.UTF8.GetBytes(data)));
示例#23
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>();
示例#24
0
 public T Deserialize <T>(string serialized)
 {
     return(_serializer.Deserialize <T>(serialized));
 }
示例#25
0
 public static T Cycle <T>(this IExtendedXmlSerializer @this, T instance)
 => @this.Deserialize <T>(@this.Serialize(instance));
示例#26
0
 static T Deserialize <T>(this IExtendedXmlSerializer @this, IXmlReaderFactory factory, Stream stream)
 => @this.Deserialize(factory.Get(stream))
 .AsValid <T>();
示例#27
0
 /// <summary>
 /// Call AllowExistingInstances when creating ConfigurationContainer to use this method.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="this"></param>
 /// <param name="existing"></param>
 /// <param name="data"></param>
 /// <returns></returns>
 public static T Deserialize <T>(this IExtendedXmlSerializer @this, T existing, string data)
     where T : class
 => @this.Deserialize(existing, CloseRead, data);