using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Text;
using System.Web. .Serialization;



[Serializable] public static class JS er { /// <summary> /// 对象转JSON /// </summary> /// <param name=\"obj\">对象</param> /// <returns>JSON格式的字符串</returns> public static string ToJSON( obj) { Serializer jss = new Serializer(); try { return jss.Serialize(obj); } catch { } return null; } /// <summary> /// /// JSON文本转对象,泛型方法 /// </summary> /// <typeparam name=\"T\">类型</typeparam> /// <param name=\"jsonText\">JSON文本</param> /// <returns>指定类型的对象</returns> public static T JSONTo <T>(string jsonText) { Serializer jss = new Serializer(); try { return jss.Deserialize<T>(jsonText); } catch{} return default(T); } public static string ToJsonDateTime( obj) { Serializer jss = new Serializer(); jss.RegisterConverters(new Converter[] { new DateTimeConverter() }); try { return jss.Serialize(obj); } catch { } return null; } }


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web. .Serialization;
    


public class DateTimeConverter :  Converter
    {
        public override   Deserialize(IDictionary<string,  > dictionary, Type type,  Serializer serializer)
        {
            return new  Serializer().ConvertToType(dictionary, type);
        }

        public override IDictionary<string,  > Serialize(  obj,  Serializer serializer)
        {
            if (!(obj is DateTime))
            {
                return null;
            }
            return new CustomString(((DateTime)obj).ToString(\"yyyy-MM-dd HH:mm:ss\"));
        }

        public override IEnumerable<Type> SupportedTypes
        {
            get
            {
                return new[] { typeof(DateTime) };
            }
        }

        private class CustomString : Uri, IDictionary<string,  >
        {
            public CustomString(string str) :  (str, UriKind.Relative)
            {
            }

            void IDictionary<string,  >.Add(string key,   value)
            {
                throw new NotImplementedException();
            }

            bool IDictionary<string,  >.ContainsKey(string key)
            {
                throw new NotImplementedException();
            }

            ICollection<string> IDictionary<string,  >.Keys
            {
                get
                {
                    throw new NotImplementedException();
                }

            }

            bool IDictionary<string,  >.Remove(string key)
            {
                throw new NotImplementedException();
            }


            bool IDictionary<string,  >.TryGetValue(string key, out   value)
            {
                throw new NotImplementedException();

            }

            ICollection< > IDictionary<string,  >.Values
            {

                get
                {
                    throw new NotImplementedException();
                }

            }

              IDictionary<string,  >.this[string key]
            {
                get
                {
                    throw new NotImplementedException();
                }
                set
                {
                    throw new NotImplementedException();
                }
            }

            void ICollection<KeyValuePair<string,  >>.Add(KeyValuePair<string,  > item)
            {
                throw new NotImplementedException();
            }

            void ICollection<KeyValuePair<string,  >>.Clear()
            {
                throw new NotImplementedException();
            }

            bool ICollection<KeyValuePair<string,  >>.Contains(KeyValuePair<string,  > item)
            {

                throw new NotImplementedException();

            }

            void ICollection<KeyValuePair<string,  >>.CopyTo(KeyValuePair<string,  >[] array, int arrayIndex)
            {
                throw new NotImplementedException();

            }

            int ICollection<KeyValuePair<string,  >>.Count
            {
                get
                {
                    throw new NotImplementedException();
                }
            }

            bool ICollection<KeyValuePair<string,  >>.IsReadOnly
            {
                get
                {
                    throw new NotImplementedException();
                }
            }

            bool ICollection<KeyValuePair<string,  >>.Remove(KeyValuePair<string,  > item)
            {
                throw new NotImplementedException();
            }

            IEnumerator<KeyValuePair<string,  >> IEnumerable<KeyValuePair<string,  >>.GetEnumerator()
            {
                throw new NotImplementedException();
            }

            IEnumerator IEnumerable.GetEnumerator()
            {
                throw new NotImplementedException();
            }
        }
    }

  

  

收藏 打印