Skip to content

Commit

Permalink
fix: Wrong data is displayed at RestApi page's examples (#10349)
Browse files Browse the repository at this point in the history
fix-restapi-page-examples
  • Loading branch information
filzrev authored Nov 4, 2024
1 parent b492fd0 commit 7f7afad
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/Docfx.Common/Json/JsonUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ public static class JsonUtility
{
public static string Serialize<T>(T graph, bool indented = false)
{
if (IsSystemTextJsonSupported<T>())
return SystemTextJsonUtility.Serialize<T>(graph, indented);
if (IsSystemTextJsonSupported(graph))
return SystemTextJsonUtility.Serialize(graph, indented);
else
return NewtonsoftJsonUtility.Serialize(graph, indented ? Formatting.Indented : Formatting.None);
}

public static void Serialize<T>(string path, T graph, bool indented = false)
{
if (IsSystemTextJsonSupported<T>())
SystemTextJsonUtility.SerializeToFile<T>(path, graph, indented);
if (IsSystemTextJsonSupported(graph))
SystemTextJsonUtility.SerializeToFile(path, graph, indented);
else
NewtonsoftJsonUtility.Serialize(path, graph, indented ? Formatting.Indented : Formatting.None);
}
Expand Down Expand Up @@ -74,6 +74,14 @@ internal static bool IsSystemTextJsonSupported<T>()
return StaticTypeCache<T>.Supported;
}

internal static bool IsSystemTextJsonSupported<T>(T obj)
{
if (typeof(T) == typeof(object) && obj.GetType().FullName.StartsWith("Newtonsoft.", StringComparison.Ordinal))
return false;

return StaticTypeCache<T>.Supported;
}

private static class StaticTypeCache<T>
{
public static readonly bool Supported;
Expand All @@ -90,7 +98,6 @@ private static bool IsSupported()
{
// Use Newtonsoft.Json for RestAPI models.
case "Docfx.DataContracts.RestApi.RestApiRootItemViewModel":
return false;

// Some unit tests using Newtonsoft.Json types.
case "Newtonsoft.Json.Linq.JObject":
Expand Down

0 comments on commit 7f7afad

Please sign in to comment.