mirror of
https://github.com/aelurum/AssetStudio.git
synced 2026-11-14 11:18:47 -05:00
48 lines
1.6 KiB
C#
48 lines
1.6 KiB
C#
using System;
|
|
using System.Globalization;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace AssetStudio
|
|
{
|
|
public static partial class JsonConverterHelper
|
|
{
|
|
public class FloatConverter : JsonConverter<float>
|
|
{
|
|
public override float Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
{
|
|
return JsonSerializer.Deserialize<float>(ref reader, new JsonSerializerOptions
|
|
{
|
|
NumberHandling = options.NumberHandling
|
|
});
|
|
}
|
|
|
|
public override void Write(Utf8JsonWriter writer, float value, JsonSerializerOptions options)
|
|
{
|
|
if (float.IsNaN(value) || float.IsInfinity(value))
|
|
{
|
|
if (options.NumberHandling == JsonNumberHandling.AllowNamedFloatingPointLiterals)
|
|
{
|
|
writer.WriteStringValue($"{value.ToString(CultureInfo.InvariantCulture)}");
|
|
}
|
|
else
|
|
{
|
|
writer.WriteStringValue(JsonSerializer.Serialize(value));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (value.Equals((int)value))
|
|
{
|
|
writer.WriteNumberValue((decimal)value + 0.0m);
|
|
}
|
|
else
|
|
{
|
|
writer.WriteNumberValue(value);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|