TL、DR:我们一直在对 SQL DB 的一些表中的一些数据进行序列化。不幸的是,序列化技术为标记字符浪费了大量空间。我们找到了一种新的、更有效的方法:为了保持向后兼容性,采用以下逻辑是否安全?-> 当序列化发生时,它总是使用新的、更有效的方式发生。当反序列化发生时,我们检查字符串是使用新的序列化格式还是旧的 -> 然后我们使用适当的方法进行反序列化。这是健壮的吗?这可以在生产中使用吗?这种方法没有任何微妙的问题吗?
问候。我正在开发一个与 SQL 数据库交互的应用程序。为了实现特定的业务需求,我们一直在我们的数据库表的特殊列中序列化一些数据,类型为ntext。基本上,在该列的每个单元格中,我们序列化一个“属性”对象数组,因此typeof(T) 是 Attributo[]:
“属性”定义如下:
public class Attributo
{
public virtual String Nome { get; set; }
public virtual String Valore { get; set; }
public virtual String Tipologia { get; set; }
}
- 反序列化以读取实际值:
XMLUtilities.Deserialize<Attributo[]>(value));
序列化以存储列中的值(对于每一行..):
XMLUtilities.Serialize(attributes.ToArray());
这是使用 XmlSerializer 对象的辅助类:
public static class XMLUtilities
{
public static T Deserialize<T>(String xmlString)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
using (TextReader reader = new StringReader(xmlString))
{
return (T) serializer.Deserialize(reader);
}
}
public static String Serialize<T>(T xmlObject)
{
MemoryStream stream = new MemoryStream();
XmlSerializer serializer = new XmlSerializer(typeof(T));
XmlSerializerNamespaces xmlnsEmpty = new XmlSerializerNamespaces();
xmlnsEmpty.Add("", "");
serializer.Serialize(stream, xmlObject, xmlnsEmpty);
stream.Seek(0, SeekOrigin.Begin);
using (StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
现在,这种技术的问题在于它为标记字符浪费了大量空间。这是存储在数据库中的示例字符串:
<?xml version="1.0"?>
<ArrayOfAttributo>
<Attributo>
<Nome>Leakage_test_Time_prg1_p1</Nome>
<Valore>4</Valore>
<Tipologia>Single</Tipologia>
</Attributo>
<Attributo>
<Nome>Leakage_air_Volume_p1</Nome>
<Valore>14</Valore>
<Tipologia>Single</Tipologia>
</Attributo>
</ArrayOfAttributo>
因此,我们找到了一种更简洁的方法来序列化这些 Attributo[],它会产生这种输出:
<ArrayOfA xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<A>
<N>Leakage_test_Time_prg1_p1</N>
<V>4</V>
<T>Single</T>
</A>
<A>
<N>Leakage_air_Volume_p1</N>
<V>14</V>
<T>Single</T>
</A>
</ArrayOfA>
然后,为了保持向后兼容性,这是我们实现以下逻辑的核心问题:
- 在序列化期间:
我们总是以新的、更简洁的方式进行连载
- 在反序列化期间:
我们检查字符串是否以:
<?xml version="1.0"?>
或不。如果是这样,这是一个旧条目,所以我们以旧方式反序列化它。否则,我们使用新格式反序列化。
我们通过以这种方式装饰“属性”来实现这一点:
[DataContract(Name = "A", Namespace= "")]
public class Attributo
{
[DataMember(Name = "N")]
public virtual String Nome { get; set; }
[DataMember(Name = "V")]
public virtual String Valore { get; set; }
[DataMember(Name = "T")]
public virtual String Tipologia { get; set; }
}
并通过对我们的序列化/反序列化方法执行以下更改,现在,对于新的序列化技术,这些更改依赖于 DataContractSerializer 对象:
public static T Deserialize<T>(String xmlString)
{
//let's see if it's an old-style entry...
if (xmlString.StartsWith("<?xml version=\"1.0\"?>\r\n<ArrayOfAttributo>"))
{
try
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
using (TextReader reader = new StringReader(xmlString))
{
return (T)serializer.Deserialize(reader);
}
}
catch { }
}
//..then it must be a new-style one
DataContractSerializer ser = new DataContractSerializer(typeof(T));
using (Stream s = _streamFromString(xmlString))
{
return (T) ser.ReadObject(s);
}
}
public static String Serialize<T>(T xmlObject)
{
MemoryStream stream1 = new MemoryStream();
DataContractSerializer ser = new DataContractSerializer(typeof(T));
ser.WriteObject(stream1, xmlObject);
stream1.Position = 0;
StreamReader sr = new StreamReader(stream1);
string xmlString = sr.ReadToEnd();
return xmlString;
}
private static Stream _streamFromString(string s)
{
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
一切似乎都在使用这种方法,但我们希望在继续进行之前评估所有可能的风险。这在生产中使用安全吗?