public string SerializeToString <T>(T obj)
        {
            if (TextSerializer != null)
            {
                return(TextSerializer.SerializeToString(obj));
            }

            if (!UseBcl)
            {
                return(JsonSerializer.SerializeToString(obj));
            }

            if (obj == null)
            {
                return(null);
            }
            var type = obj.GetType();

            try
            {
                using (var ms = MemoryStreamFactory.GetStream())
                {
                    var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(type);
                    serializer.WriteObject(ms, obj);
                    return(ms.ReadToEnd());
                }
            }
            catch (Exception ex)
            {
                throw new SerializationException("JsonDataContractSerializer: Error converting type: " + ex.Message, ex);
            }
        }
Esempio n. 2
0
        public T With_TextSerializer <T>(T dto)
        {
            var dtoString = TextSerializer.SerializeToString(dto);

            LogDto(dtoString);
            return(TextSerializer.DeserializeFromString <T>(dtoString));
        }
Esempio n. 3
0
        public void Deserialize_Supplier()
        {
            var dto = DtoFactory.SupplierDto;

            var dtoXml          = JsonUtils.SerializeDCS(dto);
            var dtoJson         = JsonUtils.SerializeDCJS(dto);
            var dtoString       = TypeSerializer.SerializeToString(dto);
            var dtoPlatformText = TextSerializer.SerializeToString(dto);

            RunMultipleTimes(() => JsonUtils.DeserializeDCS <SupplierDto>(dtoXml), "JsonUtils.DeserializeDCS<SupplierDto>(dtoXml)");
            RunMultipleTimes(() => JsonUtils.DeserializeDCJS <SupplierDto>(dtoJson), "JsonUtils.DeserializeDCJS<SupplierDto>(dtoJson)");
            RunMultipleTimes(() => TypeSerializer.DeserializeFromString <SupplierDto>(dtoString), "TypeSerializer.DeserializeFromString<SupplierDto>(dtoString)");
            RunMultipleTimes(() => TextSerializer.DeserializeFromString <SupplierDto>(dtoPlatformText), "TextSerializer.DeserializeFromString<SupplierDto>(dtoPlatformText)");
        }
        public string SerializeToString <T>(T obj)
        {
            if (TextSerializer != null)
            {
                return(TextSerializer.SerializeToString(obj));
            }

            if (!UseBcl)
            {
                return(JsonSerializer.SerializeToString(obj));
            }

            return(BclSerializeToString(obj));
        }
        public void Deserialize_MultiDtoWithOrders()
        {
            var dto = DtoFactory.MultiDtoWithOrders;

            var dtoXml          = DataContractSerializer.Instance.Parse(dto);
            var dtoJson         = JsonDataContractSerializer.Instance.Parse(dto);
            var dtoString       = TypeSerializer.SerializeToString(dto);
            var dtoPlatformText = TextSerializer.SerializeToString(dto);

            RunMultipleTimes(() => DataContractDeserializer.Instance.Parse <MultiDtoWithOrders>(dtoXml), "DataContractDeserializer.Instance.Parse<MultiDtoWithOrders>(dtoXml)");
            RunMultipleTimes(() => JsonDataContractDeserializer.Instance.Parse <MultiDtoWithOrders>(dtoJson), "JsonDataContractDeserializer.Instance.Parse<MultiDtoWithOrders>(dtoJson)");
            RunMultipleTimes(() => TypeSerializer.DeserializeFromString <MultiDtoWithOrders>(dtoString), "TypeSerializer.DeserializeFromString<MultiDtoWithOrders>(dtoString)");
            RunMultipleTimes(() => TextSerializer.DeserializeFromString <MultiDtoWithOrders>(dtoPlatformText), "TextSerializer.DeserializeFromString<MultiDtoWithOrders>(dtoPlatformText)");
        }
