{A}目录表与sharpSerializer的Hello World不包括从序列化的属性自定义序列化的属性列表AssemblyQualifiedName或作为一个短期的类型名称quot;的序列化类型的TypeName,AssemblyNamequot;DateTime和浮点值的自定义格式数据序列化到其他格式为XML{A6}{A7}序列化一个多态的财产(该财产的价值是从属性的类型继承)值从接口继承序列化泛型Dictionary序列化的多维数组序列不同的对象类型的数组和数组的数组(嵌套数组)
序列化其他类型{A8}{A9}{A10}{A11}{A12}{A13}
sharpSerializer
是一个开源的。NET Framework对象序列化器。NET精简框架和Silverlight。其目的很简单 - 快速的对象序列化,没有安全的考虑,从A到B。在这篇文章中,我会尽力说服你,sharpSerializer可以比很多内置的XmlSerializer和简单。{A14}
内置在XmlSerializer的对象序列化有一些限制:它不能序列化的多维数组。它不能序列化一个通用的Dictionarylt; TKEY,TValuegt。它不能序列化开箱多态性属性(从物业类型继承的值类型)。它需要在其序列(...)方法的序列化对象的类型。它需要很多的属性来定义你的业务对象,例如:XmlArrayAttribute,XmlArrayItemAttribute来定义的继承类型的数组。
sharpSerializer克服上述限制,而无需额外的属性,以纪念对象,并没有被序列化的类型序列化对象。{A15}与sharpSerializer的Hello World
假设我们有一个对象:public class SomeObject
{
public int SimpleInt { get; set; }
public DateTime SimpleDateTime { get; set; }
public TimeSpan SimpleTimeSpan { get; set; }
public SimpleEnum SimpleEnum { get; set; }
public string SimpleString { get; set; }
}
public enum SimpleEnum {One,Two,Three}
我们初始化和序列化这个对象:{C}
注意!您不需要标记您的任何属性的对象,也不必给Serialize方法的对象的类型。
这个对象序列化到下面的XML:<Complex name="Root" type="SharpSerializerTestApp.SomeObject, SharpSerializerTestApp">
<Properties>
<Simple name="SimpleInt" value="42" />
<Simple name="SimpleDateTime" value="04/28/2010 00:00:00" />
<Simple name="SimpleTimeSpan" value="01:02:03" />
<Simple name="SimpleEnum" value="Three" />
<Simple name="SimpleString" value="nothing" />
</Properties>
</Complex>
正如你可以看到,对象的类型是序列为quot;,AssemblyNamequot; TYPENAME和DateTime序列化CultureInfo.InvariantCulture。后来,我会告诉你如何自定义类型的名称(即,作为AssemblyQualifiedName)和如何保存你想要的格式DateTime和浮点数。不包括从序列化的属性
默认情况下,所有属性被序列化,这是公开的,例如,而不是只读。也序列化阵列(Type.IsArray == TRUE),或从IEnumerable,ICollection的或IDictionary的继承所有属性。出于性能方面的原因,不会被序列化领域。要排除在您的自定义类型的属性,你需要标记与属性。 ExcludeFromSerializationAttribute支持开箱。
如果你的对象是常见的。NET属性,如XmlIgnore标记,你可以将这些属性添加到上市AttributesToIgnore。public class MyClass
{
[ExcludeFromSerialization]
public int SimpleInt { get; set; }
}
// remove default ExcludeFromSerializationAttribute for performance gain
serializer.PropertyProvider.AttributesToIgnore.Clear();
serializer.PropertyProvider.AttributesToIgnore.Add(typeof(XmlIgnore));
或使用设置类:
要排除内置。NET类型的属性,或者如果不能延长带属性的属性,你的类型和它的属性名添加到列表SharpSerializer.PropertyProvider.PropertiesToIgnore。// for binary mode
var settings = new SharpSerializerBinarySettings();
// for xml mode
var settings = new SharpSerializerXmlSettings();
// remove default ExcludeFromSerializationAttribute for performance gain
settings.AdvancedSettings.AttributesToIgnore.Clear();
settings.AdvancedSettings.AttributesToIgnore.Add(typeof(XmlIgnore));
即System.Collections.Generic.Listlt; TGT;财产的能力,这是不相关的序列化,因此它应该被忽略。serializer.PropertyProvider.PropertiesToIgnore.Add
(typeof(List<string>), "Capacity");
PropertiesToIgnore也可以从sharpSerializer设置访问。
自定义序列化属性列表// create the settings
var settings = new SharpSerializerBinarySettings(); // for binary mode
var settings = new SharpSerializerXmlSettings(); // for xml mode
settings.AdvancedSettings.PropertiesToIgnore.Add(typeof(List), "Capacity");
如果没有足够的过滤与属性ExcludeFromSerializationAttribute,AttributesToIgnore或PropertiesToIgnore,你可以建立CustomPropertyProvider。作为一个基类,有PropertyProvider,其中有两个虚方法GetAllProperties()和IgnoreProperty (...).它们可以覆盖到自定义的逻辑。
AssemblyQualifiedName或作为一个短期的类型名称quot;的序列化类型的TypeName,AssemblyNamequot;serializer.PropertyProvider = new MyCustomPropertyProvider();
之前SharpSerializer v.2.12,所有类型的序列化类型的名称短quot;的TypeName,AssemblyNamequot;即:type="System.String, mscorlib"
这是简单的阅读,输出的规模还很小。但是,反序列化过程中遇到的问题,如果签名的程序集或特定版本的工作。
自SharpSerializer v.2.12 AssemblyQualifiedName序列化的所有类型,即:type="System.String, mscorlib, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089"
你可以改变改变sharpSerializer设置和设置的属性IncludeAssemblyVersionInTypeName,IncludeCultureInTypeName和IncludePublicKeyTokenInTypeName的类型命名。由于默认情况下,这些属性arenbsp;设置为true。
DateTime和浮点值的自定义格式var settings = new SharpSerializerXmlSettings();
settings.IncludeAssemblyVersionInTypeName = false;
settings.IncludeCultureInTypeName = false;
settings.IncludePublicKeyTokenInTypeName = false;
var serializer = new SharpSerializer(settings);
默认情况下,所有的原始类型和日期转换为字符串根据CultureInfo.InvariantCulture。如果需要自定义格式或一种文化,这可以实现设置类的以下修改:
序列化数据到其他格式为XMLvar settings = new SharpSerializerXmlSettings();
settings.Culture = System.Globalization.CultureInfo.CurrentCulture;
var serializer = new SharpSerializer(settings);
其实,sharpSerializer可以序列化到XML和自己的二进制格式。然而,注射后定制IXmlWriter或IBinaryWriter,它可以像JSON或其它加密,压缩,优化等二进制流其他文本格式序列化数据。请参考{A16}有关二进制序列化的详细信息。{A17}
这三个步骤:第1步
PropertyFactory转换属性的对象。抽象类属性包含PropertyCollection。 PropertyCollection代表对象的结构和它的数据。下面的类继承属性类:SimpleProperty用于描述所有的原始类型和字符串的DateTime,TimeSpan的所有枚举ComplexProperty为其他类和结构,这是不上市引用一个已经序列化的类ComplexReferenceProperty(节省空间。)SingleDimensionalArrayProperty为单维数组
MultiDimensionalArrayProperty为多维数组从ICollection继承,但不是从IDictionary的CollectionProperty为上市
从IDictionary的继承对上市DictionaryPropertyNullProperty所有对象/空字符串(有需要作出通知,他们都为空)第2步
属性是序列XmlPropertySerializer。其中,它是序列化的格式取决于所用的作家。第3步
XML序列化,DefaultXmlWriter负责。但作为一个接收器,可用于任何作家,实现IXmlWriter。这样,你可以写自己的作家,其中数据到其他格式如JSON序列化。
在反序列化过程中,这个过程是反向的。 IXmlReader的一个实例读取数据XmlPropertyDeserializer,反序列化属性的数据,并将其转发。最后的ObjectFactory转换成对象的属性。
请下载{A18}更多细节。{A19}
下面的例子是部分的HelloWorldDemo应用程序,你可以用{A18}下载。序列化一个多态的属性(属性类型属性的值是从继承)
是一个类型IComplexObject的属性。属性值包含一个类ComplexObject,它是从IComplexObject继承。public IComplexObject ComplexObject { get; set; }
有没有需要被序列化的类型的输入。 sharpSerializer可以序列化的预期的类型和任何继承类型的开箱:
从接口继承值序列化泛型Dictionary<Complex name="ComplexObject"
type="HalloWorldApp.BusinessObjects.ComplexObject, HalloWorldApp">
<Properties>
<Simple name="SimpleInt" value="33" />
</Properties>
</Complex>
值从接口IComplexObject(多态参数)继承了一些复杂的类,这是一个通用的字典:public IDictionary<int, IComplexObject> GenericDictionary { get; set; }
序列化:<Dictionary name="GenericDictionaryOfPolymorphicValues"
type="System.Collections.Generic.Dictionary`2[[System.Int32, mscorlib],
[HalloWorldApp.BusinessObjects.IComplexObject, HalloWorldApp]],
mscorlib" keyType="System.Int32, mscorlib"
valueType="HalloWorldApp.BusinessObjects.IComplexObject, HalloWorldApp">
<Items>
<Item>
<Simple value="2012" />
<Complex type="HalloWorldApp.BusinessObjects.ComplexObject, HalloWorldApp">
<Properties>
<Simple name="SimpleInt" value="2012000" />
</Properties>
</Complex>
</Item>
</Items>
</Dictionary>
正如你可以看到,键和值的类型并不重要。 sharpSerializer可以序列化原始类型,复杂的对象,甚至嵌套列表,例如,字典。序列化一个多维数组
有一个二维数组(它可能有更多的维度):public string[,] DoubleArray { get; set; }
序列化:<MultiArray name="DoubleArray" elementType="System.String, mscorlib">
<Dimensions>
<Dimension length="3" />
<Dimension length="2" />
</Dimensions>
<Items>
<Item indexes="0,0">
<Simple value="k1" />
</Item>
<Item indexes="0,1">
<Simple value="k2" />
</Item>
<Item indexes="1,0">
<Simple value="b1" />
</Item>
<Item indexes="1,1">
<Simple value="b2" />
</Item>
<Item indexes="2,0">
<Simple value="z1" />
</Item>
<Item indexes="2,1">
<Simple value="z2" />
</Item>
</Items>
</MultiArray>
不要紧数组是什么类型的。 sharpSerializer可以序列化任何对象的数组,数组的数组,数组的集合,数组或字典。它可以序列化真正深的嵌套数组。序列不同的对象类型的数组和数组的数组(嵌套数组)
有以下对象的数组:root.SingleArrayOfObjects = new object[]
{
42,
"nothing to say",
false,
BusinessObjects.SimpleEnum.Three,
null,
new object[]
{
42,
"nothing to say",
false,
BusinessObjects.SimpleEnum.Three,
null
}
};
序列化:
序列化其他类型<SingleArray name="SingleArrayOfObjects" elementType="System.Object, mscorlib">
<Items>
<Simple type="System.Int32, mscorlib" value="42" />
<Simple type="System.String, mscorlib" value="nothing to say" />
<Simple type="System.Boolean, mscorlib" value="False" />
<Simple type="HalloWorldApp.BusinessObjects.SimpleEnum,
HalloWorldApp" value="Three" />
<Null />
<SingleArray type="System.Object[], mscorlib"
elementType="System.Object, mscorlib">
<Items>
<Simple type="System.Int32, mscorlib" value="42" />
<Simple type="System.String, mscorlib" value="nothing to say" />
<Simple type="System.Boolean, mscorlib" value="False" />
<Simple type="HalloWorldApp.BusinessObjects.SimpleEnum, HalloWorldApp"
value="Three" />
<Null />
</Items>
</SingleArray>
</Items>
</SingleArray>
请下载{A18}看到整个的例子,还有什么可以被序列化。下面是从HelloWorldDemo其他一些属性:public class RootContainer
{
/// <summary>
/// Structures are handled as objects during serialization
/// They are serialized as ComplexProperty
/// </summary>
public AdvancedStruct AdvancedStruct { get; set; }
/// <summary>
/// Single dimensional array of simple type.
/// It is serialized as SingleDimensionalArrayProperty
/// </summary>
public string[] SingleArray { get; set; }
/// <summary>
/// Multidimensional array of simple type.
/// Is is serialized as MultiDimensionalArrayProperty
/// </summary>
public string[,] DoubleArray { get; set; }
/// <summary>
/// Single array of derived objects.
/// This is polymorphic collection - Items derive from the interface
/// </summary>
public IComplexObject[] PolymorphicSingleArray { get; set; }
/// <summary>
/// Generic list is serialized as a collection.
/// It is serialized as CollectionProperty
/// </summary>
public IList<string> GenericList { get; set; }
/// <summary>
/// Polymorphic property. Object instance derives from the property type
/// Is serialized as ComplexProperty
/// </summary>
public IComplexObject ComplexObject { get; set; }
/// <summary>
/// Collection where item values are
/// derived from the collection item type
/// </summary>
public ComplexObjectPolymorphicCollection ComplexObjectCollection
{ get; set; }
/// <summary>
/// Dictionary where values are derived
/// from the predefined dictionary value type
/// </summary>
public ComplexObjectPolymorphicDictionary ComplexObjectDictionary
{ get; set; }
/// <summary>
/// List items are derived from the generic attribute.
/// This is polymorphic attribute.
/// </summary>
public IList<IComplexObject> GenericListOfComplexObjects { get; set; }
/// <summary>
/// Generic object with polymorphic attribute.
/// It is serialized as ComplexProperty
/// </summary>
public GenericObject<IComplexObject> GenericObjectOfComplexObject
{ get; set; }
/// <summary>
/// Multidimensional array of generic object with polymorphic attribute
/// </summary>
public GenericObject<IComplexObject>[,]
MultiArrayOfGenericObjectWithPolymorphicArgument
{ get; set; }
}
public interface IComplexObject { int SimpleInt { get; set; } }
public class ComplexObject : IComplexObject {public int SimpleInt
{ get; set; }}
public class ComplexObjectPolymorphicCollection :
Collection<IComplexObject>{}
public class ComplexObjectCollection :
Collection<ComplexObject>{}
public class ComplexObjectPolymorphicDictionary :
Dictionary<int, IComplexObject>{}
public class ComplexObjectDictionary :
Dictionary<int, ComplexObject>{}
你怎么想? sharpSerializer简单的XML序列化就够了吗?{A22}
在当前版本的sharpSerializer,也有一些限制有关对象的序列化和反序列化。在未来,这些限制可以被瓦解,但其实他们并没有这样的痛苦。它们是:没有他们的公共标准构造的对象不能被反序列化。多次引用到同一个复杂的对象是没有经过优化。这种多重引用对象序列化为它的许多引用多次。 (SharpSerializer v.2.9及以上可以优化序列的同一对象的多个引用,只有一次这样的对象序列化。较小的文件大小的优化结果。)
NET精简框架和Silverlight的限制后的序列化。在一个数组LowerBound将始终为0,不管它是如何序列化反序列化。
数组LowerBound是没有的。NET Framework精简或Silverlight的一部分,因此不能由sharpSerializer处理。{A23}
我开发sharpSerializer的主要原因是保存在一个XML文件的应用程序配置。几年前,我是一个多态配置与挣扎。我需要一个轻量级的配置存储支持对象的继承。该文件应该易于阅读和手动编辑。 System.Configuration有一个大的开销,是太僵硬。
第二个原因很简单 - Silverlight的本地数据存储库的贫困支持 - 它应该有一个更好的之一:-)。
尤其有趣的是其序列化数据在WP7的二进制格式(Windows Phone 7的)的可能性。{A24}
项目页上的最近期的来源和新闻:{A25}。{A26}
,如果你喜欢sharpSerializer或本文 - 请给它打分。如果没有,请以下;-)评论{A27}2011年11月9日:根据sharpSerializer v.2.16变化2011年10月24日:更新的下载文件2011-07-31:更新的下载文件SharpSerializer v.2.122011年7月28日:更新的下载文件sharpSerializer v.2.112011年5月8日:根据sharpSerializer v.2.9的变化,GUID序列化,AttributesToIgnore和优化序列化多个引用同一个对象2010-10-07:更新的下载文件2010-10-03:根据sharpSerializer 2.0版的变化2010-05-05:根据sharpSerializer V.1.2变化2010-05-04:增加了。NET Compact Framework的支持2010-04-30:格式更改(切断一些大的XML示例部分)2010-04-29:首先释放