Skip to content
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
3 changes: 2 additions & 1 deletion Tomlet/Models/TomlTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ public class TomlTable : TomlValue, IEnumerable<KeyValuePair<string, TomlValue>>
internal bool Defined;

public bool ForceNoInline { get; set; }
public int MaxInlineEntriesCount { get; set; } = 3;

public override string StringValue => $"Table ({Entries.Count} entries)";

public HashSet<string> 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));
Expand Down
18 changes: 13 additions & 5 deletions Tomlet/TomlCompositeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static TomlSerializationMethods.Serialize<object> 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)
{
Expand All @@ -84,8 +84,12 @@ public static TomlSerializationMethods.Serialize<object> 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);
}
Expand Down Expand Up @@ -113,8 +117,12 @@ public static TomlSerializationMethods.Serialize<object> 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);
}
Expand Down
9 changes: 8 additions & 1 deletion Tomlet/TomlSerializerOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Tomlet;
using Tomlet.Models;

namespace Tomlet;

public class TomlSerializerOptions
{
Expand All @@ -21,4 +23,9 @@ public class TomlSerializerOptions
/// When deserializing dictionaries with enums as keys, invalid enum values will be skipped.
/// </remarks>
public bool IgnoreInvalidEnumValues { get; set; } = false;

/// <summary>
/// The maximum amount of entries a table may have for the table to be serialized as an inline table when <see cref="TomlTable.ForceNoInline"/> is false. The default value is 3.
/// </summary>
public int MaxTableEntriesCountToInline { get; set; } = 3;
}
Loading