Esempio n. 6
0
        public void Deserialize_MultiDtoWithOrders()
        {
            var dto = DtoFactory.MultiDtoWithOrders;

            var dtoXml          = JsonUtils.SerializeDCS(dto);
            var dtoJson         = JsonUtils.SerializeDCJS(dto);
            var dtoString       = TypeSerializer.SerializeToString(dto);
            var dtoPlatformText = TextSerializer.SerializeToString(dto);

            RunMultipleTimes(() => JsonUtils.DeserializeDCS <MultiDtoWithOrders>(dtoXml), "JsonUtils.DeserializeDCS<MultiDtoWithOrders>(dtoXml)");
            RunMultipleTimes(() => JsonUtils.DeserializeDCJS <MultiDtoWithOrders>(dtoJson), "JsonUtils.DeserializeDCJS<MultiDtoWithOrders>(dtoJson)");
            RunMultipleTimes(() => TypeSerializer.DeserializeFromString <MultiDtoWithOrders>(dtoString), "TypeSerializer.DeserializeFromString<MultiDtoWithOrders>(dtoString)");
            RunMultipleTimes(() => TextSerializer.DeserializeFromString <MultiDtoWithOrders>(dtoPlatformText), "TextSerializer.DeserializeFromString<MultiDtoWithOrders>(dtoPlatformText)");
        }
Esempio n. 7
0
        public void Run_NorthwindSupplierToStringScenario()
        {
            var supplier = NorthwindDtoFactory.Supplier(
                1, "Exotic Liquids", "Charlotte Cooper", "Purchasing Manager", "49 Gilbert St.", "London", null,
                "EC1 4SD", "UK", "(171) 555-2222", null, null);

            Log(TypeSerializer.SerializeToString(supplier));
            Log(JsonConvert.ExportToString(supplier));

            RunMultipleTimes(() => DataContractSerializer.Instance.Parse(supplier), "DataContractSerializer.Instance.Parse(supplier)");
            RunMultipleTimes(() => JsonDataContractSerializer.Instance.Parse(supplier), "JsonDataContractSerializer.Instance.Parse(supplier)");
            RunMultipleTimes(() => JsonConvert.ExportToString(supplier), "JsonConvert.ExportToString(supplier)");
            RunMultipleTimes(() => TypeSerializer.SerializeToString(supplier), "TypeSerializer.SerializeToString(supplier)");
            RunMultipleTimes(() => TextSerializer.SerializeToString(supplier), "TextSerializer.SerializeToString(supplier)");
        }
Esempio n. 8
0
        public void Deserialize_CustomerOrderArrayDto()
        {
            var dto = DtoFactory.CustomerOrderArrayDto;

            var dtoXml  = JsonUtils.SerializeDCS(dto);
            var dtoJson = JsonUtils.SerializeDCJS(dto);
            //var dtoJayrock = JsonConvert.ExportToString(dto);
            var dtoString       = TypeSerializer.SerializeToString(dto);
            var dtoPlatformText = TextSerializer.SerializeToString(dto);

            RunMultipleTimes(() => JsonUtils.DeserializeDCS <CustomerOrderArrayDto>(dtoXml), "JsonUtils.DeserializeDCS<CustomerOrderArrayDto>(dtoXml)");
            RunMultipleTimes(() => JsonUtils.DeserializeDCJS <CustomerOrderArrayDto>(dtoJson), "JsonUtils.DeserializeDCJS<CustomerOrderArrayDto>(dtoJson)");
            //RunMultipleTimes(() => JsonConvert.Import(typeof(CustomerOrderArrayDto), dtoJayrock), "JsonConvert.Import(typeof(CustomerOrderArrayDto), dtoJayrock)");  #doesn't do nullables
            RunMultipleTimes(() => TypeSerializer.DeserializeFromString <CustomerOrderArrayDto>(dtoString), "TypeSerializer.DeserializeFromString<CustomerOrderArrayDto>(dtoString)");
            RunMultipleTimes(() => TextSerializer.DeserializeFromString <CustomerOrderArrayDto>(dtoPlatformText), "TextSerializer.DeserializeFromString<CustomerOrderArrayDto>(dtoPlatformText)");
        }
