-
Notifications
You must be signed in to change notification settings - Fork 868
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add MarkdigExtensionSetting json roundtrip tests
- Loading branch information
Showing
9 changed files
with
238 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/Docfx.MarkdigEngine.Extensions/MarkdigExtensionSettingConverter.NewtonsoftJson.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Newtonsoft.Json.Linq; | ||
using Newtonsoft.Json; | ||
|
||
namespace Docfx.MarkdigEngine.Extensions; | ||
|
||
internal partial class MarkdigExtensionSettingConverter | ||
{ | ||
internal class NewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter | ||
{ | ||
/// <inheritdoc/> | ||
public override bool CanConvert(Type objectType) | ||
{ | ||
return objectType == typeof(MarkdigExtensionSetting); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
// var value = reader.Value; | ||
switch (reader.TokenType) | ||
{ | ||
case JsonToken.String: | ||
{ | ||
var name = (string)reader.Value; | ||
return new MarkdigExtensionSetting(name); | ||
} | ||
case JsonToken.StartObject: | ||
{ | ||
var jObj = JObject.Load(reader); | ||
|
||
var props = jObj.Properties().ToArray(); | ||
|
||
// Object key must be the name of markdig extension. | ||
if (props.Length != 1) | ||
return null; | ||
|
||
var prop = props[0]; | ||
var name = prop.Name; | ||
|
||
var options = prop.Value; | ||
if (options.Count() == 0) | ||
{ | ||
return new MarkdigExtensionSetting(name); | ||
} | ||
|
||
// Convert JToken to JsonElement. | ||
var json = options.ToString(); | ||
using var doc = System.Text.Json.JsonDocument.Parse(json); | ||
var jsonElement = doc.RootElement.Clone(); | ||
|
||
return new MarkdigExtensionSetting(name) | ||
{ | ||
Options = jsonElement, | ||
}; | ||
} | ||
|
||
default: | ||
return null; | ||
} | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
if (value == null) | ||
return; | ||
|
||
var model = (MarkdigExtensionSetting)value; | ||
|
||
if (model.Options == null || !model.Options.HasValue) | ||
{ | ||
writer.WriteValue(model.Name); | ||
} | ||
else | ||
{ | ||
writer.WriteStartObject(); | ||
writer.WritePropertyName(model.Name); | ||
var json = model.Options.ToString(); | ||
writer.WriteRawValue(json); | ||
writer.WriteEndObject(); | ||
} | ||
} | ||
} | ||
} | ||
|
75 changes: 75 additions & 0 deletions
75
src/Docfx.MarkdigEngine.Extensions/MarkdigExtensionSettingConverter.SystemTextJson.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using System.Text.Json.Nodes; | ||
using System.Xml.Linq; | ||
|
||
namespace Docfx.MarkdigEngine.Extensions; | ||
|
||
internal partial class MarkdigExtensionSettingConverter | ||
{ | ||
internal class SystemTextJsonConverter : JsonConverter<MarkdigExtensionSetting> | ||
{ | ||
public override MarkdigExtensionSetting Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
switch (reader.TokenType) | ||
{ | ||
case JsonTokenType.String: | ||
{ | ||
var name = reader.GetString(); | ||
return new MarkdigExtensionSetting(name); | ||
} | ||
case JsonTokenType.StartObject: | ||
{ | ||
var elem = JsonElement.ParseValue(ref reader); | ||
|
||
var props = elem.EnumerateObject().ToArray(); | ||
|
||
// Object key must be the name of markdig extension. | ||
if (props.Length != 1) | ||
return null; | ||
|
||
var prop = props[0]; | ||
var name = prop.Name; | ||
var value = prop.Value; | ||
|
||
if (value.ValueKind != JsonValueKind.Object) | ||
{ | ||
return new MarkdigExtensionSetting(name); | ||
} | ||
|
||
return new MarkdigExtensionSetting(name) | ||
{ | ||
Options = value, | ||
}; | ||
} | ||
|
||
default: | ||
return null; | ||
} | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, MarkdigExtensionSetting value, JsonSerializerOptions options) | ||
{ | ||
if (value == null) | ||
return; | ||
|
||
var model = (MarkdigExtensionSetting)value; | ||
|
||
if (model.Options == null || !model.Options.HasValue) | ||
{ | ||
writer.WriteStringValue(model.Name); | ||
} | ||
else | ||
{ | ||
writer.WriteStartObject(); | ||
writer.WritePropertyName(model.Name); | ||
var json = model.Options.ToString(); | ||
writer.WriteRawValue(json); | ||
writer.WriteEndObject(); | ||
} | ||
} | ||
} | ||
} |
87 changes: 8 additions & 79 deletions
87
src/Docfx.MarkdigEngine.Extensions/MarkdigExtensionSettingConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,25 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Docfx.MarkdigEngine.Extensions; | ||
|
||
internal class MarkdigExtensionSettingConverter : Newtonsoft.Json.JsonConverter | ||
internal partial class MarkdigExtensionSettingConverter | ||
{ | ||
// JsonSerializerOptions that used to deserialize MarkdigExtension options. | ||
// Shared JsonSerializerOptions instance. | ||
internal static readonly System.Text.Json.JsonSerializerOptions DefaultSerializerOptions = new() | ||
{ | ||
IncludeFields = true, | ||
AllowTrailingCommas = true, | ||
DictionaryKeyPolicy = System.Text.Json.JsonNamingPolicy.CamelCase, | ||
PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase, | ||
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase, | ||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, | ||
PropertyNameCaseInsensitive = true, | ||
Converters = { | ||
new System.Text.Json.Serialization.JsonStringEnumConverter() | ||
new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) | ||
}, | ||
WriteIndented = false, | ||
}; | ||
|
||
/// <inheritdoc/> | ||
public override bool CanConvert(Type objectType) | ||
{ | ||
return objectType == typeof(MarkdigExtensionSetting); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
// var value = reader.Value; | ||
switch (reader.TokenType) | ||
{ | ||
case JsonToken.String: | ||
{ | ||
var name = (string)reader.Value; | ||
return new MarkdigExtensionSetting(name); | ||
} | ||
case JsonToken.StartObject: | ||
{ | ||
var jObj = JObject.Load(reader); | ||
|
||
var props = jObj.Properties().ToArray(); | ||
|
||
// Object key must be the name of markdig extension. | ||
if (props.Length != 1) | ||
return null; | ||
|
||
var prop = props[0]; | ||
var name = prop.Name; | ||
|
||
var options = prop.Value; | ||
if (options.Count() == 0) | ||
{ | ||
return new MarkdigExtensionSetting(name); | ||
} | ||
|
||
// Serialize options to JsonElement. | ||
var jsonElement = System.Text.Json.JsonSerializer.SerializeToElement(options, DefaultSerializerOptions); | ||
|
||
return new MarkdigExtensionSetting(name) | ||
{ | ||
Options = jsonElement, | ||
}; | ||
} | ||
|
||
default: | ||
return null; | ||
} | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
if (value == null) | ||
return; | ||
|
||
var model = (MarkdigExtensionSetting)value; | ||
|
||
if (model.Options == null || !model.Options.HasValue) | ||
{ | ||
writer.WriteValue(model.Name); | ||
} | ||
else | ||
{ | ||
writer.WriteStartObject(); | ||
writer.WritePropertyName(model.Name); | ||
var json = model.Options.ToString(); | ||
writer.WriteRawValue(json); | ||
writer.WriteEndObject(); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
test/docfx.Tests/SerializationTests/JsonSerializationTest.MarkdownServiceProperties.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Text.Json; | ||
using Docfx; | ||
using Docfx.Common; | ||
using Docfx.MarkdigEngine.Extensions; | ||
using Docfx.Plugins; | ||
using FluentAssertions; | ||
|
||
namespace docfx.Tests; | ||
|
||
public partial class JsonSerializationTest | ||
{ | ||
[Theory] | ||
[TestData<MarkdownServiceProperties>] | ||
public void JsonSerializationTest_MarkdownServiceProperties(string path) | ||
{ | ||
// Arrange | ||
var model = TestData.Load<MarkdownServiceProperties>(path); | ||
|
||
// Act/Assert | ||
ValidateJsonRoundTrip(model); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...s/SerializationTests/TestData/MarkdownServiceProperties/markdownServiceProperties_01.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"enableSourceInfo": false, | ||
"markdigExtensions": [ | ||
"FootNotes", | ||
{ "Emojis": "default" }, | ||
{ "AutoIdentifiers": "default" }, | ||
{ | ||
"MediaLinks": { | ||
"width": 800, | ||
"height": 400 | ||
} | ||
} | ||
], | ||
"fallbackFolders": [ | ||
"fallbackdir" | ||
], | ||
"alerts": { | ||
"TODO": "alert alert-secondary" | ||
}, | ||
"plantUml": { | ||
"javaPath": "dummy", | ||
"localGraphvizDotPath": "dummy", | ||
"localPlantUmlPath": "dummy", | ||
"outputFormat": "svg", | ||
"remoteUrl": "dummy", | ||
"renderingMode": "local" | ||
} | ||
} |