diff --git a/Tomlet/Models/TomlTable.cs b/Tomlet/Models/TomlTable.cs index 67fac2d..ad7c716 100644 --- a/Tomlet/Models/TomlTable.cs +++ b/Tomlet/Models/TomlTable.cs @@ -18,12 +18,13 @@ public class TomlTable : TomlValue, IEnumerable> internal bool Defined; public bool ForceNoInline { get; set; } + public int MaxInlineEntriesCount { get; set; } = 3; public override string StringValue => $"Table ({Entries.Count} entries)"; public HashSet Keys => new(Entries.Keys); - public bool ShouldBeSerializedInline => !ForceNoInline && Entries.Count < 4 + public bool ShouldBeSerializedInline => !ForceNoInline && Entries.Count <= MaxInlineEntriesCount && Entries.All(e => !e.Key.Contains(" ") && e.Value.Comments.ThereAreNoComments && (e.Value is TomlArray arr ? arr.IsSimpleArray : e.Value is not TomlTable)); diff --git a/Tomlet/TomlCompositeSerializer.cs b/Tomlet/TomlCompositeSerializer.cs index 26421fe..f91b3c9 100644 --- a/Tomlet/TomlCompositeSerializer.cs +++ b/Tomlet/TomlCompositeSerializer.cs @@ -59,7 +59,7 @@ public static TomlSerializationMethods.Serialize For(Type type, TomlSeri if (instance == null) throw new ArgumentNullException(nameof(instance), "Object being serialized is null. TOML does not support null values."); - var resultTable = new TomlTable {ForceNoInline = isForcedNoInline}; + var resultTable = new TomlTable {ForceNoInline = isForcedNoInline, MaxInlineEntriesCount = options.MaxTableEntriesCountToInline}; foreach (var field in fields) { @@ -84,8 +84,12 @@ public static TomlSerializationMethods.Serialize For(Type type, TomlSeri tomlValue.Comments.InlineComment = thisFieldAttribs.inline?.Comment; tomlValue.Comments.PrecedingComment = thisFieldAttribs.preceding?.Comment; - if(thisFieldAttribs.noInline != null && tomlValue is TomlTable table) - table.ForceNoInline = true; + if (tomlValue is TomlTable table) + { + table.MaxInlineEntriesCount = options.MaxTableEntriesCountToInline; + if (thisFieldAttribs.noInline != null) + table.ForceNoInline = true; + } resultTable.PutValue(thisFieldAttribs.field?.GetMappedString() ?? field.Name, tomlValue); } @@ -113,8 +117,12 @@ public static TomlSerializationMethods.Serialize For(Type type, TomlSeri tomlValue.Comments.InlineComment = thisPropAttribs.inline?.Comment; tomlValue.Comments.PrecedingComment = thisPropAttribs.preceding?.Comment; - if (thisPropAttribs.noInline != null && tomlValue is TomlTable table) - table.ForceNoInline = true; + if (tomlValue is TomlTable table) + { + table.MaxInlineEntriesCount = options.MaxTableEntriesCountToInline; + if (thisPropAttribs.noInline != null) + table.ForceNoInline = true; + } resultTable.PutValue(thisPropAttribs.prop?.GetMappedString() ?? prop.Name, tomlValue); } diff --git a/Tomlet/TomlSerializerOptions.cs b/Tomlet/TomlSerializerOptions.cs index 0cdcf87..c0c090e 100644 --- a/Tomlet/TomlSerializerOptions.cs +++ b/Tomlet/TomlSerializerOptions.cs @@ -1,4 +1,6 @@ -namespace Tomlet; +using Tomlet.Models; + +namespace Tomlet; public class TomlSerializerOptions { @@ -21,4 +23,9 @@ public class TomlSerializerOptions /// When deserializing dictionaries with enums as keys, invalid enum values will be skipped. /// public bool IgnoreInvalidEnumValues { get; set; } = false; + + /// + /// The maximum amount of entries a table may have for the table to be serialized as an inline table when is false. The default value is 3. + /// + public int MaxTableEntriesCountToInline { get; set; } = 3; } \ No newline at end of file