C# 扩展方法—— 序列化与反序列化

2021-01-22 08:12

阅读:519

标签:inf   日期   custom   strong   contract   comm   soft   tar   sub   

其他扩展方法详见: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 { new IsoDateTimeConverter() },
                ContractResolver = new CustomContractResolver(),
                ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
            };
        }
 
        /// 
        /// Serialize an object to json string.
        /// 
        /// 
        /// 
        public static string Serialize(this object obj)
        {
            return obj == null ? null : JsonConvert.SerializeObject(obj, Settings);
        }
 
        /// 
        /// Serialize an object to json string.
        /// 
        /// 
        /// 
        /// 
        public static string SerializeObjectTime(this object obj, string joinChar = "/")
        {
            Settings = new JsonSerializerSettings
            {
                Converters =
                    new List
                    {
                        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
                    {
                        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
                    {
                        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);
        }
 
        /// 
        /// Deserialize a json string to an object.
        /// 
        /// 
        /// 
        /// 
        public static object Deserialize(this string value, Type type)
        {
            return JsonConvert.DeserializeObject(value, type, Settings);
        }
 
        /// 
        /// Deserialize a json string to a strong type object.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T Deserialize(this string value, string joinChar = "/") where T : class
        {
            Settings = new JsonSerializerSettings
            {
                Converters = new List { new IsoDateTimeConverter() { DateTimeFormat = string.Format("yyyy{0}MM{0}dd", joinChar) } },
                DateTimeZoneHandling = DateTimeZoneHandling.Utc
            };
            return JsonConvert.DeserializeObject(value, Settings);
        }
 
        /// 
        /// Deserialize a json string to a strong type object.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T DeserializeNoSecond(this string value, string joinChar = "/") where T : class
        {
            Settings = new JsonSerializerSettings
            {
                Converters = new List { new IsoDateTimeConverter() { DateTimeFormat = string.Format("yyyy{0}MM{0}dd HH:mm", joinChar) } },
                DateTimeZoneHandling = DateTimeZoneHandling.Utc
            };
            return JsonConvert.DeserializeObject(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;
            }
        }
    }
}

  

C# 扩展方法—— 序列化与反序列化

标签:inf   日期   custom   strong   contract   comm   soft   tar   sub   

原文地址:https://www.cnblogs.com/zhuanjiao/p/12083377.html


评论


亲,登录后才可以留言!