1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-27 01:12:54 +08:00
osu-lazer/osu.Game/IO/Serialization/JsonSerializableExtensions.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

31 lines
1.3 KiB
C#
Raw Normal View History

// 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
using System.Collections.Generic;
using Newtonsoft.Json;
using osu.Framework.IO.Serialization;
2018-04-13 17:19:50 +08:00
namespace osu.Game.IO.Serialization
{
public static class JsonSerializableExtensions
{
public static string Serialize(this object obj) => JsonConvert.SerializeObject(obj, CreateGlobalSettings());
2018-04-13 17:19:50 +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
public static JsonSerializerSettings CreateGlobalSettings() => new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
Formatting = Formatting.Indented,
ObjectCreationHandling = ObjectCreationHandling.Replace,
DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate,
Converters = new List<JsonConverter> { new Vector2Converter() },
ContractResolver = new SnakeCaseKeyContractResolver()
};
}
}