zoukankan      html  css  js  c++  java
  • C# 扩展方法——序列化与反序列化

    其他扩展方法详见:https://www.cnblogs.com/zhuanjiao/p/12060937.html

    主要是是对日期格式的处理

    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;
    using Newtonsoft.Json.Serialization;
    using System;
    using System.Collections.Generic;
    using System.Reflection;
     
    namespace CoSubject.Common.JsonNet
    {
        public static class NewtonsoftJsonSerializer
        {
            public static JsonSerializerSettings Settings { get; private set; }
     
            static NewtonsoftJsonSerializer()
            {
                Settings = new JsonSerializerSettings
                {
                    Converters = new List<JsonConverter> { new IsoDateTimeConverter() },
                    ContractResolver = new CustomContractResolver(),
                    ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
                };
            }
     
            /// <summary>
            /// Serialize an object to json string.
            /// </summary>
            /// <param name="obj"></param>
            /// <returns></returns>
            public static string Serialize(this object obj)
            {
                return obj == null ? null : JsonConvert.SerializeObject(obj, Settings);
            }
     
            /// <summary>
            /// Serialize an object to json string.
            /// </summary>
            /// <param name="obj"></param>
            /// <param name="joinChar"></param>
            /// <returns></returns>
            public static string SerializeObjectTime(this object obj, string joinChar = "/")
            {
                Settings = new JsonSerializerSettings
                {
                    Converters =
                        new List<JsonConverter>
                        {
                            new IsoDateTimeConverter()
                            {
                                DateTimeFormat =
                                    string.Format( "yyyy{0}MM{0}dd HH:mm:ss",joinChar)
                            }
                        },
                    ContractResolver = new CustomContractResolver(),
                    ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
                };
                return obj == null ? null : JsonConvert.SerializeObject(obj, Settings);
            }
     
            public static string SerializeObjectTimeNoSecond(this object obj, string joinChar = "/")
            {
                Settings = new JsonSerializerSettings
                {
                    Converters =
                        new List<JsonConverter>
                        {
                            new IsoDateTimeConverter()
                            {
                                DateTimeFormat = string.Format( "yyyy{0}MM{0}dd HH:mm", joinChar)
                            }
                        },
                    ContractResolver = new CustomContractResolver(),
                    ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
                };
                return obj == null ? null : JsonConvert.SerializeObject(obj, Settings);
            }
     
            public static string SerializeObjectDate(this object obj, string joinChar = "/")
            {
                Settings = new JsonSerializerSettings
                {
                    Converters =
                        new List<JsonConverter>
                        {
                            new IsoDateTimeConverter()
                            {
                                DateTimeFormat = string.Format( "yyyy{0}MM{0}dd", joinChar)
                            }
                        },
                    ContractResolver = new CustomContractResolver(),
                    ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
                };
                return obj == null ? null : JsonConvert.SerializeObject(obj, Settings);
            }
     
            /// <summary>
            /// Deserialize a json string to an object.
            /// </summary>
            /// <param name="value"></param>
            /// <param name="type"></param>
            /// <returns></returns>
            public static object Deserialize(this string value, Type type)
            {
                return JsonConvert.DeserializeObject(value, type, Settings);
            }
     
            /// <summary>
            /// Deserialize a json string to a strong type object.
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="value"></param>
            /// <param name="joinChar"></param>
            /// <returns></returns>
            public static T Deserialize<T>(this string value, string joinChar = "/") where T : class
            {
                Settings = new JsonSerializerSettings
                {
                    Converters = new List<JsonConverter> { new IsoDateTimeConverter() { DateTimeFormat = string.Format("yyyy{0}MM{0}dd", joinChar) } },
                    DateTimeZoneHandling = DateTimeZoneHandling.Utc
                };
                return JsonConvert.DeserializeObject<T>(value, Settings);
            }
     
            /// <summary>
            /// Deserialize a json string to a strong type object.
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="value"></param>
            /// <param name="joinChar"></param>
            /// <returns></returns>
            public static T DeserializeNoSecond<T>(this string value, string joinChar = "/") where T : class
            {
                Settings = new JsonSerializerSettings
                {
                    Converters = new List<JsonConverter> { new IsoDateTimeConverter() { DateTimeFormat = string.Format("yyyy{0}MM{0}dd HH:mm", joinChar) } },
                    DateTimeZoneHandling = DateTimeZoneHandling.Utc
                };
                return JsonConvert.DeserializeObject<T>(value, Settings);
            }
     
            class CustomContractResolver : DefaultContractResolver
            {
                protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
                {
                    var jsonProperty = base.CreateProperty(member, memberSerialization);
                    if (jsonProperty.Writable) return jsonProperty;
                    var property = member as PropertyInfo;
                    if (property == null) return jsonProperty;
                    var hasPrivateSetter = property.GetSetMethod(true) != null;
                    jsonProperty.Writable = hasPrivateSetter;
                    return jsonProperty;
                }
            }
        }
    }
    

      

  • 相关阅读:
    MySQL用户信息表中主键userID自动增加问题
    PHP输出当前系统时间
    PHP连接MySQL方式比较问题
    DWZ分页处理
    NHibernate ICriteria 查询[转 十年如一]
    HttpContext.Current.Request.Files后台取不到值的解决方法 [转]
    Hibernate Projections(投影、统计、不重复结果)[转]
    细说Form(表单)[ 转 Fish Li]
    HTML <a> 标签的 rel 属性
    dwz rel
  • 原文地址:https://www.cnblogs.com/zhuanjiao/p/12083377.html
Copyright © 2011-2022 走看看