JD2022-TU1/main/tools/services/JD.SonyTrophy.Service/Extension/JObjectExtension.cs

34 lines
873 B
C#

using Newtonsoft.Json.Linq;
namespace JD.SonyTrophy.Service.Extension
{
public static class JObjectExtension
{
public static T Get<T>(this JObject jObject, string propertyName)
where T : JToken
{
if (jObject != null && jObject.TryGetValue(propertyName, out JToken token) && token is T child)
{
return child;
}
return null;
}
public static T GetOrCreate<T>(this JObject jObject, string propertyName)
where T : JToken, new()
{
T child = jObject.Get<T>(propertyName);
if (jObject != null && child is null)
{
jObject.Remove(propertyName);
child = new T();
jObject.Add(propertyName, child);
}
return child;
}
}
}