summaryrefslogtreecommitdiff
path: root/Plugins/MonoGame.Extended/source/MonoGame.Extended.Particles/Serialization/ModifierExecutionStrategyJsonConverter.cs
blob: 8fc894bbd65c3224d06d20af1fd926f273f49a31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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());
    }
}