mirror of
https://github.com/ppy/osu.git
synced 2024-11-14 18:47:27 +08:00
e75f438c29
Also fixes circular references when using IJsonSerializable.
31 lines
896 B
C#
31 lines
896 B
C#
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
namespace osu.Game.IO.Serialization
|
|
{
|
|
public interface IJsonSerializable
|
|
{
|
|
}
|
|
|
|
public static class JsonSerializableExtensions
|
|
{
|
|
public static string Serialize(this IJsonSerializable obj)
|
|
{
|
|
return JsonConvert.SerializeObject(obj, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
|
|
}
|
|
|
|
public static T Deserialize<T>(this string objString)
|
|
{
|
|
return JsonConvert.DeserializeObject<T>(objString);
|
|
}
|
|
|
|
public static T DeepClone<T>(this T obj)
|
|
where T : IJsonSerializable
|
|
{
|
|
return Deserialize<T>(Serialize(obj));
|
|
}
|
|
}
|
|
}
|