Skip to content

Commit 7b4680c

Browse files
taylorjonlJonathan Taylorpolenter
authored
Added support to ignore properties based on declared type (#24)
Added support to ignore properties based on declared type Co-authored-by: Jonathan Taylor <jltaylor@ebay.com> Co-authored-by: Pawel Idzikowski <29297999+polenter@users.noreply.github.com>
1 parent d94b23e commit 7b4680c

File tree

5 files changed

+88
-6
lines changed

5 files changed

+88
-6
lines changed

HelloWorldApp/Form1.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ private SharpSerializerXmlSettings createXmlSettings()
157157
settings.IncludeCultureInTypeName = true;
158158
settings.IncludePublicKeyTokenInTypeName = true;
159159

160-
160+
161161

162162
// ADVANCED SETTINGS
163163
// Most of the classes needed to alter these settings are in the namespace Polenter.Serialization.Advanced
@@ -173,7 +173,13 @@ private SharpSerializerXmlSettings createXmlSettings()
173173
// serializer.PropertyProvider.PropertiesToIgnore.Add(typeof(List<string>), "Capacity")
174174
settings.AdvancedSettings.PropertiesToIgnore.Add(typeof (List<string>), "Capacity");
175175

176-
176+
177+
// PropertyTypesToIgnore
178+
// Sometimes you want to ignore some types during the serialization.
179+
// To ignore a type add these types to the list PropertyTypesToIgnore.
180+
settings.AdvancedSettings.PropertyTypesToIgnore.Add(typeof(List<string>));
181+
182+
177183
// RootName
178184
// There is always a root element during serialization. Default name of this element is "Root",
179185
// but you can change it to any other text.
@@ -271,6 +277,12 @@ private SharpSerializerBinarySettings createBinarySettings()
271277
settings.AdvancedSettings.PropertiesToIgnore.Add(typeof(List<string>), "Capacity");
272278

273279

280+
// PropertyTypesToIgnore
281+
// Sometimes you want to ignore some types during the serialization.
282+
// To ignore a type add these types to the list PropertyTypesToIgnore.
283+
settings.AdvancedSettings.PropertyTypesToIgnore.Add(typeof(List<string>));
284+
285+
274286
// RootName
275287
// There is always a root element during the serialization. Default name of this element is "Root",
276288
// but you can change it to any other text.
@@ -354,19 +366,20 @@ private SharpSerializer createSerializerWithCustomReaderAndWriter()
354366

355367
var sharpSerializer = new SharpSerializer(serializer, deserializer);
356368

357-
369+
358370
// there is one more option you can alter directly on your instance of SharpSerializer
359371

360372
// *************************************************************************************
361373
// PropertyProvider
362-
// If the advanced setting PropertiesToIgnore are not enough there is possibility to create your own PropertyProvider
374+
// If the advanced setting PropertiesToIgnore or PropertyTypesToIgnore are not enough there is possibility to create your own PropertyProvider
363375
// As a standard there are only properties serialized which:
364376
// - are public
365377
// - are not static
366378
// - does not contain ExcludeFromSerializationAttribute
367379
// - have their set and get accessors
368380
// - are not indexers
369381
// - are not in PropertyProvider.PropertiesToIgnore
382+
// - are not in PropertyProvider.PropertyTypesToIgnore
370383
// You can replace this functionality with an inheritor class of PropertyProvider
371384

372385
sharpSerializer.PropertyProvider = new MyVerySophisticatedPropertyProvider();

SharpSerializer.Tests/SerializationTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ public class Class2BeSerialized
4242

4343
[MyExcludeAttribute]
4444
public virtual Class2BeSerialized ComplexPrivateAttribute { get; set; }
45+
46+
public Class2BeIgnored IgnoredType { get; set; }
47+
}
48+
49+
/// <summary>
50+
/// Local testclass to be ignored
51+
/// </summary>
52+
public class Class2BeIgnored
53+
{
54+
public string Name { get; set; }
4555
}
4656

4757
[TestMethod]
@@ -62,6 +72,10 @@ public void XmlSerial_IgnoredAttributesShouldNotBeSerialized()
6272
ComplexSystemAttribute = child,
6373
ComplexPrivateAttribute = child,
6474
ComplexRule = child,
75+
IgnoredType = new Class2BeIgnored
76+
{
77+
Name = "Ignored"
78+
}
6579
};
6680

