From bcca8458f6a317239241c417c707067cfb8c364f Mon Sep 17 00:00:00 2001 From: Savorboard Date: Wed, 11 Oct 2017 23:12:27 +0800 Subject: [PATCH] update add jsonproperty --- src/DotNetCore.CAP/Infrastructure/Helper.cs | 194 ++++++++++---------- 1 file changed, 102 insertions(+), 92 deletions(-) diff --git a/src/DotNetCore.CAP/Infrastructure/Helper.cs b/src/DotNetCore.CAP/Infrastructure/Helper.cs index f0b274a..1574886 100644 --- a/src/DotNetCore.CAP/Infrastructure/Helper.cs +++ b/src/DotNetCore.CAP/Infrastructure/Helper.cs @@ -7,96 +7,106 @@ using Newtonsoft.Json.Linq; namespace DotNetCore.CAP.Infrastructure { - public static class Helper - { - private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Local); - private static JsonSerializerSettings _serializerSettings; - - public static void SetSerializerSettings(JsonSerializerSettings setting) - { - _serializerSettings = setting; - } - - public static string ToJson(object value) - { - return value != null - ? JsonConvert.SerializeObject(value, _serializerSettings) - : null; - } - - public static T FromJson(string value) - { - return value != null - ? JsonConvert.DeserializeObject(value, _serializerSettings) - : default(T); - } - - public static object FromJson(string value, Type type) - { - if (type == null) throw new ArgumentNullException(nameof(type)); - - return value != null - ? JsonConvert.DeserializeObject(value, type, _serializerSettings) - : null; - } - - public static long ToTimestamp(DateTime value) - { - var elapsedTime = value - Epoch; - return (long) elapsedTime.TotalSeconds; - } - - public static DateTime FromTimestamp(long value) - { - // ReSharper disable once ImpureMethodCallOnReadonlyValueField - return Epoch.AddSeconds(value); - } - - public static bool IsController(TypeInfo typeInfo) - { - if (!typeInfo.IsClass) - return false; - - if (typeInfo.IsAbstract) - return false; - - if (!typeInfo.IsPublic) - return false; - - return !typeInfo.ContainsGenericParameters - && typeInfo.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase); - } - - public static bool IsComplexType(Type type) - { - return !CanConvertFromString(type); - } - - public static string AddJsonProperty(string json, IList> properties) - { - var jObj = JObject.Parse(json); - foreach (var property in properties) - jObj.Add(new JProperty(property.Key, property.Value)); - return jObj.ToString(); - } - - private static bool CanConvertFromString(Type destinationType) - { - destinationType = Nullable.GetUnderlyingType(destinationType) ?? destinationType; - return IsSimpleType(destinationType) || - TypeDescriptor.GetConverter(destinationType).CanConvertFrom(typeof(string)); - } - - private static bool IsSimpleType(Type type) - { - return type.GetTypeInfo().IsPrimitive || - type == typeof(decimal) || - type == typeof(string) || - type == typeof(DateTime) || - type == typeof(Guid) || - type == typeof(DateTimeOffset) || - type == typeof(TimeSpan) || - type == typeof(Uri); - } - } + public static class Helper + { + private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Local); + private static JsonSerializerSettings _serializerSettings; + + public static void SetSerializerSettings(JsonSerializerSettings setting) + { + _serializerSettings = setting; + } + + public static string ToJson(object value) + { + return value != null + ? JsonConvert.SerializeObject(value, _serializerSettings) + : null; + } + + public static T FromJson(string value) + { + return value != null + ? JsonConvert.DeserializeObject(value, _serializerSettings) + : default(T); + } + + public static object FromJson(string value, Type type) + { + if (type == null) throw new ArgumentNullException(nameof(type)); + + return value != null + ? JsonConvert.DeserializeObject(value, type, _serializerSettings) + : null; + } + + public static long ToTimestamp(DateTime value) + { + var elapsedTime = value - Epoch; + return (long)elapsedTime.TotalSeconds; + } + + public static DateTime FromTimestamp(long value) + { + // ReSharper disable once ImpureMethodCallOnReadonlyValueField + return Epoch.AddSeconds(value); + } + + public static bool IsController(TypeInfo typeInfo) + { + if (!typeInfo.IsClass) + return false; + + if (typeInfo.IsAbstract) + return false; + + if (!typeInfo.IsPublic) + return false; + + return !typeInfo.ContainsGenericParameters + && typeInfo.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase); + } + + public static bool IsComplexType(Type type) + { + return !CanConvertFromString(type); + } + + public static string AddJsonProperty(string json, IList> properties) + { + var jObj = JObject.Parse(json); + foreach (var property in properties) + { + if (jObj.TryGetValue(property.Key, out var _)) + { + jObj[property.Key] = property.Value; + } + else + { + jObj.Add(new JProperty(property.Key, property.Value)); + } + } + + return jObj.ToString(); + } + + private static bool CanConvertFromString(Type destinationType) + { + destinationType = Nullable.GetUnderlyingType(destinationType) ?? destinationType; + return IsSimpleType(destinationType) || + TypeDescriptor.GetConverter(destinationType).CanConvertFrom(typeof(string)); + } + + private static bool IsSimpleType(Type type) + { + return type.GetTypeInfo().IsPrimitive || + type == typeof(decimal) || + type == typeof(string) || + type == typeof(DateTime) || + type == typeof(Guid) || + type == typeof(DateTimeOffset) || + type == typeof(TimeSpan) || + type == typeof(Uri); + } + } } \ No newline at end of file