Esempio n. 9
0
        public void Deserialize_CustomerOrderListDto()
        {
            var dto = DtoFactory.CustomerOrderListDto;

            Console.WriteLine(dto.Dump());

            var dtoXml          = JsonUtils.SerializeDCS(dto);
            var dtoJson         = JsonUtils.SerializeDCJS(dto);
            var dtoString       = TypeSerializer.SerializeToString(dto);
            var dtoPlatformText = TextSerializer.SerializeToString(dto);

            RunMultipleTimes(() => JsonUtils.DeserializeDCS <CustomerOrderListDto>(dtoXml), "JsonUtils.DeserializeDCS<CustomerOrderListDto>(dtoXml)");
            RunMultipleTimes(() => JsonUtils.DeserializeDCJS <CustomerOrderListDto>(dtoJson), "JsonUtils.DeserializeDCJS<CustomerOrderListDto>(dtoJson)");
            RunMultipleTimes(() => TypeSerializer.DeserializeFromString <CustomerOrderListDto>(dtoString), "TypeSerializer.DeserializeFromString<CustomerOrderListDto>(dtoString)");
            RunMultipleTimes(() => TextSerializer.DeserializeFromString <CustomerOrderListDto>(dtoPlatformText), "TextSerializer.DeserializeFromString<CustomerOrderListDto>(dtoPlatformText)");
        }
Esempio n. 10
0
        public void Serialize_CustomerOrderListDto()
        {
            var dto = DtoFactory.CustomerOrderListDto;

            Log(DataContractSerializer.Instance.Parse(dto));
            Log(JsonDataContractSerializer.Instance.Parse(dto));
            Log(JsonConvert.ExportToString(dto));
            Log(TypeSerializer.SerializeToString(dto));
            Log(TextSerializer.SerializeToString(dto));

            RunMultipleTimes(() => DataContractSerializer.Instance.Parse(dto), "DataContractSerializer.Instance.Parse(dto)");
            RunMultipleTimes(() => JsonDataContractSerializer.Instance.Parse(dto), "JsonDataContractSerializer.Instance.Parse(dto)");
            RunMultipleTimes(() => JsonConvert.ExportToString(dto), "JsonConvert.ExportToString(dto)");
            RunMultipleTimes(() => TypeSerializer.SerializeToString(dto), "TypeSerializer.SerializeToString(dto)");
            RunMultipleTimes(() => TextSerializer.SerializeToString(dto), "TextSerializer.SerializeToString(dto)");
        }
Esempio n. 11
0
        public void Serialize_ArrayDtoWithOrders()
        {
            var dto = DtoFactory.ArrayDtoWithOrders;

            Log(DataContractSerializer.Instance.Parse(dto));
            Log(JsonDataContractSerializer.Instance.Parse(dto));
            Log(JsonConvert.ExportToString(dto));
            Log(TypeSerializer.SerializeToString(dto));
            Log(TextSerializer.SerializeToString(dto));

            RunMultipleTimes(() => DataContractSerializer.Instance.Parse(dto), "DataContractSerializer.Instance.Parse(dto)");
            RunMultipleTimes(() => JsonDataContractSerializer.Instance.Parse(dto), "JsonDataContractSerializer.Instance.Parse(dto)");
            RunMultipleTimes(() => JsonConvert.ExportToString(dto), "JsonConvert.ExportToString(dto)");
            RunMultipleTimes(() => TypeSerializer.SerializeToString(dto), "TypeSerializer.SerializeToString(dto)");
            RunMultipleTimes(() => TextSerializer.SerializeToString(dto), "TextSerializer.SerializeToString(dto)");
        }
        public void Deserialize_Customer()
        {
            var dto = DtoFactory.CustomerDto;

            var dtoXml          = DataContractSerializer.Instance.Parse(dto);
            var dtoJson         = JsonDataContractSerializer.Instance.Parse(dto);
            var dtoJayrock      = JsonConvert.ExportToString(dto);
            var dtoString       = TypeSerializer.SerializeToString(dto);
            var dtoPlatformText = TextSerializer.SerializeToString(dto);

            RunMultipleTimes(() => DataContractDeserializer.Instance.Parse <CustomerDto>(dtoXml), "DataContractDeserializer.Instance.Parse<CustomerDto>(dtoXml)");
            RunMultipleTimes(() => JsonDataContractDeserializer.Instance.Parse <CustomerDto>(dtoJson), "JsonDataContractDeserializer.Instance.Parse<CustomerDto>(dtoJson)");
            RunMultipleTimes(() => JsonConvert.Import(typeof(CustomerDto), dtoJayrock), "JsonConvert.Import(typeof(CustomerDto), dtoJayrock)");
            RunMultipleTimes(() => TypeSerializer.DeserializeFromString <CustomerDto>(dtoString), "TypeSerializer.DeserializeFromString<CustomerDto>(dtoString)");
            RunMultipleTimes(() => TextSerializer.DeserializeFromString <CustomerDto>(dtoPlatformText), "TextSerializer.DeserializeFromString<CustomerDto>(dtoPlatformText)");
        }
        public void Deserialize_CustomerOrderListDto()
        {
            var dto = DtoFactory.CustomerOrderListDto;

            Console.WriteLine(dto.Dump());

            var dtoXml          = DataContractSerializer.Instance.Parse(dto);
            var dtoJson         = JsonDataContractSerializer.Instance.Parse(dto);
            var dtoString       = TypeSerializer.SerializeToString(dto);
            var dtoPlatformText = TextSerializer.SerializeToString(dto);

            RunMultipleTimes(() => DataContractDeserializer.Instance.Parse <CustomerOrderListDto>(dtoXml), "DataContractDeserializer.Instance.Parse<CustomerOrderListDto>(dtoXml)");
            RunMultipleTimes(() => JsonDataContractDeserializer.Instance.Parse <CustomerOrderListDto>(dtoJson), "JsonDataContractDeserializer.Instance.Parse<CustomerOrderListDto>(dtoJson)");
            RunMultipleTimes(() => TypeSerializer.DeserializeFromString <CustomerOrderListDto>(dtoString), "TypeSerializer.DeserializeFromString<CustomerOrderListDto>(dtoString)");
            RunMultipleTimes(() => TextSerializer.DeserializeFromString <CustomerOrderListDto>(dtoPlatformText), "TextSerializer.DeserializeFromString<CustomerOrderListDto>(dtoPlatformText)");
        }
