Ejemplo n.º 1
0
        public String Serialize <T>(T value, SerializeType SerType)
        {
            List <Type> currentTypes = ExtensionManager.GetObjectTypes();

            if (!currentTypes.Contains(typeof(T)))
            {
                currentTypes.Add(typeof(T));
            }


            switch (SerType)
            {
            case SerializeType.JSON:
                return(JsonConvert.SerializeObject(value, Newtonsoft.Json.Formatting.Indented));

            case SerializeType.XML:
                using (var stringWriter = new StringWriter(CultureInfo.InvariantCulture))
                {
                    var writer = XmlWriter.Create(stringWriter, new XmlWriterSettings {
                        Indent = true, IndentChars = "\t"
                    });

                    var dataContractSerializer = new DataContractSerializer(typeof(T), currentTypes);
                    dataContractSerializer.WriteObject(writer, value);
                    writer.Flush();
                    return(stringWriter.ToString());
                }

            default:
                return(value.ToString());
            }
        }