Use IReadOnlyList<T> for TypedListConverter.

This commit is contained in:
Huo Yaoyuan 2020-05-12 21:49:55 +08:00
parent f7d26688f4
commit f07d95ac59

View File

@ -9,12 +9,12 @@ using Newtonsoft.Json.Linq;
namespace osu.Game.IO.Serialization.Converters namespace osu.Game.IO.Serialization.Converters
{ {
/// <summary> /// <summary>
/// A type of <see cref="JsonConverter"/> that serializes an <see cref="IEnumerable{T}"/> alongside /// A type of <see cref="JsonConverter"/> that serializes an <see cref="IReadOnlyList{T}"/> alongside
/// a lookup table for the types contained. The lookup table is used in deserialization to /// a lookup table for the types contained. The lookup table is used in deserialization to
/// 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="IEnumerable{T}"/> this attribute is attached to.</typeparam> /// <typeparam name="T">The type of objects contained in the <see cref="IReadOnlyList{T}"/> this attribute is attached to.</typeparam>
public class TypedListConverter<T> : JsonConverter<IEnumerable<T>> public class TypedListConverter<T> : JsonConverter<IReadOnlyList<T>>
{ {
private readonly bool requiresTypeVersion; private readonly bool requiresTypeVersion;
@ -36,7 +36,7 @@ namespace osu.Game.IO.Serialization.Converters
this.requiresTypeVersion = requiresTypeVersion; this.requiresTypeVersion = requiresTypeVersion;
} }
public override IEnumerable<T> ReadJson(JsonReader reader, Type objectType, IEnumerable<T> existingValue, bool hasExistingValue, JsonSerializer serializer) public override IReadOnlyList<T> ReadJson(JsonReader reader, Type objectType, IReadOnlyList<T> existingValue, bool hasExistingValue, JsonSerializer serializer)
{ {
var list = new List<T>(); var list = new List<T>();
@ -57,7 +57,7 @@ namespace osu.Game.IO.Serialization.Converters
return list; return list;
} }
public override void WriteJson(JsonWriter writer, IEnumerable<T> value, JsonSerializer serializer) public override void WriteJson(JsonWriter writer, IReadOnlyList<T> value, JsonSerializer serializer)
{ {
var lookupTable = new List<string>(); var lookupTable = new List<string>();
var objects = new List<JObject>(); var objects = new List<JObject>();