Esempio n. 14
0
        public void Serialize_Customer()
        {
            var customer = NorthwindDtoFactory.Customer(
                1.ToString("x"), "Alfreds Futterkiste", "Maria Anders", "Sales Representative", "Obere Str. 57",
                "Berlin", null, "12209", "Germany", "030-0074321", "030-0076545", null);

            Log(TypeSerializer.SerializeToString(customer));
            Log(JsonConvert.ExportToString(customer));
            Log(ProtoBufToString(customer));

            RunMultipleTimes(() => DataContractSerializer.Instance.Parse(customer), "DataContractSerializer.Instance.Parse(customer)");
            RunMultipleTimes(() => JsonDataContractSerializer.Instance.Parse(customer), "JsonDataContractSerializer.Instance.Parse(customer)");
            RunMultipleTimes(() => JsonConvert.ExportToString(customer), "JsonConvert.ExportToString(customer)");
            RunMultipleTimes(() => ProtoBufToBytes(customer), "ProtoBufToBytes(customer)");
            RunMultipleTimes(() => TypeSerializer.SerializeToString(customer), "TypeSerializer.SerializeToString(customer)");
            RunMultipleTimes(() => TextSerializer.SerializeToString(customer), "TextSerializer.SerializeToString(customer)");
        }
Esempio n. 15
0
        public void Run_NorthwindOrderToStringScenario()
        {
            var order = NorthwindDtoFactory.Order(
                1, "VINET", 5, new DateTime(1996, 7, 4), new DateTime(1996, 1, 8), new DateTime(1996, 7, 16),
                3, 32.38m, "Vins et alcools Chevalier", "59 rue de l'Abbaye", "Reims", null, "51100", "France");

            Log(TypeSerializer.SerializeToString(order));
            Log(TextSerializer.SerializeToString(order));
            Log(JsonConvert.ExportToString(order));

            RunMultipleTimes(() => DataContractSerializer.Instance.Parse(order), "DataContractSerializer.Instance.Parse(order)");
            RunMultipleTimes(() => JsonDataContractSerializer.Instance.Parse(order), "JsonDataContractSerializer.Instance.Parse(order)");
            RunMultipleTimes(() => JsonConvert.ExportToString(order), "JsonConvert.ExportToString(order)");
            RunMultipleTimes(() => SerializationTestBase.ProtoBufToBytes(order), "SerializationTestBase.ProtoBufToBytes(order)");
            RunMultipleTimes(() => TypeSerializer.SerializeToString(order), "TypeSerializer.SerializeToString(order)");
            RunMultipleTimes(() => TextSerializer.SerializeToString(order), "TextSerializer.SerializeToString(order)");
        }
