Skip to content

Commit

Permalink
Add option to not throw on invalid enum deserialization.
Browse files Browse the repository at this point in the history
Closes #48
  • Loading branch information
SamboyCoding committed Dec 22, 2024
1 parent 289d7ce commit 8887144
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Tomlet/TomlCompositeDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public static TomlSerializationMethods.Deserialize<object> For(Type type, TomlSe
}
catch (Exception)
{
if(options.IgnoreInvalidEnumValues)
return Enum.GetValues(type).GetValue(0)!;

throw new TomlEnumParseException(enumName, type);
}
};
Expand Down
13 changes: 13 additions & 0 deletions Tomlet/TomlSerializerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@
public class TomlSerializerOptions
{
public static TomlSerializerOptions Default = new();

/// <summary>
/// When set to false (default) the deserializer will skip assigning fields that have constructor params of the same name.
/// </summary>
public bool OverrideConstructorValues { get; set; } = false;

/// <summary>
/// When set to true, the deserializer will ignore non-public members. When set to false, only members marked [NonSerialized] will be ignored.
/// </summary>
public bool IgnoreNonPublicMembers { get; set; } = false;

/// <summary>
/// When set to true, the deserializer will ignore invalid enum values (and they will be implicitly left at their default value). When set to false, an exception will be thrown if the enum value is not found.
/// </summary>
public bool IgnoreInvalidEnumValues { get; set; } = false;
}

0 comments on commit 8887144

Please sign in to comment.