2019-01-24 16:43:03 +08:00
|
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
|
#nullable disable
|
|
|
|
|
|
2021-03-19 19:07:13 +08:00
|
|
|
|
using System.Collections.Generic;
|
2017-04-06 14:54:50 +08:00
|
|
|
|
using Newtonsoft.Json;
|
2021-03-19 19:07:13 +08:00
|
|
|
|
using osu.Framework.IO.Serialization;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-04-06 14:54:50 +08:00
|
|
|
|
namespace osu.Game.IO.Serialization
|
|
|
|
|
{
|
|
|
|
|
public static class JsonSerializableExtensions
|
|
|
|
|
{
|
2021-08-31 13:38:35 +08:00
|
|
|
|
public static string Serialize(this object obj) => JsonConvert.SerializeObject(obj, CreateGlobalSettings());
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-12-05 23:37:37 +08:00
|
|
|
|
public static T Deserialize<T>(this string objString) => JsonConvert.DeserializeObject<T>(objString, CreateGlobalSettings());
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-12-07 02:39:43 +08:00
|
|
|
|
public static void DeserializeInto<T>(this string objString, T target) => JsonConvert.PopulateObject(objString, target, CreateGlobalSettings());
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
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,
|
2021-03-19 19:07:13 +08:00
|
|
|
|
Converters = new List<JsonConverter> { new Vector2Converter() },
|
2021-09-29 10:26:37 +08:00
|
|
|
|
ContractResolver = new SnakeCaseKeyContractResolver()
|
2017-12-05 23:37:37 +08:00
|
|
|
|
};
|
2017-04-06 14:54:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|