Skip to content

Commit

Permalink
Add option to ignore non-public members when de/serializing (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sprixitite authored Jul 29, 2024
1 parent 132745b commit 9fd4001
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
9 changes: 7 additions & 2 deletions Tomlet/TomlCompositeDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,17 @@ public static TomlSerializationMethods.Deserialize<object> For(Type type, TomlSe
else
{
//Get all instance fields
var fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
var memberFlags = BindingFlags.Public | BindingFlags.Instance;
if (!options.IgnoreNonPublicMembers) {
memberFlags |= BindingFlags.NonPublic;
}

var fields = type.GetFields(memberFlags);

//Ignore NonSerialized and CompilerGenerated fields.
fields = fields.Where(f => !f.IsNotSerialized && GenericExtensions.GetCustomAttribute<CompilerGeneratedAttribute>(f) == null).ToArray();

var props = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
var props = type.GetProperties(memberFlags);

//Ignore TomlNonSerializedAttribute Decorated Properties
var propsDict = props
Expand Down
9 changes: 7 additions & 2 deletions Tomlet/TomlCompositeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ public static TomlSerializationMethods.Serialize<object> For(Type type, TomlSeri
else
{
//Get all instance fields
var fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
var memberFlags = BindingFlags.Public | BindingFlags.Instance;
if (!options.IgnoreNonPublicMembers) {
memberFlags |= BindingFlags.NonPublic;
}

var fields = type.GetFields(memberFlags);
var fieldAttribs = fields
.ToDictionary(f => f, f => new {inline = GenericExtensions.GetCustomAttribute<TomlInlineCommentAttribute>(f), preceding = GenericExtensions.GetCustomAttribute<TomlPrecedingCommentAttribute>(f), noInline = GenericExtensions.GetCustomAttribute<TomlDoNotInlineObjectAttribute>(f)});
var props = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
var props = type.GetProperties(memberFlags)
.ToArray();
var propAttribs = props
.ToDictionary(p => p, p => new {inline = GenericExtensions.GetCustomAttribute<TomlInlineCommentAttribute>(p), preceding = GenericExtensions.GetCustomAttribute<TomlPrecedingCommentAttribute>(p), prop = GenericExtensions.GetCustomAttribute<TomlPropertyAttribute>(p), noInline = GenericExtensions.GetCustomAttribute<TomlDoNotInlineObjectAttribute>(p)});
Expand Down
1 change: 1 addition & 0 deletions Tomlet/TomlSerializerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ public class TomlSerializerOptions
{
public static TomlSerializerOptions Default = new();
public bool OverrideConstructorValues { get; set; } = false;
public bool IgnoreNonPublicMembers { get; set; } = false;
}
}

0 comments on commit 9fd4001

Please sign in to comment.