From 97f58456c2a65b0018b6c379bdc5b652b47b29c0 Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Tue, 19 Oct 2021 10:32:30 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20XmlSerializer=20?= =?UTF-8?q?=E6=BD=9C=E5=9C=A8=E7=9A=84=E5=86=85=E5=AD=98=E6=B3=84=E6=BC=8F?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WechatApiClientEventExtensions.cs | 5 +-- .../Utilities/Internal/XmlUtility.cs | 43 ++++++++++++++----- ...chatOpenAIPlatformClientEventExtensions.cs | 6 +-- .../Utilities/Internal/XmlUtility.cs | 43 ++++++++++++++----- .../WechatWorkClientEventExtensions.cs | 6 +-- .../Utilities/Internal/XmlUtility.cs | 43 ++++++++++++++----- 6 files changed, 99 insertions(+), 47 deletions(-) diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientEventExtensions.cs b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientEventExtensions.cs index ffb5b645..490beddb 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientEventExtensions.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientEventExtensions.cs @@ -79,10 +79,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api callbackXml = Utilities.WxBizMsgCryptor.AESDecrypt(cipherText: encryptedXml!, encodingAESKey: client.Credentials.PushEncodingAESKey!, out _); } - using var reader = new StringReader(callbackXml); - - XmlSerializer xmlSerializer = new XmlSerializer(typeof(TEvent), new XmlRootAttribute("xml")); - return (TEvent)xmlSerializer.Deserialize(reader)!; + return Utilities.XmlUtility.Deserialize(callbackXml); } catch (WechatApiException) { diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/Utilities/Internal/XmlUtility.cs b/src/SKIT.FlurlHttpClient.Wechat.Api/Utilities/Internal/XmlUtility.cs index cd7f4ec2..57a6bd4f 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.Api/Utilities/Internal/XmlUtility.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/Utilities/Internal/XmlUtility.cs @@ -13,6 +13,21 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Utilities // REF: https://docs.microsoft.com/zh-cn/dotnet/api/system.xml.serialization.xmlserializer#dynamically-generated-assemblies private static Hashtable _serializers = new Hashtable(); + private static XmlSerializer GetTypedSerializer(Type type) + { + if (type == null) throw new ArgumentNullException(nameof(type)); + + string skey = type.AssemblyQualifiedName; + XmlSerializer? xmlSerializer = (XmlSerializer)_serializers[skey]; + if (xmlSerializer == null) + { + xmlSerializer = new XmlSerializer(type, new XmlRootAttribute("xml")); + _serializers[skey] = xmlSerializer; + } + + return xmlSerializer; + } + public static string Serialize(Type type, object obj) { string xml; @@ -24,19 +39,12 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Utilities settings.WriteEndDocumentOnClose = false; settings.NamespaceHandling = NamespaceHandling.OmitDuplicates; - string skey = type.AssemblyQualifiedName; - XmlSerializer? xmlSerializer = (XmlSerializer)_serializers[skey]; - if (xmlSerializer == null) - { - xmlSerializer = new XmlSerializer(type, new XmlRootAttribute("xml")); - _serializers[skey] = xmlSerializer; - } - using var stream = new MemoryStream(); using var writer = XmlWriter.Create(stream, settings); - XmlSerializerNamespaces xmlNamespace = new XmlSerializerNamespaces(); - xmlNamespace.Add(string.Empty, string.Empty); - xmlSerializer.Serialize(writer, obj, xmlNamespace); + XmlSerializer serializer = GetTypedSerializer(type); + XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); + ns.Add(string.Empty, string.Empty); + serializer.Serialize(writer, obj, ns); writer.Flush(); xml = Encoding.UTF8.GetString(stream.ToArray()); xml = Regex.Replace(xml, "\\s*<\\w+ (xsi|d2p1):nil=\"true\"[^>]*/>", string.Empty, RegexOptions.IgnoreCase); @@ -50,5 +58,18 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Utilities { return Serialize(typeof(T), obj); } + + public static object Deserialize(Type type, string xml) + { + using var reader = new StringReader(xml); + XmlSerializer serializer = GetTypedSerializer(type); + return serializer.Deserialize(reader); + } + + public static T Deserialize(string xml) + where T : class + { + return (T)Deserialize(typeof(T), xml); + } } } diff --git a/src/SKIT.FlurlHttpClient.Wechat.OpenAI/Extensions/WechatOpenAIPlatformClientEventExtensions.cs b/src/SKIT.FlurlHttpClient.Wechat.OpenAI/Extensions/WechatOpenAIPlatformClientEventExtensions.cs index 5fc79c66..f7072c4d 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.OpenAI/Extensions/WechatOpenAIPlatformClientEventExtensions.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.OpenAI/Extensions/WechatOpenAIPlatformClientEventExtensions.cs @@ -41,11 +41,7 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI throw new Exceptions.WechatOpenAIEventSerializationException("Encrypt event failed, because of empty encrypted data."); callbackXml = Utilities.WxBizMsgCryptor.AESDecrypt(cipherText: encryptedXml!, encodingAESKey: client.Credentials.EncodingAESKey!, out _); - - using var reader = new StringReader(callbackXml); - - XmlSerializer xmlSerializer = new XmlSerializer(typeof(TEvent), new XmlRootAttribute("xml")); - return (TEvent)xmlSerializer.Deserialize(reader)!; + return Utilities.XmlUtility.Deserialize(callbackXml); } catch (WechatOpenAIException) { diff --git a/src/SKIT.FlurlHttpClient.Wechat.OpenAI/Utilities/Internal/XmlUtility.cs b/src/SKIT.FlurlHttpClient.Wechat.OpenAI/Utilities/Internal/XmlUtility.cs index 92d2ffdf..38d62f3c 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.OpenAI/Utilities/Internal/XmlUtility.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.OpenAI/Utilities/Internal/XmlUtility.cs @@ -13,6 +13,21 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Utilities // REF: https://docs.microsoft.com/zh-cn/dotnet/api/system.xml.serialization.xmlserializer#dynamically-generated-assemblies private static Hashtable _serializers = new Hashtable(); + private static XmlSerializer GetTypedSerializer(Type type) + { + if (type == null) throw new ArgumentNullException(nameof(type)); + + string skey = type.AssemblyQualifiedName; + XmlSerializer? xmlSerializer = (XmlSerializer)_serializers[skey]; + if (xmlSerializer == null) + { + xmlSerializer = new XmlSerializer(type, new XmlRootAttribute("xml")); + _serializers[skey] = xmlSerializer; + } + + return xmlSerializer; + } + public static string Serialize(Type type, object obj) { string xml; @@ -24,19 +39,12 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Utilities settings.WriteEndDocumentOnClose = false; settings.NamespaceHandling = NamespaceHandling.OmitDuplicates; - string skey = type.AssemblyQualifiedName; - XmlSerializer? xmlSerializer = (XmlSerializer)_serializers[skey]; - if (xmlSerializer == null) - { - xmlSerializer = new XmlSerializer(type, new XmlRootAttribute("xml")); - _serializers[skey] = xmlSerializer; - } - using var stream = new MemoryStream(); using var writer = XmlWriter.Create(stream, settings); - XmlSerializerNamespaces xmlNamespace = new XmlSerializerNamespaces(); - xmlNamespace.Add(string.Empty, string.Empty); - xmlSerializer.Serialize(writer, obj, xmlNamespace); + XmlSerializer serializer = GetTypedSerializer(type); + XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); + ns.Add(string.Empty, string.Empty); + serializer.Serialize(writer, obj, ns); writer.Flush(); xml = Encoding.UTF8.GetString(stream.ToArray()); xml = Regex.Replace(xml, "\\s*<\\w+ (xsi|d2p1):nil=\"true\"[^>]*/>", string.Empty, RegexOptions.IgnoreCase); @@ -50,5 +58,18 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Utilities { return Serialize(typeof(T), obj); } + + public static object Deserialize(Type type, string xml) + { + using var reader = new StringReader(xml); + XmlSerializer serializer = GetTypedSerializer(type); + return serializer.Deserialize(reader); + } + + public static T Deserialize(string xml) + where T : class + { + return (T)Deserialize(typeof(T), xml); + } } } diff --git a/src/SKIT.FlurlHttpClient.Wechat.Work/Extensions/WechatWorkClientEventExtensions.cs b/src/SKIT.FlurlHttpClient.Wechat.Work/Extensions/WechatWorkClientEventExtensions.cs index f048e65b..348f4fc9 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.Work/Extensions/WechatWorkClientEventExtensions.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.Work/Extensions/WechatWorkClientEventExtensions.cs @@ -71,11 +71,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work throw new Exceptions.WechatWorkEventSerializationException("Encrypt event failed, because of empty encrypted data."); callbackXml = Utilities.WxBizMsgCryptor.AESDecrypt(cipherText: encryptedXml!, encodingAESKey: client.Credentials.PushEncodingAESKey!, out _); - - using var reader = new StringReader(callbackXml); - - XmlSerializer xmlSerializer = new XmlSerializer(typeof(TEvent), new XmlRootAttribute("xml")); - return (TEvent)xmlSerializer.Deserialize(reader)!; + return Utilities.XmlUtility.Deserialize(callbackXml); } catch (WechatWorkException) { diff --git a/src/SKIT.FlurlHttpClient.Wechat.Work/Utilities/Internal/XmlUtility.cs b/src/SKIT.FlurlHttpClient.Wechat.Work/Utilities/Internal/XmlUtility.cs index 99bdf5fc..1260d779 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.Work/Utilities/Internal/XmlUtility.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.Work/Utilities/Internal/XmlUtility.cs @@ -13,6 +13,21 @@ namespace SKIT.FlurlHttpClient.Wechat.Work.Utilities // REF: https://docs.microsoft.com/zh-cn/dotnet/api/system.xml.serialization.xmlserializer#dynamically-generated-assemblies private static Hashtable _serializers = new Hashtable(); + private static XmlSerializer GetTypedSerializer(Type type) + { + if (type == null) throw new ArgumentNullException(nameof(type)); + + string skey = type.AssemblyQualifiedName; + XmlSerializer? xmlSerializer = (XmlSerializer)_serializers[skey]; + if (xmlSerializer == null) + { + xmlSerializer = new XmlSerializer(type, new XmlRootAttribute("xml")); + _serializers[skey] = xmlSerializer; + } + + return xmlSerializer; + } + public static string Serialize(Type type, object obj) { string xml; @@ -24,19 +39,12 @@ namespace SKIT.FlurlHttpClient.Wechat.Work.Utilities settings.WriteEndDocumentOnClose = false; settings.NamespaceHandling = NamespaceHandling.OmitDuplicates; - string skey = type.AssemblyQualifiedName; - XmlSerializer? xmlSerializer = (XmlSerializer)_serializers[skey]; - if (xmlSerializer == null) - { - xmlSerializer = new XmlSerializer(type, new XmlRootAttribute("xml")); - _serializers[skey] = xmlSerializer; - } - using var stream = new MemoryStream(); using var writer = XmlWriter.Create(stream, settings); - XmlSerializerNamespaces xmlNamespace = new XmlSerializerNamespaces(); - xmlNamespace.Add(string.Empty, string.Empty); - xmlSerializer.Serialize(writer, obj, xmlNamespace); + XmlSerializer serializer = GetTypedSerializer(type); + XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); + ns.Add(string.Empty, string.Empty); + serializer.Serialize(writer, obj, ns); writer.Flush(); xml = Encoding.UTF8.GetString(stream.ToArray()); xml = Regex.Replace(xml, "\\s*<\\w+ (xsi|d2p1):nil=\"true\"[^>]*/>", string.Empty, RegexOptions.IgnoreCase); @@ -50,5 +58,18 @@ namespace SKIT.FlurlHttpClient.Wechat.Work.Utilities { return Serialize(typeof(T), obj); } + + public static object Deserialize(Type type, string xml) + { + using var reader = new StringReader(xml); + XmlSerializer serializer = GetTypedSerializer(type); + return serializer.Deserialize(reader); + } + + public static T Deserialize(string xml) + where T : class + { + return (T)Deserialize(typeof(T), xml); + } } }