Esempio n. 16
0
        public string SerializeToString <T>(T obj)
        {
            if (TextSerializer != null)
            {
                return(TextSerializer.SerializeToString(obj));
            }

#if !(SL5 || __IOS__ || XBOX || ANDROID || PCL || NETSTANDARD1_1)
            if (!UseBcl)
            {
                return(JsonSerializer.SerializeToString(obj));
            }

            if (obj == null)
            {
                return(null);
            }
            var type = obj.GetType();
            try
            {
                using (var ms = MemoryStreamFactory.GetStream())
                {
                    var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(type);
                    serializer.WriteObject(ms, obj);
                    ms.Position = 0;
                    using (var sr = new StreamReader(ms))
                    {
                        return(sr.ReadToEnd());
                    }
                }
            }
            catch (Exception ex)
            {
                throw new SerializationException("JsonDataContractSerializer: Error converting type: " + ex.Message, ex);
            }
#else
            return(JsonSerializer.SerializeToString(obj));
#endif
        }
        public string SerializeToString <T>(T obj)
        {
            if (TextSerializer != null)
            {
                return(TextSerializer.SerializeToString(obj));
            }

#if !SILVERLIGHT && !MONOTOUCH && !XBOX && !ANDROIDINDIE
            if (!UseBcl)
            {
                return(JsonSerializer.SerializeToString(obj));
            }

            if (obj == null)
            {
                return(null);
            }
            var type = obj.GetType();
            try
            {
                using (var ms = new MemoryStream())
                {
                    var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(type);
                    serializer.WriteObject(ms, obj);
                    ms.Position = 0;
                    using (var sr = new StreamReader(ms))
                    {
                        return(sr.ReadToEnd());
                    }
                }
            }
            catch (Exception ex)
            {
                throw new SerializationException("JsonDataContractSerializer: Error converting type: " + ex.Message, ex);
            }
#else
            return(JsonSerializer.SerializeToString(obj));
#endif
        }
Esempio n. 18
0
        private void CompareSerializers <T>(T dto)
        {
            CompareMultipleRuns(
                "TypeSerializer", () => TypeSerializer.SerializeToString(dto),
                "TextSerializer", () => TextSerializer.SerializeToString(dto)
                );

            var stringStr = TypeSerializer.SerializeToString(dto);
            var textStr   = TextSerializer.SerializeToString(dto);

            //return;

            CompareMultipleRuns(
                "TypeSerializer", () => TypeSerializer.DeserializeFromString <T>(stringStr),
                "TextSerializer", () => TextSerializer.DeserializeFromString <T>(textStr)
                );

            var seraializedStringDto = TypeSerializer.DeserializeFromString <T>(stringStr);

            Assert.That(seraializedStringDto.Equals(dto), Is.True);

            var seraializedTextDto = TextSerializer.DeserializeFromString <T>(textStr);
            //Assert.That(seraializedTextDto.Equals(dto), Is.True);
        }
        public string SerializeToString <T>(T obj)
        {
            if (TextSerializer != null)
            {
                return(TextSerializer.SerializeToString(obj));
            }

            if (!UseBcl)
            {
                return(JsonSerializer.SerializeToString(obj));
            }

            if (obj == null)
            {
                return(null);
            }
            var type = obj.GetType();

            try
            {
                using (var ms = new MemoryStream())
                {
                    var serializer = new DataContractJsonSerializer(type);
                    serializer.WriteObject(ms, obj);
                    ms.Position = 0;
                    using (var sr = new StreamReader(ms))
                    {
                        return(sr.ReadToEnd());
                    }
                }
            }
            catch (Exception ex)
            {
                throw new SerializationException("JsonDataContractSerializer: Error converting type: " + ex.Message, ex);
            }
        }