|
@@ -3,9 +3,8 @@ using System;
|
|
|
using Vit.Core.Util.ConfigurationManager;
|
|
|
using System.Runtime.CompilerServices;
|
|
|
using System.Text;
|
|
|
-using Newtonsoft.Json.Linq;
|
|
|
using Vit.Extensions.Json_Extensions;
|
|
|
-using Vit.Extensions.Newtonsoft_Extensions;
|
|
|
+using Newtonsoft.Json.Converters;
|
|
|
|
|
|
namespace Vit.Core.Module.Serialization
|
|
|
{
|
|
@@ -23,16 +22,11 @@ namespace Vit.Core.Module.Serialization
|
|
|
|
|
|
|
|
|
|
|
|
- #region 基础对象
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- #region 成员对象
|
|
|
|
|
|
|
|
|
+ #region Member
|
|
|
public Encoding encoding { get; set; } = defaultEncoding;
|
|
|
- public string charset { get => encoding.GetCharset(); }
|
|
|
-
|
|
|
+ public string charset => encoding?.GetCharset();
|
|
|
#endregion
|
|
|
|
|
|
|
|
@@ -55,7 +49,7 @@ namespace Vit.Core.Module.Serialization
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
- #region (x.1)bytes <--> String
|
|
|
+ #region bytes <--> String
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
public string BytesToString(byte[] data, Encoding encoding = null)
|
|
@@ -69,7 +63,7 @@ namespace Vit.Core.Module.Serialization
|
|
|
{
|
|
|
return (encoding ?? this.encoding).GetBytes(data);
|
|
|
}
|
|
|
- #endregion
|
|
|
+ #endregion
|
|
|
|
|
|
|
|
|
|
|
@@ -79,7 +73,7 @@ namespace Vit.Core.Module.Serialization
|
|
|
///
|
|
|
/// </summary>
|
|
|
/// <param name="value"></param>
|
|
|
- /// <param name="type">必须为 where T : struct</param>
|
|
|
+ /// <param name="type">must be struct</param>
|
|
|
/// <returns></returns>
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
public object DeserializeStruct(string value, Type type)
|
|
@@ -87,51 +81,34 @@ namespace Vit.Core.Module.Serialization
|
|
|
try
|
|
|
{
|
|
|
if (type.IsStringType())
|
|
|
+ {
|
|
|
return value;
|
|
|
+ }
|
|
|
return value.Convert(type);
|
|
|
}
|
|
|
catch { }
|
|
|
return type.DefaultValue();
|
|
|
}
|
|
|
|
|
|
- ///// <summary>
|
|
|
- /////
|
|
|
- ///// </summary>
|
|
|
- ///// <typeparam name="T">必须为 where T : struct</typeparam>
|
|
|
- ///// <param name="value"></param>
|
|
|
- ///// <returns></returns>
|
|
|
- //public object DeserializeStruct<T>(string value)
|
|
|
- //{
|
|
|
- // return DeserializeStruct(value, typeof(T));
|
|
|
- //}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
#endregion
|
|
|
|
|
|
- #endregion
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
|
|
|
|
|
|
public readonly Newtonsoft.Json.JsonSerializerSettings serializeSetting = new Newtonsoft.Json.JsonSerializerSettings();
|
|
|
|
|
|
public Serialization_Newtonsoft()
|
|
|
{
|
|
|
-
|
|
|
- //忽略空值
|
|
|
+ // ignore properties with null value when serializing
|
|
|
serializeSetting.NullValueHandling = NullValueHandling.Ignore;
|
|
|
|
|
|
- //不缩进
|
|
|
+ // no pretty style
|
|
|
serializeSetting.Formatting = Formatting.None;
|
|
|
|
|
|
- //日期格式化
|
|
|
- var DateTimeFormat = Appsettings.json.GetByPath<string>("Vit.Serialization.DateTimeFormat")
|
|
|
- ?? "yyyy-MM-dd HH:mm:ss";
|
|
|
+ // serialize enum to string not int
|
|
|
+ serializeSetting.Converters.Add(new StringEnumConverter());
|
|
|
|
|
|
+ // DateTimeFormat
|
|
|
+ var DateTimeFormat = Appsettings.json.GetByPath<string>("Vit.Serialization.DateTimeFormat") ?? "yyyy-MM-dd HH:mm:ss";
|
|
|
serializeSetting.DateFormatHandling = global::Newtonsoft.Json.DateFormatHandling.IsoDateFormat;
|
|
|
serializeSetting.DateFormatString = DateTimeFormat;
|
|
|
}
|
|
@@ -140,53 +117,41 @@ namespace Vit.Core.Module.Serialization
|
|
|
|
|
|
|
|
|
|
|
|
- #region (x.1)object <--> String
|
|
|
+ #region #1 object <--> String
|
|
|
|
|
|
#region Serialize
|
|
|
|
|
|
/// <summary>
|
|
|
- /// T也可为值类型(例如 int?、bool)
|
|
|
+ /// value and type could be: byte[] / string / Object / Array / struct or ValueType(int? / bool)
|
|
|
/// </summary>
|
|
|
- /// <typeparam name="T"></typeparam>
|
|
|
/// <param name="value"></param>
|
|
|
+ /// <param name="type"></param>
|
|
|
/// <returns></returns>
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
- public string Serialize<T>(T value)
|
|
|
+ public string Serialize(object value, Type type = null)
|
|
|
{
|
|
|
- if (null == value) return null;
|
|
|
+ //if (null == value) return null;
|
|
|
|
|
|
- return Serialize(value, value.GetType());
|
|
|
- }
|
|
|
+ //if (value is Newtonsoft.Json.Linq.JToken token)
|
|
|
+ //{
|
|
|
+ // if (token.Type == JTokenType.String) return token.Value<string>();
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// T也可为值类型(例如 int?、bool)
|
|
|
- /// </summary>
|
|
|
- /// <param name="value"></param>
|
|
|
- /// <param name="type"></param>
|
|
|
- /// <returns></returns>
|
|
|
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
- public string Serialize(object value, Type type)
|
|
|
- {
|
|
|
- if (null == value) return null;
|
|
|
+ // return token.ToString(serializeSetting.Formatting);
|
|
|
+ //}
|
|
|
|
|
|
- if (value is Newtonsoft.Json.Linq.JToken token)
|
|
|
- {
|
|
|
- if (token.TypeMatch(JTokenType.String)) return token.ToString();
|
|
|
+ ////if (value is DateTime time)
|
|
|
+ ////{
|
|
|
+ //// return time.ToString(serializeSetting.DateFormatString);
|
|
|
+ ////}
|
|
|
|
|
|
- return token.ToString(serializeSetting.Formatting);
|
|
|
- }
|
|
|
+ //if (type == null) type = value.GetType();
|
|
|
|
|
|
- if (value is DateTime time)
|
|
|
- {
|
|
|
- return time.ToString(serializeSetting.DateFormatString);
|
|
|
- }
|
|
|
+ //if (type.IsValueType && type.GetUnderlyingTypeIfNullable() != typeof(DateTime))
|
|
|
+ //{
|
|
|
+ // return value.ToString();
|
|
|
+ //}
|
|
|
|
|
|
- if (type.TypeIsValueTypeOrStringType())
|
|
|
- {
|
|
|
- //return value.Convert<string>();
|
|
|
- return value.ToString();
|
|
|
- }
|
|
|
- return JsonConvert.SerializeObject(value, serializeSetting);
|
|
|
+ return JsonConvert.SerializeObject(value, type, serializeSetting);
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
@@ -194,33 +159,34 @@ namespace Vit.Core.Module.Serialization
|
|
|
#region Deserialize
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 使用Newtonsoft反序列化。T也可为值类型(例如 int?、bool)
|
|
|
+ /// T could be: byte[] / string / Object / Array / struct or ValueType(int? / bool)
|
|
|
/// </summary>
|
|
|
/// <param name="value"></param>
|
|
|
/// <returns></returns>
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
public T Deserialize<T>(string value)
|
|
|
{
|
|
|
- //return (T)Deserialize(value,typeof(T));
|
|
|
- if (null == value) return default;
|
|
|
+ ////return (T)Deserialize(value,typeof(T));
|
|
|
+ //if (null == value) return default;
|
|
|
|
|
|
- Type type = typeof(T);
|
|
|
+ //Type type = typeof(T);
|
|
|
|
|
|
- if (type.GetUnderlyingTypeIfNullable().IsEnum)
|
|
|
- return value.StringToEnum<T>();
|
|
|
+ //if (type.GetUnderlyingTypeIfNullable().IsEnum)
|
|
|
+ // return value.StringToEnum<T>();
|
|
|
|
|
|
- if (type.TypeIsValueTypeOrStringType())
|
|
|
- return (T)DeserializeStruct(value, type);
|
|
|
+ ////if (type.TypeIsValueTypeOrStringType())
|
|
|
+ //if (type.IsValueType && type.GetUnderlyingTypeIfNullable() != typeof(DateTime))
|
|
|
+ // return (T)DeserializeStruct(value, type);
|
|
|
|
|
|
|
|
|
- //if (string.IsNullOrWhiteSpace(value)) return type.DefaultValue();
|
|
|
+ ////if (string.IsNullOrWhiteSpace(value)) return type.DefaultValue();
|
|
|
|
|
|
- return JsonConvert.DeserializeObject<T>(value);
|
|
|
+ return JsonConvert.DeserializeObject<T>(value, serializeSetting);
|
|
|
}
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 使用Newtonsoft反序列化。T也可为值类型(例如 int?、bool)
|
|
|
+ /// value and type could be: byte[] / string / Object / Array / struct or ValueType(int? / bool)
|
|
|
/// </summary>
|
|
|
/// <param name="value"></param>
|
|
|
/// <param name="type"></param>
|
|
@@ -228,17 +194,18 @@ namespace Vit.Core.Module.Serialization
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
public object Deserialize(string value, Type type)
|
|
|
{
|
|
|
- if (null == value || null == type) return null;
|
|
|
+ //if (null == value || null == type) return null;
|
|
|
|
|
|
- if (type.GetUnderlyingTypeIfNullable().IsEnum)
|
|
|
- return value.StringToEnum(type);
|
|
|
+ //if (type.GetUnderlyingTypeIfNullable().IsEnum)
|
|
|
+ // return value.StringToEnum(type);
|
|
|
|
|
|
- if (type.TypeIsValueTypeOrStringType())
|
|
|
- return DeserializeStruct(value, type);
|
|
|
+ ////if (type.TypeIsValueTypeOrStringType())
|
|
|
+ //if (type.IsValueType && type.GetUnderlyingTypeIfNullable() != typeof(DateTime))
|
|
|
+ // return DeserializeStruct(value, type);
|
|
|
|
|
|
- //if (string.IsNullOrWhiteSpace(value)) return type.DefaultValue();
|
|
|
+ ////if (string.IsNullOrWhiteSpace(value)) return type.DefaultValue();
|
|
|
|
|
|
- return JsonConvert.DeserializeObject(value, type);
|
|
|
+ return JsonConvert.DeserializeObject(value, type, serializeSetting);
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
@@ -249,32 +216,20 @@ namespace Vit.Core.Module.Serialization
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
- #region (x.2)object <--> bytes
|
|
|
+ #region #2 object <--> bytes
|
|
|
|
|
|
#region SerializeToBytes
|
|
|
- /// <summary>
|
|
|
- /// T 可以为 byte[]、string、 object 、struct
|
|
|
- /// </summary>
|
|
|
- /// <typeparam name="T"></typeparam>
|
|
|
- /// <param name="obj"></param>
|
|
|
- /// <returns></returns>
|
|
|
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
- public byte[] SerializeToBytes<T>(T obj)
|
|
|
- {
|
|
|
- return SerializeToBytes<T>(obj, null);
|
|
|
- }
|
|
|
+
|
|
|
|
|
|
/// <summary>
|
|
|
- /// T 可以为 byte[]、string、 object 、struct
|
|
|
+ /// value and type could be: byte[] / string / Object / Array / struct or ValueType(int? / bool)
|
|
|
/// </summary>
|
|
|
- /// <typeparam name="T"></typeparam>
|
|
|
/// <param name="obj"></param>
|
|
|
+ /// <param name="type"></param>
|
|
|
/// <param name="encoding"></param>
|
|
|
/// <returns></returns>
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
- public byte[] SerializeToBytes<T>(T obj, Encoding encoding)
|
|
|
+ public byte[] SerializeToBytesWithEncoding(object obj, Type type = null, Encoding encoding = null)
|
|
|
{
|
|
|
if (null == obj) return null;
|
|
|
|
|
@@ -286,25 +241,27 @@ namespace Vit.Core.Module.Serialization
|
|
|
case ArraySegment<byte> asbs:
|
|
|
return asbs.ArraySegmentByteToBytes();
|
|
|
case string str:
|
|
|
- strValue = str; break;
|
|
|
- default: strValue = Serialize(obj); break;
|
|
|
+ strValue = str;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ strValue = Serialize(obj, type);
|
|
|
+ break;
|
|
|
}
|
|
|
-
|
|
|
return StringToBytes(strValue, encoding);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
- /// type 可以为 byte[]、string、 object 、struct
|
|
|
+ /// value and type could be: byte[] / string / Object / Array / struct or ValueType(int? / bool)
|
|
|
/// </summary>
|
|
|
/// <param name="value"></param>
|
|
|
/// <param name="type"></param>
|
|
|
/// <returns></returns>
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
- public byte[] SerializeToBytes(object value, Type type)
|
|
|
+ public byte[] SerializeToBytes(object value, Type type = null)
|
|
|
{
|
|
|
- return SerializeToBytes(value);
|
|
|
+ return SerializeToBytesWithEncoding(value, type);
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
@@ -381,10 +338,7 @@ namespace Vit.Core.Module.Serialization
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- #region (x.4)object <--> ArraySegmentByte
|
|
|
+ #region #3 object <--> ArraySegmentByte
|
|
|
|
|
|
#region SerializeToArraySegmentByte
|
|
|
/// <summary>
|