Files
MonoGame.Extended/source/MonoGame.Extended/Serialization/ContentManagerJsonConverter.cs
T
Christopher Whitley d008b1bd41 Replace Newtonsoft.Json with System.Text.Json (#869)
* Converted to use `System.Text.Json`

* Remove Newtonsoft.Json Dependency
Newtonsoft.Json dependency has been removed in favor of System.Text.Json

* Treat MGFXO file as binary
2024-05-22 23:23:36 -04:00

50 lines
1.8 KiB
C#

using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Xna.Framework.Content;
namespace MonoGame.Extended.Serialization;
/// <summary>
/// Loads content from a JSON file into the <see cref="ContentManager"/> using the asset name
/// </summary>
/// <typeparam name="T">The type of content to load</typeparam>
public class ContentManagerJsonConverter<T> : JsonConverter<T>
{
private readonly ContentManager _contentManager;
private readonly Func<T, string> _getAssetName;
/// <summary>
/// Initializes a new instance of the <see cref="ContentManagerJsonConverter{T}"/> class.
/// </summary>
/// <param name="contentManager">The <see cref="ContentManager"/> used to load content.</param>
/// <param name="getAssetName">A function that returns the asset name for a given instance of <typeparamref name="T"/>.</param>
public ContentManagerJsonConverter(ContentManager contentManager, Func<T, string> getAssetName)
{
_contentManager = contentManager;
_getAssetName = getAssetName;
}
/// <inheritdoc />
public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(T);
/// <inheritdoc />
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var assetName = reader.GetString();
return _contentManager.Load<T>(assetName);
}
/// <inheritdoc />
/// <exception cref="ArgumentNullException">
/// Throw if <paramref name="writer"/> is <see langword="null"/>.
/// </exception>
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
ArgumentNullException.ThrowIfNull(writer);
var asset = (T)value;
var assetName = _getAssetName(asset);
writer.WriteStringValue(assetName);
}
}