예제 #1
0
 public void DeserializeFromString_TextJson()
 {
     textJsonSerializer.DeserializeFromString <TestClass>(jsonFromString);
 }
예제 #2
0
        private void CompareDeserializes <T>(T obj, int runs)
        {
            var objType = obj.GetType();

            //warm-up

            byte[] protobufData, binaryData, dataContractData, soapData, netData;

#if NETFULL || NETCOREAPP3_1
            var jilSerializedText = jilserializer.SerializeToString(obj);
            using (MemoryStream mem = new MemoryStream())
            {
                binaryserializer.Serialize(obj, mem);
                binaryData = mem.ToArray();
            }

            using (MemoryStream mem = new MemoryStream())
            {
                datacontractserializer.Serialize(obj, mem);
                dataContractData = mem.ToArray();
            }
#endif

#if NETFULL
            using (MemoryStream mem = new MemoryStream())
            {
                soapserializer.Serialize(obj, mem);
                soapData = mem.ToArray();
            }
#endif

            var netserializer = SerializerFactory.Get("NET");
            using (MemoryStream mem = new MemoryStream())
            {
                netserializer.Serialize(obj, mem);
                netData = mem.ToArray();
            }

            var jsonnetSerializedText  = jsonnetserializer.SerializeToString(obj);
            var textjsonSerializedText = textjsonserializer.SerializeToString(obj);
            var xmlSerializedText      = xmlserializer.SerializeToString(obj);
            using (MemoryStream mem = new MemoryStream())
            {
                protobufserializer.Serialize(obj, mem);
                protobufData = mem.ToArray();
            }

            var keys = serializer.Keys.ToList();

#if NETFULL || NETCOREAPP3_1
            serializer["Jil"].Act = () =>
            {
                GC.Collect(2, GCCollectionMode.Forced, blocking: true);
                serializer["Jil"].Score = Helper.AverageRuntime(() =>
                {
                    jilserializer.DeserializeFromString(jilSerializedText, objType);
                }, runs);
            };
            serializer["Binary"].Act = () =>
            {
                GC.Collect(2, GCCollectionMode.Forced, blocking: true);
                serializer["Binary"].Score = Helper.AverageRuntime(() =>
                {
                    using (MemoryStream mem = new MemoryStream(binaryData))
                    {
                        binaryserializer.Deserialize(mem, objType);
                    }
                }, runs);
            };

            serializer["DataContract"].Act = () =>
            {
                GC.Collect(2, GCCollectionMode.Forced, blocking: true);
                serializer["DataContract"].Score = Helper.AverageRuntime(() =>
                {
                    using (MemoryStream mem = new MemoryStream(dataContractData))
                    {
                        datacontractserializer.Deserialize(mem, objType);
                    }
                }, runs);
            };
#endif

#if NETFULL
            serializer["Soap"].Act = () =>
            {
                GC.Collect(2, GCCollectionMode.Forced, blocking: true);
                serializer["Soap"].Score = Helper.AverageRuntime(() =>
                {
                    using (MemoryStream mem = new MemoryStream(soapData))
                    {
                        soapserializer.Deserialize(mem, objType);
                    }
                }, runs);
            };
#endif

            serializer["NET"].Act = () =>
            {
                GC.Collect(2, GCCollectionMode.Forced, blocking: true);
                serializer["NET"].Score = Helper.AverageRuntime(() =>
                {
                    using (MemoryStream mem = new MemoryStream(netData))
                    {
                        netserializer.Deserialize(mem, objType);
                    }
                }, runs);
            };

            serializer["Json"].Act = () =>
            {
                GC.Collect(2, GCCollectionMode.Forced, blocking: true);
                serializer["Json"].Score = Helper.AverageRuntime(() =>
                {
                    jsonnetserializer.DeserializeFromString(jsonnetSerializedText, objType);
                }, runs);
            };

            serializer["TextJson"].Act = () =>
            {
                GC.Collect(2, GCCollectionMode.Forced, blocking: true);
                serializer["TextJson"].Score = Helper.AverageRuntime(() =>
                {
                    textjsonserializer.DeserializeFromString(textjsonSerializedText, objType);
                }, runs);
            };

            serializer["Xml"].Act = () =>
            {
                GC.Collect(2, GCCollectionMode.Forced, blocking: true);
                serializer["Xml"].Score = Helper.AverageRuntime(() =>
                {
                    xmlserializer.DeserializeFromString(xmlSerializedText, objType);
                }, runs);
            };

            serializer["Protobuf"].Act = () =>
            {
                GC.Collect(2, GCCollectionMode.Forced, blocking: true);
                serializer["Protobuf"].Score = Helper.AverageRuntime(() =>
                {
                    using (MemoryStream mem = new MemoryStream(protobufData))
                    {
                        protobufserializer.Deserialize(mem, objType);
                    }
                }, runs);
            };

            keys.ForEach(k =>
            {
                serializer[k].Act();
            });
        }