1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-13 12:53:11 +08:00

Use strongly-typed JsonConerter.

This commit is contained in:
Huo Yaoyuan 2020-05-09 16:39:46 +08:00
parent 2d49210250
commit 74cbe9306c
2 changed files with 9 additions and 17 deletions

View File

@ -14,7 +14,7 @@ namespace osu.Game.IO.Serialization.Converters
/// reconstruct the objects with their original types. /// reconstruct the objects with their original types.
/// </summary> /// </summary>
/// <typeparam name="T">The type of objects contained in the <see cref="List{T}"/> this attribute is attached to.</typeparam> /// <typeparam name="T">The type of objects contained in the <see cref="List{T}"/> this attribute is attached to.</typeparam>
public class TypedListConverter<T> : JsonConverter public class TypedListConverter<T> : JsonConverter<List<T>>
{ {
private readonly bool requiresTypeVersion; private readonly bool requiresTypeVersion;
@ -36,9 +36,7 @@ namespace osu.Game.IO.Serialization.Converters
this.requiresTypeVersion = requiresTypeVersion; this.requiresTypeVersion = requiresTypeVersion;
} }
public override bool CanConvert(Type objectType) => objectType == typeof(List<T>); public override List<T> ReadJson(JsonReader reader, Type objectType, List<T> existingValue, bool hasExistingValue, JsonSerializer serializer)
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{ {
var list = new List<T>(); var list = new List<T>();
@ -59,14 +57,12 @@ namespace osu.Game.IO.Serialization.Converters
return list; return list;
} }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) public override void WriteJson(JsonWriter writer, List<T> value, JsonSerializer serializer)
{ {
var list = (IEnumerable<T>)value;
var lookupTable = new List<string>(); var lookupTable = new List<string>();
var objects = new List<JObject>(); var objects = new List<JObject>();
foreach (var item in list) foreach (var item in value)
{ {
var type = item.GetType(); var type = item.GetType();
var assemblyName = type.Assembly.GetName(); var assemblyName = type.Assembly.GetName();

View File

@ -11,26 +11,22 @@ namespace osu.Game.IO.Serialization.Converters
/// <summary> /// <summary>
/// A type of <see cref="JsonConverter"/> that serializes only the X and Y coordinates of a <see cref="Vector2"/>. /// A type of <see cref="JsonConverter"/> that serializes only the X and Y coordinates of a <see cref="Vector2"/>.
/// </summary> /// </summary>
public class Vector2Converter : JsonConverter public class Vector2Converter : JsonConverter<Vector2>
{ {
public override bool CanConvert(Type objectType) => objectType == typeof(Vector2); public override Vector2 ReadJson(JsonReader reader, Type objectType, Vector2 existingValue, bool hasExistingValue, JsonSerializer serializer)
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{ {
var obj = JObject.Load(reader); var obj = JObject.Load(reader);
return new Vector2((float)obj["x"], (float)obj["y"]); return new Vector2((float)obj["x"], (float)obj["y"]);
} }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) public override void WriteJson(JsonWriter writer, Vector2 value, JsonSerializer serializer)
{ {
var vector = (Vector2)value;
writer.WriteStartObject(); writer.WriteStartObject();
writer.WritePropertyName("x"); writer.WritePropertyName("x");
writer.WriteValue(vector.X); writer.WriteValue(value.X);
writer.WritePropertyName("y"); writer.WritePropertyName("y");
writer.WriteValue(vector.Y); writer.WriteValue(value.Y);
writer.WriteEndObject(); writer.WriteEndObject();
} }