From 6a2a4eefb59455bdb02e8ff6aadf6a766bade8ea Mon Sep 17 00:00:00 2001 From: aldelaro5 Date: Tue, 23 Dec 2025 11:28:47 -0500 Subject: [PATCH] Add an option to specify the max amount of table entries for inline tables --- Tomlet/Models/TomlTable.cs | 3 ++- Tomlet/TomlCompositeSerializer.cs | 18 +++++++++++++----- Tomlet/TomlSerializerOptions.cs | 9 ++++++++- 3 files changed, 23 insertions(+), 7 deletions(-) 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 37daa9a..60b0dde 100644 --- a/Tomlet/TomlSerializerOptions.cs +++ b/Tomlet/TomlSerializerOptions.cs @@ -1,4 +1,6 @@ -namespace Tomlet; +using Tomlet.Models; + +namespace Tomlet; public class TomlSerializerOptions { @@ -18,4 +20,9 @@ public class TomlSerializerOptions /// 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. /// 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