1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-29 23:58:34 +01:00
MLEM/MLEM.Data/Json/JsonConverters.cs
2021-03-13 03:15:39 +01:00

30 lines
No EOL
1.2 KiB
C#

using System;
using System.Linq;
using Newtonsoft.Json;
namespace MLEM.Data.Json {
/// <summary>
/// A helper class that stores all of the <see cref="JsonConverter"/> types that are part of MLEM.Data.
/// </summary>
public class JsonConverters {
/// <summary>
/// An array of all of the <see cref="JsonConverter"/>s that are part of MLEM.Data
/// </summary>
public static readonly JsonConverter[] Converters = typeof(JsonConverters).Assembly.GetExportedTypes()
.Where(t => t.IsSubclassOf(typeof(JsonConverter)) && !t.IsGenericType)
.Select(t => t.GetConstructor(Type.EmptyTypes).Invoke(null)).Cast<JsonConverter>().ToArray();
/// <summary>
/// Adds all of the <see cref="JsonConverter"/> objects that are part of MLEM.Data to the given <see cref="JsonSerializer"/>
/// </summary>
/// <param name="serializer">The serializer to add the converters to</param>
/// <returns>The given serializer, for chaining</returns>
public static JsonSerializer AddAll(JsonSerializer serializer) {
foreach (var converter in Converters)
serializer.Converters.Add(converter);
return serializer;
}
}
}