2018-01-05 19:21:19 +08:00
|
|
|
|
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
2017-04-06 14:54:50 +08:00
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
|
|
|
|
|
using Newtonsoft.Json;
|
2017-12-05 23:37:37 +08:00
|
|
|
|
using osu.Game.IO.Serialization.Converters;
|
2017-04-06 14:54:50 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.IO.Serialization
|
|
|
|
|
{
|
|
|
|
|
public interface IJsonSerializable
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class JsonSerializableExtensions
|
|
|
|
|
{
|
2017-12-05 23:37:37 +08:00
|
|
|
|
public static string Serialize(this IJsonSerializable obj) => JsonConvert.SerializeObject(obj, CreateGlobalSettings());
|
2017-04-06 14:54:50 +08:00
|
|
|
|
|
2017-12-05 23:37:37 +08:00
|
|
|
|
public static T Deserialize<T>(this string objString) => JsonConvert.DeserializeObject<T>(objString, CreateGlobalSettings());
|
|
|
|
|
|
2017-12-07 02:39:43 +08:00
|
|
|
|
public static void DeserializeInto<T>(this string objString, T target) => JsonConvert.PopulateObject(objString, target, CreateGlobalSettings());
|
2017-12-05 23:37:37 +08:00
|
|
|
|
|
|
|
|
|
public static T DeepClone<T>(this T obj) where T : IJsonSerializable => Deserialize<T>(Serialize(obj));
|
2017-04-06 14:54:50 +08:00
|
|
|
|
|
2017-12-07 10:17:32 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates the default <see cref="JsonSerializerSettings"/> that should be used for all <see cref="IJsonSerializable"/>s.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2017-12-05 23:37:37 +08:00
|
|
|
|
public static JsonSerializerSettings CreateGlobalSettings() => new JsonSerializerSettings
|
2017-04-06 14:54:50 +08:00
|
|
|
|
{
|
2017-12-05 23:37:37 +08:00
|
|
|
|
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
|
|
|
|
|
Formatting = Formatting.Indented,
|
|
|
|
|
ObjectCreationHandling = ObjectCreationHandling.Replace,
|
|
|
|
|
DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate,
|
2017-12-07 13:36:32 +08:00
|
|
|
|
Converters = new JsonConverter[] { new Vector2Converter() },
|
|
|
|
|
ContractResolver = new KeyContractResolver()
|
2017-12-05 23:37:37 +08:00
|
|
|
|
};
|
2017-04-06 14:54:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|