﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Xml.Schema;

namespace Arch.CFramework.Utility
{
    /// <summary>
    /// Serializes and deserializes objects 
    /// </summary>
     static class ObjectSerializer
    {

        /// <summary>
        /// Serializes the specified System.Object to XML string.
        /// </summary>
        /// <param name="serialObject"></param>
        /// <returns></returns>
        public static string XmlSerializer(object serialObject)
        {
            XmlSerializer serializer = new XmlSerializer(serialObject.GetType());
            using (MemoryStream stream = new MemoryStream())
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Encoding = Encoding.UTF8;
                settings.Indent = true;
                settings.OmitXmlDeclaration = true;
                XmlWriter writer = XmlWriter.Create(stream, settings);

                XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                ns.Add("", "");
                serializer.Serialize(writer, serialObject, ns);
                writer.Close();
                return Encoding.UTF8.GetString(stream.ToArray());
            }
        }

        /// <summary>
        /// Deserializes the XML string to T object by the specified T.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="str"></param>
        /// <returns></returns>
        public static T XmlDeserialize<T>(string str) where T : class
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            using (StreamReader stream = new StreamReader(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(str)), System.Text.Encoding.UTF8))
            {
                return (T)serializer.Deserialize(stream);
            }
        }
     
        /// <summary>
        /// Serializes the specified System.Object to Json string.
        /// </summary>
        /// <param name="serialObject"></param>
        /// <returns></returns>
        public static string JsonSerializer(object serialObject) 
        {
            string szJson = "";
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(serialObject.GetType());
            using (MemoryStream stream = new MemoryStream())
            {
                serializer.WriteObject(stream, serialObject);
                szJson = Encoding.UTF8.GetString(stream.ToArray());
            }
            return szJson;
        }

        /// <summary>
        /// Deserializes the Json string to T object by the specified T.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="str"></param>
        /// <returns></returns>
        public static T JsonDeserialize<T>(string str) where T : class
        {
            using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(str)))
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
                return serializer.ReadObject(stream) as T;
            }
        }
    }
}
