Skip to content

Commit

Permalink
chore: switch XRefMap serialization logics to use System.Text.Json
Browse files Browse the repository at this point in the history
  • Loading branch information
filzrev committed Sep 14, 2024
1 parent f47673c commit 165bb8a
Show file tree
Hide file tree
Showing 8 changed files with 3,639 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/Docfx.Build/XRefMaps/XRefMapDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private static async ValueTask<IXRefContainer> ReadLocalFileAsync(string filePat
switch (Path.GetExtension(Path.GetFileNameWithoutExtension(filePath)).ToLowerInvariant())
{
case ".json":
return await SystemTextJsonUtility.DeserializeAsync<XRefMap>(stream, token);
return await JsonUtility.DeserializeAsync<XRefMap>(stream, token);
case ".yml":
default:
{
Expand All @@ -141,7 +141,7 @@ private static async ValueTask<IXRefContainer> ReadLocalFileAsync(string filePat
case ".json":
{
using var stream = File.OpenRead(filePath);
return await SystemTextJsonUtility.DeserializeAsync<XRefMap>(stream, token);
return await JsonUtility.DeserializeAsync<XRefMap>(stream, token);
}

case ".yml":
Expand Down Expand Up @@ -172,7 +172,7 @@ private static async Task<XRefMap> DownloadFromWebAsync(Uri uri, CancellationTok
{
case ".json":
{
var xrefMap = await SystemTextJsonUtility.DeserializeAsync<XRefMap>(stream, token);
var xrefMap = await JsonUtility.DeserializeAsync<XRefMap>(stream, token);
xrefMap.BaseUrl = ResolveBaseUrl(xrefMap, uri);
return xrefMap;
}
Expand Down
13 changes: 12 additions & 1 deletion src/Docfx.Common/Json/JsonUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ public static T Deserialize<T>(TextReader reader)
return NewtonsoftJsonUtility.Deserialize<T>(reader);
}

internal static ValueTask<T> DeserializeAsync<T>(Stream stream, CancellationToken cancellationToken)
{
if (IsSystemTextJsonSupported<T>())
return SystemTextJsonUtility.DeserializeAsync<T>(stream, cancellationToken);
else
throw new NotSupportedException();
}

public static string ToJsonString<T>(this T graph)
{
if (IsSystemTextJsonSupported<T>())
Expand Down Expand Up @@ -79,9 +87,12 @@ private static bool IsSupported()
var type = typeof(T);
var fullName = type.FullName;

// TODO: Return `true` for types that support serialize/deserializenon with System.Text.Json.
switch (fullName)
{
// TODO: Return `true` for types that support serialize/deserializenon with System.Text.Json.
case "Docfx.Build.Engine.XRefMap":
return true;

default:
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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.Build.Engine;
using Docfx.Common;
using Docfx.Plugins;
using FluentAssertions;

namespace docfx.Tests;

public partial class JsonSerializationTest
{
[Theory]
[TestData<XRefMap>]
public void JsonSerializationTest_XRefMap(string path)
{
// Arrange
var model = TestData.Load<XRefMap>(path);

// Act/Assert
ValidateJsonRoundTrip(model);
}
}
1,926 changes: 1,926 additions & 0 deletions test/docfx.Tests/SerializationTests/TestData/XRefMap/xrefmap_01.json

Large diffs are not rendered by default.

Loading

0 comments on commit 165bb8a

Please sign in to comment.