mirror of
https://github.com/AlexMacocian/MonoGame.Extended.git
synced 2026-07-15 15:09:29 +00:00
d008b1bd41
* 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
34 lines
1.0 KiB
C#
34 lines
1.0 KiB
C#
using System;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace MonoGame.Extended.Particles.Serialization;
|
|
|
|
public class TimeSpanJsonConverter : JsonConverter<TimeSpan>
|
|
{
|
|
/// <inheritdoc />
|
|
public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(TimeSpan);
|
|
|
|
/// <inheritdoc />
|
|
public override TimeSpan Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
{
|
|
if (reader.TokenType == JsonTokenType.Number)
|
|
{
|
|
double seconds = reader.GetDouble();
|
|
return TimeSpan.FromSeconds(seconds);
|
|
}
|
|
|
|
return TimeSpan.Zero;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
/// <exception cref="ArgumentNullException">
|
|
/// Throw if <paramref name="writer"/> is <see langword="null"/>.
|
|
/// </exception>
|
|
public override void Write(Utf8JsonWriter writer, TimeSpan value, JsonSerializerOptions options)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(writer);
|
|
writer.WriteNumberValue(value.TotalSeconds);
|
|
}
|
|
}
|