Files
MonoGame.Extended/source/MonoGame.Extended.Particles/Serialization/ModifierExecutionStrategyJsonConverter.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

35 lines
1.3 KiB
C#

using System;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace MonoGame.Extended.Particles.Serialization;
/// <summary>
/// Converts a <see cref="ParticleModifierExecutionStrategy"/> value to or from JSON.
/// </summary>
public class ModifierExecutionStrategyJsonConverter : JsonConverter<ParticleModifierExecutionStrategy>
{
/// <inheritdoc />
public override bool CanConvert(Type typeToConvert) =>
typeToConvert == typeof(ParticleModifierExecutionStrategy) ||
typeToConvert.GetTypeInfo().BaseType == typeof(ParticleModifierExecutionStrategy);
/// <inheritdoc />
public override ParticleModifierExecutionStrategy Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var value = JsonSerializer.Deserialize<string>(ref reader, options);
return ParticleModifierExecutionStrategy.Parse(value);
}
/// <inheritdoc />
/// <exception cref="ArgumentNullException">
/// Throw if <paramref name="writer"/> is <see langword="null"/>.
/// </exception>
public override void Write(Utf8JsonWriter writer, ParticleModifierExecutionStrategy value, JsonSerializerOptions options)
{
ArgumentNullException.ThrowIfNull(writer);
writer.WriteStringValue(value.ToString());
}
}