-
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 FileMetadataPairs converters and JSON roundtrip test
- Loading branch information
Showing
8 changed files
with
214 additions
and
49 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
64 changes: 64 additions & 0 deletions
64
src/Docfx.App/Config/FileMetadataPairsConverter.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,64 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Docfx.Common; | ||
|
||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Docfx; | ||
|
||
internal partial class FileMetadataPairsConverter | ||
{ | ||
/// <summary> | ||
/// JsonConverter for FileMetadataPairs | ||
/// </summary> | ||
internal class NewtonsoftJsonConverter : JsonConverter | ||
{ | ||
/// <inheritdoc/> | ||
public override bool CanConvert(Type objectType) | ||
{ | ||
return objectType == typeof(FileMetadataPairs); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
var value = reader.Value; | ||
IEnumerable<JToken> jItems; | ||
if (reader.TokenType == JsonToken.StartObject) | ||
{ | ||
jItems = JContainer.Load(reader); | ||
} | ||
else throw new JsonReaderException($"{reader.TokenType} is not a valid {objectType.Name}."); | ||
return new FileMetadataPairs(jItems.Select(ParseItem).ToList()); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
writer.WriteStartObject(); | ||
foreach (var item in ((FileMetadataPairs)value).Items) | ||
{ | ||
writer.WritePropertyName(item.Glob.Raw); | ||
writer.WriteRawValue(JsonUtility.Serialize(item.Value)); | ||
} | ||
writer.WriteEndObject(); | ||
} | ||
|
||
private static FileMetadataPairsItem ParseItem(JToken item) | ||
{ | ||
if (item.Type == JTokenType.Property) | ||
{ | ||
JProperty jProperty = item as JProperty; | ||
var pattern = jProperty.Name; | ||
var rawValue = jProperty.Value; | ||
return new FileMetadataPairsItem(pattern, rawValue); | ||
} | ||
else | ||
{ | ||
throw new JsonReaderException($"Unsupported value {item} (type: {item.Type})."); | ||
} | ||
} | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
src/Docfx.App/Config/FileMetadataPairsConverter.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,78 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Linq; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using Docfx.Common; | ||
|
||
#nullable enable | ||
|
||
namespace Docfx; | ||
|
||
internal partial class FileMetadataPairsConverter | ||
{ | ||
/// <summary> | ||
/// JsonConverter for FileMetadataPairs | ||
/// </summary> | ||
internal class SystemTextJsonConverter : JsonConverter<FileMetadataPairs> | ||
{ | ||
public override FileMetadataPairs Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType != JsonTokenType.StartObject) | ||
{ | ||
throw new JsonException($"{reader.TokenType} is not a valid {typeToConvert.Name}."); | ||
} | ||
|
||
using var document = JsonDocument.ParseValue(ref reader); | ||
var properties = document.RootElement.EnumerateObject(); | ||
var items = properties.Select(x => new FileMetadataPairsItem(x.Name, ToInferredType(x.Value))).ToArray(); | ||
return new FileMetadataPairs(items); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, FileMetadataPairs value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStartObject(); | ||
foreach (var item in value.Items) | ||
{ | ||
writer.WritePropertyName(item.Glob.Raw); | ||
writer.WriteRawValue(JsonUtility.Serialize(item.Value)); | ||
} | ||
writer.WriteEndObject(); | ||
} | ||
|
||
/// <summary> | ||
/// Convert JsonElement to .NET object. | ||
/// </summary> | ||
private static object? ToInferredType(JsonElement elem) | ||
{ | ||
switch (elem.ValueKind) | ||
{ | ||
case JsonValueKind.Null: | ||
return null; | ||
case JsonValueKind.True: | ||
return true; | ||
case JsonValueKind.False: | ||
return false; | ||
case JsonValueKind.String when elem.TryGetDateTime(out DateTime datetime): | ||
return datetime; | ||
case JsonValueKind.String: | ||
return elem.GetString(); | ||
case JsonValueKind.Array: | ||
return elem.EnumerateArray().Select(ToInferredType).ToArray(); | ||
case JsonValueKind.Object: | ||
var properties = elem.EnumerateObject(); | ||
return properties.ToDictionary(x => x.Name, x => ToInferredType(x.Value)); | ||
case JsonValueKind.Number when elem.TryGetInt32(out int intValue): | ||
return intValue; | ||
case JsonValueKind.Number when elem.TryGetInt64(out long longValue): | ||
return longValue; | ||
case JsonValueKind.Number: | ||
return elem.GetDouble(); | ||
case JsonValueKind.Undefined: | ||
default: | ||
throw new NotSupportedException($"{elem.ValueKind}"); | ||
} | ||
} | ||
} | ||
} |
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
22 changes: 22 additions & 0 deletions
22
test/docfx.Tests/SerializationTests/JsonSerializationTest.FileMetadataPairs.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,22 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Docfx; | ||
using Docfx.Common; | ||
using FluentAssertions; | ||
|
||
namespace docfx.Tests; | ||
|
||
public partial class JsonSerializationTest | ||
{ | ||
[Theory] | ||
[TestData<FileMetadataPairs>] | ||
public void JsonSerializationTest_FileMetadataPairs(string path) | ||
{ | ||
// Arrange | ||
var model = TestData.Load<FileMetadataPairs>(path); | ||
|
||
// Act/Assert | ||
ValidateJsonRoundTrip(model); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
test/docfx.Tests/SerializationTests/TestData/FileMetadataPairs/fileMetadataPairs_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 @@ | ||
{ | ||
"priority": { | ||
"**.md": 2.5, | ||
"spec/**.md": 3 | ||
}, | ||
"keywords": { | ||
"obj/docfx/**": [ | ||
"API", | ||
"Reference" | ||
], | ||
"spec/**.md": [ | ||
"Spec", | ||
"Conceptual" | ||
] | ||
}, | ||
"_noindex": { | ||
"articles/**/article.md": true | ||
}, | ||
"others": { | ||
"nested": { | ||
"null": null, | ||
"val": "dummy", | ||
"datetime": "2024-01-01:T00:00:00", | ||
"double": 1.2345, | ||
"array": [ 1, 2, 3, 4 ] | ||
} | ||
} | ||
} |