6781
/*
@@ -91,6 +105,8 @@ public void XmlSerial_IgnoredAttributesShouldNotBeSerialized()
91105
Assert.AreEqual(0, doc.SelectNodes("//Complex[@name='ComplexRule']").Count, "ComplexRule");
92106
Assert.AreEqual(0, doc.SelectNodes("//Complex[@name='ComplexSystemAttribute']").Count, "ComplexSystemAttribute");
93107
Assert.AreEqual(0, doc.SelectNodes("//Complex[@name='ComplexPrivateAttribute']").Count, "ComplexPrivateAttribute");
108+
109+
Assert.AreEqual(0, doc.SelectNodes("//Complex[@name='IgnoredType']").Count, "IgnoredType");
94110
}
95111

96112
private static XmlDocument Save(object data)
@@ -101,6 +117,8 @@ private static XmlDocument Save(object data)
101117
settings.AdvancedSettings.PropertiesToIgnore.Add(typeof(Class2BeSerialized), "NameRule");
102118
settings.AdvancedSettings.PropertiesToIgnore.Add(typeof(Class2BeSerialized), "ComplexRule");
103119

120+
settings.AdvancedSettings.PropertyTypesToIgnore.Add(typeof(Class2BeIgnored));
121+
104122
settings.AdvancedSettings.AttributesToIgnore.Add(typeof(MyExcludeAttribute));
105123
// this does not work
106124
//settings.AdvancedSettings.PropertiesToIgnore.Add(null, "NameRule");

SharpSerializer/Advanced/PropertyProvider.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#region Copyright © 2010 Pawel Idzikowski [idzikowski@sharpserializer.com]
1+
#region Copyright © 2010 Pawel Idzikowski [idzikowski@sharpserializer.com]
22

33
// ***********************************************************************
44
// Project: sharpSerializer
@@ -43,11 +43,13 @@ namespace Polenter.Serialization.Advanced
4343
///
4444
/// Its methods GetAllProperties and IgnoreProperty can be
4545
/// overwritten in an inherited class to customize its functionality.
46-
/// Its property PropertiesToIgnore contains properties, which are ignored during the serialization.
46+
/// Its property PropertiesToIgnore contains properties and its property PropertyTypesToIgnore
47+
/// container types, which are ignored during the serialization.
4748
/// </summary>
4849
public class PropertyProvider
4950
{
5051
private PropertiesToIgnore _propertiesToIgnore;
52+
private IList<Type> _propertyTypesToIgnore;
5153
private IList<Type> _attributesToIgnore;
5254

5355
[ThreadStatic]
@@ -75,6 +77,23 @@ public PropertiesToIgnore PropertiesToIgnore
7577
set { _propertiesToIgnore = value; }
7678
}
7779

80+
/// <summary>
81+
/// Which types should be ignored
82+
/// </summary>
83+
/// <remarks>
84+
/// Sometimes you want to ignore some types during the serialization.
85+
// To ignore a type add these types to the list PropertyTypesToIgnore.
86+
/// </remarks>
87+
public IList<Type> PropertyTypesToIgnore
88+
{
89+
get
90+
{
91+
if (_propertyTypesToIgnore == null) _propertyTypesToIgnore = new List<Type>();
92+
return _propertyTypesToIgnore;
93+
}
94+
set { _propertyTypesToIgnore = value; }
95+
}
96+
7897
/// <summary>
7998
/// All Properties markt with one of the contained attribute-types will be ignored on save.
8099
/// </summary>
@@ -133,14 +152,24 @@ public IList<PropertyInfo> GetProperties(Serialization.Serializing.TypeInfo type
133152
/// <returns>
134153
/// true if the property:
135154
/// - is in the PropertiesToIgnore,
155+
/// - is in the PropertyTypesToIgnore,
136156
/// - contains ExcludeFromSerializationAttribute,
137157
/// - does not have it's set or get accessor
138158
/// - is indexer
139159
/// </returns>
140160
protected virtual bool IgnoreProperty(Serialization.Serializing.TypeInfo info, PropertyInfo property)
141161
{
142162
// Soll die Eigenschaft ignoriert werden
163+
if (PropertyTypesToIgnore.Contains(property.PropertyType))
164+
{
165+
return true;
166+
}
167+
143168
if (PropertiesToIgnore.Contains(info.Type, property.Name))
169+
{
170+
return true;
171+
}
172+
144173
{
145174
return true;
146175
}

SharpSerializer/Core/SharpSerializerSettings.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ public sealed class AdvancedSharpSerializerBinarySettings : AdvancedSharpSeriali
111111
public class AdvancedSharpSerializerSettings
112112
{
113113
private PropertiesToIgnore _propertiesToIgnore;
114+
private IList<Type> _propertyTypesToIgnore;
114115
private IList<Type> _attributesToIgnore;
115116

116117
///<summary>
@@ -140,6 +141,25 @@ public PropertiesToIgnore PropertiesToIgnore
140141
set { _propertiesToIgnore = value; }
141142
}
142143

144+
/// <summary>
145+
/// Which properties should be ignored during the serialization.
146+
/// </summary>
147+
/// <remarks>
148+
/// In your business objects you can mark these properties with ExcludeFromSerializationAttribute
149+
/// In built in .NET Framework classes you can not do this. Therefore you define these properties here.
150+
/// I.e. System.Collections.Generic.List has property Capacity which is irrelevant for
151+
/// the whole Serialization and should be ignored.
152+
/// </remarks>
153+
public IList<Type> PropertyTypesToIgnore
154+
{
155+
get
156+
{
157+
if (_propertyTypesToIgnore == null) _propertyTypesToIgnore = new List<Type>();
158+
return _propertyTypesToIgnore;
159+
}
160+
set { _propertyTypesToIgnore = value; }
161+
}
162+
143163
/// <summary>
144164
/// All Properties marked with one of the contained attribute-types will be ignored on save.
145165
/// As default, this list contains only ExcludeFromSerializationAttribute.

SharpSerializer/SharpSerializer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ private void initialize(SharpSerializerXmlSettings settings)
148148

149149
// PropertiesToIgnore
150150
PropertyProvider.PropertiesToIgnore = settings.AdvancedSettings.PropertiesToIgnore;
151+
PropertyProvider.PropertyTypesToIgnore = settings.AdvancedSettings.PropertyTypesToIgnore;
151152
PropertyProvider.AttributesToIgnore = settings.AdvancedSettings.AttributesToIgnore;
152153
//RootName
153154
RootName = settings.AdvancedSettings.RootName;
@@ -179,6 +180,7 @@ private void initialize(SharpSerializerBinarySettings settings)
179180

180181
// PropertiesToIgnore
181182
PropertyProvider.PropertiesToIgnore = settings.AdvancedSettings.PropertiesToIgnore;
183+
PropertyProvider.PropertyTypesToIgnore = settings.AdvancedSettings.PropertyTypesToIgnore;
182184
PropertyProvider.AttributesToIgnore = settings.AdvancedSettings.AttributesToIgnore;
183185

184186
//RootName

0 commit comments

Comments
 (0)