Skip to content

Commit

Permalink
Reuse GxSerializationErrorManager instances associated to chached Xml…
Browse files Browse the repository at this point in the history
…Serializer instances. (#739)
  • Loading branch information
claudiamurialdo authored Dec 26, 2022
1 parent 8b3ba79 commit fe0d43d
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions dotnet/src/dotnetframework/GxClasses/Helpers/XMLHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ private GXXmlSerializer() { }
public static T Deserialize<T>(Type TargetType, string serialized, string sName, string sNameSpace, out List<string> serializationErrors)
{
T deserialized;
GxSerializationErrorManager serErr = null;
if (!string.IsNullOrEmpty(serialized))
{
XmlSerializer xmls;
GxSerializationErrorManager serErr = new GxSerializationErrorManager();
if (string.IsNullOrEmpty(sNameSpace) && string.IsNullOrEmpty(sName))
{
#pragma warning disable SCS0028 // Unsafe deserialization possible from {1} argument passed to '{0}'
Expand All @@ -75,17 +75,13 @@ public static T Deserialize<T>(Type TargetType, string serialized, string sName,
}
else
{
xmls = GetSerializer(TargetType, null, sName, sNameSpace);
xmls.UnknownNode += serErr.unknownNode;
xmls = GetSerializer(TargetType, null, sName, sNameSpace, out serErr);
}
try
{
using (StringReader sr = new StringReader(serialized))
{
using (XmlReader xmlReader = XmlReader.Create(sr, new XmlReaderSettings {CheckCharacters=false }))
{
deserialized = (T)(xmls.Deserialize(xmlReader));
}
deserialized = (T)(xmls.Deserialize(sr));
}
}
catch (InvalidOperationException ex)
Expand All @@ -103,40 +99,53 @@ public static T Deserialize<T>(Type TargetType, string serialized, string sName,
throw ex;
}
}
finally
if (serErr != null)
{
xmls.UnknownNode -= serErr.unknownNode;
serializationErrors = serErr.GetErrors();
serErr.ClearErrors();
}
else
{
serializationErrors = null;
}

serializationErrors = serErr.GetErrors();
return deserialized;
}
serializationErrors = new List<string>();
return default(T);
}
private static ConcurrentDictionary<string, XmlSerializer> serializers = new ConcurrentDictionary<string, XmlSerializer>();
private static ConcurrentDictionary<string, GxSerializationErrorManager> serializersErr = new ConcurrentDictionary<string, GxSerializationErrorManager>();

private static XmlSerializer GetSerializer(Type type, XmlAttributeOverrides ovAttrs, string rootName, string sNameSpace)
private static XmlSerializer GetSerializer(Type type, XmlAttributeOverrides ovAttrs, string rootName, string sNameSpace, out GxSerializationErrorManager serErr)
{
string key = string.Format("{0},{1},{2},{3}", type.FullName, rootName, sNameSpace, ovAttrs == null);
XmlRootAttribute root = new XmlRootAttribute(rootName);
XmlSerializer serializer;
if (serializers.TryGetValue(key, out serializer))
{
serializersErr.TryGetValue(key, out serErr);
return serializer;
}
else
{
return CreateSerializer(key, type, ovAttrs, root, sNameSpace);
return CreateSerializer(key, type, ovAttrs, root, sNameSpace, out serErr);
}
}

private static XmlSerializer CreateSerializer(string key, Type type, XmlAttributeOverrides ovAttrs, XmlRootAttribute root, string sNameSpace)
private static void XmlSerializer_UnknownNode(object sender, XmlNodeEventArgs e)
{
throw new NotImplementedException();
}

private static XmlSerializer CreateSerializer(string key, Type type, XmlAttributeOverrides ovAttrs, XmlRootAttribute root, string sNameSpace, out GxSerializationErrorManager serErr)
{
serErr = new GxSerializationErrorManager();
#pragma warning disable SCS0028 // Unsafe deserialization possible from {1} argument passed to '{0}'
var s = new XmlSerializer(type, ovAttrs, Array.Empty<Type>(), root, sNameSpace);
XmlSerializer s = new XmlSerializer(type, ovAttrs, Array.Empty<Type>(), root, sNameSpace);
#pragma warning restore SCS0028 // Unsafe deserialization possible from {1} argument passed to '{0}'
s.UnknownNode += serErr.unknownNode;
serializers.TryAdd(key, s);
serializersErr.TryAdd(key, serErr);
return s;
}

Expand All @@ -162,7 +171,7 @@ internal static string Serialize(string rootName, string sNameSpace, XmlAttribut
{
indentElements = xmlIndent.Trim().ToLower() == "true";
}
XmlSerializer xmls = GetSerializer(instance.GetType(), ovAttrs, rootName, sNameSpace);
XmlSerializer xmls = GetSerializer(instance.GetType(), ovAttrs, rootName, sNameSpace, out _);
string s;
using (MemoryStream stream = new MemoryStream())
{
Expand Down Expand Up @@ -229,5 +238,6 @@ public List<string> GetErrors()
{
return this.errors;
}
internal void ClearErrors() { this.errors = new List<string>();}
}
}

0 comments on commit fe0d43d

Please sign in to comment.