Skip to content

Commit

Permalink
chore: add ManagedReference YAML roundtrip tests
Browse files Browse the repository at this point in the history
  • Loading branch information
filzrev committed Sep 28, 2024
1 parent 0644b4a commit bd66e2a
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/Docfx.DataContracts.Common/JTokenConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Reflection;
using System.Text.Json;
using Docfx.Common;
using Newtonsoft.Json.Linq;

Expand All @@ -22,22 +21,23 @@ public static T Convert<T>(object obj)
return jToken.ToObject<T>();
}


var type = typeof(T);

// Try to convert `List<object>` to T (`ObjectToInferredTypesConverter`)
// Currently `ObjectToInferredTypesConverter` convert item to List<object>. So it need to convert type.
if (obj is List<object> list
&& type.GetTypeInfo().IsGenericType
&& type.GetGenericTypeDefinition() == typeof(List<>))
// Custom code path for `System.Text.Json` deserialization.
// `ObjectToInferredTypesConverter` deserialize items as `List<object>`.
// So it need to convert `List<object>` to `List<TItem>`.
if (obj is List<object> list && IsListType<T>())
{
// TODO: performance optimization.
var json = JsonUtility.Serialize(list);
var result = JsonUtility.Deserialize<T>(new StringReader(json));
return result;
}


throw new InvalidCastException();
}

private static bool IsListType<T>()
{
var type = typeof(T);
return type.GetTypeInfo().IsGenericType
&& type.GetGenericTypeDefinition() == typeof(List<>);
}
}

0 comments on commit bd66e2a

Please sign in to comment.