Title: Added support for 'Do' attribute in XML Formatter

x) Introduced the 'Do' attribute to the XML formatter.
x) The 'Do' attribute is now fetched and checked before filtering fields and properties.
x) This change allows more granular control over serialization, as items marked with the 'Do' attribute will not be filtered out.
x) Adjusted formatting in some areas for consistency.
This commit is contained in:
Marc Hernandez 2024-07-29 20:25:22 -07:00
parent b0b9496c4a
commit b66bc4e5ed

View File

@ -561,6 +561,7 @@ namespace lib
string name = childFi.Name; string name = childFi.Name;
var dontAtt = childFi.GetCustomAttributes<lib.Dont>(); var dontAtt = childFi.GetCustomAttributes<lib.Dont>();
var doAtt = childFi.GetCustomAttributes<lib.Do>();
string propName = ""; string propName = "";
@ -573,6 +574,8 @@ namespace lib
var propInfo = narrowType.GetProperty( propName ); var propInfo = narrowType.GetProperty( propName );
dontAtt = propInfo.GetCustomAttributes<lib.Dont>(); dontAtt = propInfo.GetCustomAttributes<lib.Dont>();
doAtt = propInfo.GetCustomAttributes<lib.Do>();
} }
if( dontAtt.Any() ) if( dontAtt.Any() )
@ -586,7 +589,7 @@ namespace lib
name = refl.TypeToIdentifier( name ); name = refl.TypeToIdentifier( name );
// @@@ TODO This doesnt yet handle propNames! // @@@ TODO This doesnt yet handle propNames!
if( FilterField( filterFields, doImpls, whitelistFields, childFi as MemberInfo, name ) ) if( !doAtt.Any() && FilterField( filterFields, doImpls, whitelistFields, childFi as MemberInfo, name ) )
continue; continue;
var useFieldName = true; var useFieldName = true;
@ -641,9 +644,12 @@ namespace lib
continue; continue;
} }
var doAtt = childPi.GetCustomAttributes<lib.Do>();
name = refl.TypeToIdentifier( name ); name = refl.TypeToIdentifier( name );
if( FilterField( filterProps, doImpls, whitelistProps, childPi as PropertyInfo, name ) ) if( !doAtt.Any() && FilterField( filterProps, doImpls, whitelistProps, childPi as PropertyInfo, name ) )
continue; continue;
XmlElement childElem = getNamedChild( allChildren, name ); XmlElement childElem = getNamedChild( allChildren, name );
@ -1338,6 +1344,7 @@ namespace lib
{ {
string name = childFi.Name; string name = childFi.Name;
var dontAtt = childFi.GetCustomAttributes<lib.Dont>(); var dontAtt = childFi.GetCustomAttributes<lib.Dont>();
var doAtt = childFi.GetCustomAttributes<lib.Do>();
string propName = ""; string propName = "";
if( name.StartsWith( "<" ) && name.EndsWith( "BackingField" ) ) if( name.StartsWith( "<" ) && name.EndsWith( "BackingField" ) )
@ -1349,6 +1356,7 @@ namespace lib
var propInfo = narrowType.GetProperty( propName ); var propInfo = narrowType.GetProperty( propName );
dontAtt = propInfo.GetCustomAttributes<lib.Dont>(); dontAtt = propInfo.GetCustomAttributes<lib.Dont>();
doAtt = propInfo.GetCustomAttributes<lib.Do>();
} }
@ -1363,7 +1371,7 @@ namespace lib
if( name == "Fn" ) continue; if( name == "Fn" ) continue;
} }
if( FilterField( filterFields, doImpls, whitelistFields, childFi as MemberInfo, name ) ) if( !doAtt.Any() && FilterField( filterFields, doImpls, whitelistFields, childFi as MemberInfo, name ) )
continue; continue;
object[] objs = childFi.GetCustomAttributes( typeof( NonSerializedAttribute ), true ); object[] objs = childFi.GetCustomAttributes( typeof( NonSerializedAttribute ), true );
@ -1429,15 +1437,15 @@ namespace lib
var custWLProps = mi?.GetCustomAttribute<ChildPropsAttribute>( true ); var custWLProps = mi?.GetCustomAttribute<ChildPropsAttribute>( true );
filterFields = custWLFields != null; filterFields = custWLFields != null;
filterProps = custWLProps != null; filterProps = custWLProps != null;
var typesTodo = type.GetCustomAttribute<Ser>( true )?.Types ?? TypesDefault; var typesTodo = type.GetCustomAttribute<Ser>( true )?.Types ?? TypesDefault;
doImpls = typesTodo.HasFlag( Types.Implied ); doImpls = typesTodo.HasFlag( Types.Implied );
doFields = filterFields || typesTodo.HasFlag( Types.Fields ); doFields = filterFields || typesTodo.HasFlag( Types.Fields );
doProps = filterProps || typesTodo.HasFlag( Types.Props ); doProps = filterProps || typesTodo.HasFlag( Types.Props );
whitelistFields = new( custWLFields?.Values ?? new string[0] ); whitelistFields = new( custWLFields?.Values ?? new string[0] );
whitelistProps = new( custWLProps?.Values ?? new string[0] ); whitelistProps = new( custWLProps?.Values ?? new string[0] );
} }
private void SerializeArray( XmlWriter writer, MemberInfo mi, Type mType, object root, int depth ) private void SerializeArray( XmlWriter writer, MemberInfo mi, Type mType, object root, int depth )