Skip to content

Commit

Permalink
chore: refactor MarkdigExtensionSetting deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
filzrev committed Oct 21, 2024
1 parent 7b7e3e7 commit c5a7791
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public MarkdigExtensionSetting(string name, JsonNode? options = null)
public T GetOptions<T>(T fallbackValue)
{
return Options is null ? fallbackValue
: JsonSerializer.Deserialize<T>(JsonSerializer.Serialize(Options), MarkdigExtensionSettingConverter.DefaultSerializerOptions) ?? fallbackValue;
: Options.Value.Deserialize<T>(MarkdigExtensionSettingConverter.DefaultSerializerOptions) ?? fallbackValue;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ internal partial class MarkdigExtensionSettingConverter
// Shared JsonSerializerOptions instance.
internal static readonly System.Text.Json.JsonSerializerOptions DefaultSerializerOptions = new()
{
IncludeFields = true,
AllowTrailingCommas = true,
ReadCommentHandling = JsonCommentHandling.Skip,
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,14 @@ public void JsonSerializationTest_MarkdownServiceProperties(string path)

// Act/Assert
ValidateJsonRoundTrip(model);

// Additional test to validate deserialized result.
var medialinksSettings = model.MarkdigExtensions.First(x => x.Name == "MediaLinks");
var options = medialinksSettings.GetOptions(fallbackValue: new MediaOptions());
options.Should().BeEquivalentTo(new Markdig.Extensions.MediaLinks.MediaOptions
{
Width = "800",
Height = "400",
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
{ "AutoIdentifiers": "default" },
{
"MediaLinks": {
"width": 800,
"height": 400
"width": "800",
"height": "400"
}
}
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"markdigExtensions": [
"FootNotes",
// Comment1
{ "Emojis": "default" },
{ "AutoIdentifiers": "default" },
{
// Comment2
"MediaLinks": {
"width": "800", // Comment3
"height": "400"
}
}
]
}

0 comments on commit c5a7791

Please sign in to comment.