/// <summary>
        /// Deserializes from stream.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="type">The type.</param>
        /// <param name="format">The format.</param>
        /// <returns>System.Object.</returns>
        /// <exception cref="System.NotImplementedException"></exception>
        protected object DeserializeFromStream(Stream stream, Type type, SerializationFormats format)
        {
            if (format == SerializationFormats.Json)
            {
                return(JsonSerializer.DeserializeFromStream(stream, type));
            }

            throw new NotImplementedException();
        }
        /// <summary>
        /// Converts an instance of an object to a string format
        /// </summary>
        /// <param name="format">Specifies if it should convert to XML, BSON or JSON</param>
        /// <returns>The object, converted to a string representation</returns>
        public string ToString(SerializationFormats format)
        {
            Encryption64 encryptor = new Encryption64();
            List <DataLayer.FooEnterprises.Product.SerializableProduct> zs = new List <DataLayer.FooEnterprises.Product.SerializableProduct>();

            foreach (Product z in this)
            {
                DataLayer.FooEnterprises.Product.SerializableProduct serializableProduct = new DataLayer.FooEnterprises.Product.SerializableProduct();
                serializableProduct.ProductId = z.IsNull(Product.Fields.ProductId)
                    ? (int?)null : z.ProductId;
                serializableProduct.ProductName = z.IsNull(Product.Fields.ProductName)
                    ? null : z.ProductName;
                serializableProduct.WholesaleCost = z.IsNull(Product.Fields.WholesaleCost)
                    ? (decimal?)null : z.WholesaleCost;
                serializableProduct.SaleCost = z.IsNull(Product.Fields.SaleCost)
                    ? (decimal?)null : z.SaleCost;
                serializableProduct.SerializationIsUpdate         = z.LayerGenIsUpdate();
                serializableProduct.SerializationConnectionString = encryptor.Encrypt(z.LayerGenConnectionString(), DataLayer.FooEnterprises.Universal.LayerGenEncryptionKey);
                zs.Add(serializableProduct);
            }

            if (format == SerializationFormats.Json)
            {
                return(Newtonsoft.Json.JsonConvert.SerializeObject(zs));
            }

            if (format == SerializationFormats.Xml)
            {
                System.Xml.Serialization.XmlSerializer xType = new System.Xml.Serialization.XmlSerializer(zs.GetType());

                using (System.IO.StringWriter sw = new System.IO.StringWriter())
                {
                    xType.Serialize(sw, zs);
                    return(sw.ToString());
                }
            }

            if (format == SerializationFormats.BsonBase64)
            {
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                {
                    using (Newtonsoft.Json.Bson.BsonWriter writer = new Newtonsoft.Json.Bson.BsonWriter(ms))
                    {
                        Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer();
                        serializer.Serialize(writer, zs);
                    }
                    return(Convert.ToBase64String(ms.ToArray()));
                }
            }

            return("");
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Converts an instance of an object to a string format
        /// </summary>
        /// <param name="format">Specifies if it should convert to XML, BSON or JSON</param>
        /// <returns>The object, converted to a string representation</returns>
        public string ToString(SerializationFormats format)
        {
            Encryption64 encryptor = new Encryption64();
            List <DataLayer.Customer.SerializableCustomer> zs = new List <DataLayer.Customer.SerializableCustomer>();

            foreach (Customer z in this)
            {
                DataLayer.Customer.SerializableCustomer serializableCustomer = new DataLayer.Customer.SerializableCustomer();
                serializableCustomer.CustomerId = z.IsNull(Customer.Fields.CustomerId)
                    ? (int?)null : z.CustomerId;
                serializableCustomer.FirstName = z.IsNull(Customer.Fields.FirstName)
                    ? null : z.FirstName;
                serializableCustomer.LastName = z.IsNull(Customer.Fields.LastName)
                    ? null : z.LastName;
                serializableCustomer.SerializationIsUpdate         = z.LayerGenIsUpdate();
                serializableCustomer.SerializationConnectionString = encryptor.Encrypt(z.LayerGenConnectionString(), DataLayer.Universal.LayerGenEncryptionKey);
                zs.Add(serializableCustomer);
            }

            if (format == SerializationFormats.Json)
            {
                return(Newtonsoft.Json.JsonConvert.SerializeObject(zs));
            }

            if (format == SerializationFormats.Xml)
            {
                System.Xml.Serialization.XmlSerializer xType = new System.Xml.Serialization.XmlSerializer(zs.GetType());

                using (System.IO.StringWriter sw = new System.IO.StringWriter())
                {
                    xType.Serialize(sw, zs);
                    return(sw.ToString());
                }
            }

            if (format == SerializationFormats.BsonBase64)
            {
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                {
                    using (Newtonsoft.Json.Bson.BsonWriter writer = new Newtonsoft.Json.Bson.BsonWriter(ms))
                    {
                        Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer();
                        serializer.Serialize(writer, zs);
                    }
                    return(Convert.ToBase64String(ms.ToArray()));
                }
            }

            return("");
        }