1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-23 05:08:34 +01:00

allow using a custom json serializer for LoadJson

This commit is contained in:
Ell 2020-11-22 16:57:44 +01:00
parent 6e7720295f
commit 3044a2bc81

View file

@ -53,10 +53,12 @@ namespace MLEM.Data {
/// <param name="content">The content manager to load content with</param> /// <param name="content">The content manager to load content with</param>
/// <param name="name">The name of the file to load</param> /// <param name="name">The name of the file to load</param>
/// <param name="extensions">The file extensions that should be appended, or ".json", ".json5" and ".jsonc" by default.</param> /// <param name="extensions">The file extensions that should be appended, or ".json", ".json5" and ".jsonc" by default.</param>
/// <param name="serializer">The json serializer to use, or <see cref="GetJsonSerializer"/> by default.</param>
/// <typeparam name="T">The type of asset to load</typeparam> /// <typeparam name="T">The type of asset to load</typeparam>
/// <returns>The loaded asset</returns> /// <returns>The loaded asset</returns>
public static T LoadJson<T>(this ContentManager content, string name, string[] extensions = null) { public static T LoadJson<T>(this ContentManager content, string name, string[] extensions = null, JsonSerializer serializer = null) {
var triedFiles = new List<string>(); var triedFiles = new List<string>();
var serializerToUse = serializer ?? content.GetJsonSerializer();
foreach (var extension in extensions ?? JsonExtensions) { foreach (var extension in extensions ?? JsonExtensions) {
var file = Path.Combine(content.RootDirectory, name + extension); var file = Path.Combine(content.RootDirectory, name + extension);
triedFiles.Add(file); triedFiles.Add(file);
@ -64,7 +66,7 @@ namespace MLEM.Data {
continue; continue;
using (var stream = File.OpenText(file)) { using (var stream = File.OpenText(file)) {
using (var reader = new JsonTextReader(stream)) { using (var reader = new JsonTextReader(stream)) {
return GetJsonSerializer(content).Deserialize<T>(reader); return serializerToUse.Deserialize<T>(reader);
} }
} }
} }