Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to ignore non-public members when de/serializing #43

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading