2017-04-06 14:54:50 +08:00
|
|
|
|
// 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)
|
|
|
|
|
{
|
2017-08-05 15:22:10 +08:00
|
|
|
|
return JsonConvert.SerializeObject(obj, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
|
2017-04-06 14:54:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-08 19:33:37 +08:00
|
|
|
|
public static T Deserialize<T>(this string objString)
|
2017-04-06 14:54:50 +08:00
|
|
|
|
{
|
|
|
|
|
return JsonConvert.DeserializeObject<T>(objString);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-08 19:33:37 +08:00
|
|
|
|
public static T DeepClone<T>(this T obj)
|
|
|
|
|
where T : IJsonSerializable
|
2017-04-06 14:54:50 +08:00
|
|
|
|
{
|
|
|
|
|
return Deserialize<T>(Serialize(obj));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|