protected override object DeserializeObject(ArraySegment<byte> value)
		{
			var ds = new NetDataContractSerializer();

			using (var ms = new MemoryStream(value.Array, value.Offset, value.Count))
				return ds.Deserialize(ms);
		}
 public override object Deserialize(Type type, byte[] data)
 {
     var formatter = new NetDataContractSerializer();
     using (var ms = new MemoryStream(data))
     {
         return formatter.Deserialize(ms);
     }
 }
Example #3
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);
     }
 }
        object DeserializeObject(NetDataContractSerializer serializer, XmlElement element)
        {
            object deserializedObject = null;

            MemoryStream stm = new MemoryStream();
            XmlDictionaryWriter wtr = XmlDictionaryWriter.CreateTextWriter(stm);
            element.WriteContentTo(wtr);
            wtr.Flush();
            stm.Position = 0;

            deserializedObject = serializer.Deserialize(stm);

            return deserializedObject;
        }
        private Object Deserialize(NetDataContractSerializer serializer, XElement element)
        {
            Object result;
            using(MemoryStream stream = new MemoryStream())
            {
                using(XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(stream))
                {
                    foreach(XNode node in element.Nodes())
                    {
                        node.WriteTo(writer);
                    }

                    writer.Flush();
                    stream.Position = 0;
                    result = serializer.Deserialize(stream);
                }
            }
            return result;
        }
 public void AfterReceiveReply(ref NDceRpc.ServiceModel.Channels.Message reply, object correlationState)
 {
     var stream = new MemoryStream(reply.Fault.Detail);
     var serializer = new NetDataContractSerializer();
     var exeption = (Exception)serializer.Deserialize(stream);
     throw exeption;
 }
    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;
     }
 }
Example #9
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;
        }
Example #10
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;
        }