public override byte[] Serialize(object obj)
 {
     var formatter = new NetDataContractSerializer();
     using (var ms = new MemoryStream())
     {
         formatter.Serialize(ms, obj);
         return ms.ToArray();
     }
 }
示例#2
0
 /// <summary>
 /// Creates a new instance of the class with the same value as instance.
 /// </summary>
 /// <returns>Returns cloned object.</returns>
 public virtual object Clone()
 {
     IFormatter formatter = new NetDataContractSerializer();
     using(Stream stream = new MemoryStream())
     {
         formatter.Serialize(stream, this);
         stream.Seek(0, SeekOrigin.Begin);
         return formatter.Deserialize(stream);
     }
 }
        public void SholdSerializeOperator()
        {
            // Given
            var entity = new FilterTestEntity { StringField = "Abc", IntField = 5 };

            // Какое-то сложное логическое выражение
            var specification = new FilterSpecification<FilterTestEntity>(
                b => b.Or(
                    c => c
                             .And(a => a.Or(o => o
                                                     .Null(i => i.StringField)
                                                     .Equal(i => i.StringField, string.Empty))
                                           .Equal(i => i.IntField, 0))
                             .And(a => a
                                           .NotNull(i => i.StringField)
                                           .NotEqual(i => i.StringField, string.Empty)
                                           .Or(o => o
                                                        .In(i => i.StringField.ToLower(), new[] { "a", "b", "c" })
                                                        .Contains(i => i.StringField.ToLower(), "abc")
                                                        .StartsWith(i => i.StringField, "1")
                                                        .EndsWith(i => i.StringField, "5"))
                                           .Or(o => o
                                                        .And(a2 => a2
                                                                       .GreaterOrEqual(i => i.IntField, 0)
                                                                       .LessOrEqual(i => i.IntField, 10))
                                                        .And(a2 => a2
                                                                       .Greater(i => i.IntField, 10)
                                                                       .Less(i => i.IntField, 20))
                                                        .Between(i => i.IntField, 20, 30)))));

            IFilterOperator operatorFromStream;
            IFilterOperator operatorToStream = specification.Operator;

            // When
            using (var stream = new MemoryStream())
            {
                // Сериализация
                var serializer = new NetDataContractSerializer();
                serializer.Serialize(stream, operatorToStream);
                stream.Position = 0;

                // Десериализация
                operatorFromStream = (IFilterOperator)serializer.ReadObject(stream);
            }

            // Then
            Assert.IsNotNull(operatorFromStream);
            Assert.AreEqual(operatorToStream.IsSatisfiedBy(entity), operatorFromStream.IsSatisfiedBy(entity));
        }
        private void Serialize(NetDataContractSerializer serializer, XElement parent, string name, object value)
        {
            XElement element = new XElement(name);
            using(MemoryStream stream = new MemoryStream())
            {
                serializer.Serialize(stream, value);
                stream.Position = 0;

                element.Add(XElement.Load(stream));
            }
            parent.Add(element);
        }
        public XmlElement SerializeObject(string elementName, object o, XmlDocument doc)
        {
            var serializer = new NetDataContractSerializer();
            var newElement = doc.CreateElement(elementName);

            using (var memoryStream = new MemoryStream())
            {
                serializer.Serialize(memoryStream, o);
                memoryStream.Position = 0;
                var rdr = new StreamReader(memoryStream);
                newElement.InnerXml = rdr.ReadToEnd();
            }

            return newElement;
        }
 /// <summary>
 /// The serialize object.
 /// </summary>
 /// <param name="elementName">The element name.</param>
 /// <param name="objectToSerialize">The object to serialize.</param>
 /// <param name="xmlDocument">The xml document.</param>
 /// <returns>The <see cref="XmlElement" />.</returns>
 private static XmlElement SerializeObject(string elementName, object objectToSerialize, XmlDocument xmlDocument)
 {
     var netDataContractSerializer = new NetDataContractSerializer();
     var newElement = xmlDocument.CreateElement(elementName);
     var memoryStream = new MemoryStream();
     netDataContractSerializer.Serialize(memoryStream, objectToSerialize);
     memoryStream.Position = 0;
     var rdr = new StreamReader(memoryStream);
     newElement.InnerXml = rdr.ReadToEnd();
     return newElement;
 }
        XmlElement SerializeObject(string elementName, object o, XmlDocument doc)
        {
            NetDataContractSerializer s = new NetDataContractSerializer();
            XmlElement newElement = doc.CreateElement(elementName);
            MemoryStream stm = new MemoryStream();

            s.Serialize(stm, o);
            stm.Position = 0;
            StreamReader rdr = new StreamReader(stm);
            newElement.InnerXml = rdr.ReadToEnd();

            return newElement;
        }
 public void ProvideFault(Exception error, NDceRpc.ServiceModel.Channels.MessageVersion version, ref NDceRpc.ServiceModel.Channels.Message fault)
 {
     if (!HandleError(error))
     {
         return;
     }
     var serializer = new NetDataContractSerializer();
     var stream = new MemoryStream();
     serializer.Serialize(stream, error);
     stream.Position = 0;
     fault.Fault.Detail = stream.ToArray();
 }
    static void Main()
    {
        var orig = new Game
        {
            Finished = true,
            GameGUID = Guid.NewGuid(),
            GameID = 12345,
            GameSetup = false,
            MaximumCardsInDeck = 20,
            Player = new Player { Name = "Fred" },
            Player1 = new Player { Name = "Barney" },
            Player1Connected = true,
            Player1EnvironmentSetup = true,
            Player1ID = 12345,
            Player1Won = 3,
            Player2Connected = true,
            Player2EnvironmentSetup = true,
            Player2ID = 23456,
            Player2Won = 0,
            Round = 4,
            RoundsToWin = 5,
            Started = true,
            StateXML = "not really xml",
            TimeEnded = null,
            TimeLimitPerTurn = 500,
            TimeStamp = new byte[] { 1, 2, 3, 4, 5, 6 },
            TimeStarted = DateTime.Today
        };
        const int LOOP = 50000;

        GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
        GC.WaitForPendingFinalizers();
        using (var ms = new MemoryStream())
        {
            var ser = new BinaryFormatter();
            Console.WriteLine();
            Console.WriteLine(ser.GetType().Name);
            ser.Serialize(ms, orig);
            Console.WriteLine("Length: " + ms.Length);
            ms.Position = 0;
            ser.Deserialize(ms);

            var watch = Stopwatch.StartNew();
            for (int i = 0; i < LOOP; i++)
            {
                ms.Position = 0;
                ms.SetLength(0);
                ser.Serialize(ms, orig);
            }
            watch.Stop();
            Console.WriteLine("Serialize: " + watch.ElapsedMilliseconds);
            watch = Stopwatch.StartNew();
            for (int i = 0; i < LOOP; i++)
            {
                ms.Position = 0;
                ser.Deserialize(ms);
            }
            watch.Stop();
            Console.WriteLine("Deserialize: " + watch.ElapsedMilliseconds);
        }

        GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
        GC.WaitForPendingFinalizers();
        using (var ms = new MemoryStream())
        {
            var ser = new XmlSerializer(typeof(Game));
            Console.WriteLine();
            Console.WriteLine(ser.GetType().Name);
            ser.Serialize(ms, orig);
            Console.WriteLine("Length: " + ms.Length);
            ms.Position = 0;
            ser.Deserialize(ms);

            var watch = Stopwatch.StartNew();
            for (int i = 0; i < LOOP; i++)
            {
                ms.Position = 0;
                ms.SetLength(0);
                ser.Serialize(ms, orig);
            }
            watch.Stop();
            Console.WriteLine("Serialize: " + watch.ElapsedMilliseconds);
            watch = Stopwatch.StartNew();
            for (int i = 0; i < LOOP; i++)
            {
                ms.Position = 0;
                ser.Deserialize(ms);
            }
            watch.Stop();
            Console.WriteLine("Deserialize: " + watch.ElapsedMilliseconds);
        }

        GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
        GC.WaitForPendingFinalizers();
        using (var ms = new MemoryStream())
        {
            var ser = new DataContractSerializer(typeof(Game));
            Console.WriteLine();
            Console.WriteLine(ser.GetType().Name);
            ser.WriteObject(ms, orig);
            Console.WriteLine("Length: " + ms.Length);
            ms.Position = 0;
            ser.ReadObject(ms);

            var watch = Stopwatch.StartNew();
            for (int i = 0; i < LOOP; i++)
            {
                ms.Position = 0;
                ms.SetLength(0);
                ser.WriteObject(ms, orig);
            }
            watch.Stop();
            Console.WriteLine("Serialize: " + watch.ElapsedMilliseconds);
            watch = Stopwatch.StartNew();
            for (int i = 0; i < LOOP; i++)
            {
                ms.Position = 0;
                ser.ReadObject(ms);
            }
            watch.Stop();
            Console.WriteLine("Deserialize: " + watch.ElapsedMilliseconds);
        }

        GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
        GC.WaitForPendingFinalizers();
        using (var ms = new MemoryStream())
        {
            var ser = new NetDataContractSerializer();
            Console.WriteLine();
            Console.WriteLine(ser.GetType().Name);
            ser.Serialize(ms, orig);
            Console.WriteLine("Length: " + ms.Length);
            ms.Position = 0;
            ser.Deserialize(ms);

            var watch = Stopwatch.StartNew();
            for (int i = 0; i < LOOP; i++)
            {
                ms.Position = 0;
                ms.SetLength(0);
                ser.Serialize(ms, orig);
            }
            watch.Stop();
            Console.WriteLine("Serialize: " + watch.ElapsedMilliseconds);
            watch = Stopwatch.StartNew();
            for (int i = 0; i < LOOP; i++)
            {
                ms.Position = 0;
                ser.Deserialize(ms);
            }
            watch.Stop();
            Console.WriteLine("Deserialize: " + watch.ElapsedMilliseconds);
        }

        GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
        GC.WaitForPendingFinalizers();
        {
            var sb = new StringBuilder();
            // var ser = new JavaScriptSerializer(); // netfx 4.5
            var ser = new Polenter.Serialization.SharpSerializer(true); // SharpSerializer Binary
            Console.WriteLine();
            Console.WriteLine(ser.GetType().Name);
            // ser.Serialize(orig, sb);
            // Console.WriteLine("Length: " + sb.Length);
            // ser.Deserialize(sb.ToString(), typeof(Game));
            //
            // var watch = Stopwatch.StartNew();
            // for (int i = 0; i < LOOP; i++)
            // {
            // 	sb.Length = 0;
            // 	ser.Serialize(orig, sb);
            // }
            // watch.Stop();
            // string s = sb.ToString();
            // Console.WriteLine("Serialize: " + watch.ElapsedMilliseconds);
            // watch = Stopwatch.StartNew();
            // for (int i = 0; i < LOOP; i++)
            // {
            // 	ser.Deserialize(s, typeof(Game));
            // }
            // watch.Stop();
            // Console.WriteLine("Deserialize: " + watch.ElapsedMilliseconds);
        }

        GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
        GC.WaitForPendingFinalizers();
        using (var ms = new MemoryStream())
        {
            var ser = CreateProto();
            Console.WriteLine();
            Console.WriteLine("(protobuf-net v2)");
            ser.Serialize(ms, orig);
            Console.WriteLine("Length: " + ms.Length);
            ms.Position = 0;
            ser.Deserialize(ms, null, typeof(Game));

            var watch = Stopwatch.StartNew();
            for (int i = 0; i < LOOP; i++)
            {
                ms.Position = 0;
                ms.SetLength(0);
                ser.Serialize(ms, orig);
            }
            watch.Stop();
            Console.WriteLine("Serialize: " + watch.ElapsedMilliseconds);
            watch = Stopwatch.StartNew();
            for (int i = 0; i < LOOP; i++)
            {
                ms.Position = 0;
                ser.Deserialize(ms, null, typeof(Game));
            }
            watch.Stop();
            Console.WriteLine("Deserialize: " + watch.ElapsedMilliseconds);
        }

        Console.WriteLine();
        Console.WriteLine("All done; any key to exit");
        Console.ReadKey();
    }
 private static JsonValue ValidateSerialization(JsonValue beforeSerialization)
 {
     Assert.NotNull(beforeSerialization);
     NetDataContractSerializer serializer = new NetDataContractSerializer();
     using (MemoryStream memStream = new MemoryStream())
     {
         serializer.Serialize(memStream, beforeSerialization);
         memStream.Position = 0;
         JsonValue afterDeserialization = (JsonValue)serializer.Deserialize(memStream);
         Assert.Equal(beforeSerialization.ToString(), afterDeserialization.ToString());
         return afterDeserialization;
     }
 }
示例#11
0
        SignedHeaderResponse SerializeDeserialize(SignedHeaderResponse authResponse)
        {
            var serializer = new NetDataContractSerializer();
            var stream = new MemoryStream();
            serializer.Serialize(stream, authResponse);
            stream.Position = 0;
            var result = (SignedHeaderResponse)serializer.Deserialize(stream);

            return result;
        }
示例#12
0
        static SignedHeaderRequest SerializeDeserialize(SignedHeaderRequest authRequest)
        {
            var serializer = new NetDataContractSerializer();
            var stream = new MemoryStream();
            serializer.Serialize(stream, authRequest);
            stream.Position = 0;
            var addRequest2 = (SignedHeaderRequest)serializer.Deserialize(stream);

            return addRequest2;
        }