mirror of
https://github.com/aelurum/AssetStudio.git
synced 2026-11-15 19:28:48 -05:00
Migrate to System.Text.Json in Live2D exporter
- [exp3.json] Fixed format of Blend values (there should be strings according to the official specification) - Fixed BlendType enum
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace CubismLive2DExtractor.CubismJsonConverters
|
||||
{
|
||||
public sealed class MyJsonConverterBool : JsonConverter<bool>
|
||||
{
|
||||
public override bool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
switch(reader.TokenType)
|
||||
{
|
||||
case JsonTokenType.True: return true;
|
||||
case JsonTokenType.False: return false;
|
||||
case JsonTokenType.Number: return reader.GetByte() == 1;
|
||||
default: throw new JsonException($"Unable to convert \"{reader.TokenType}\" token to boolean.");
|
||||
}
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteBooleanValue(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace CubismLive2DExtractor.CubismJsonConverters
|
||||
{
|
||||
public class MyJsonConverterFloat : JsonConverter<float>
|
||||
{
|
||||
private readonly bool _asDecimal;
|
||||
|
||||
public MyJsonConverterFloat(bool asDecimal = false)
|
||||
{
|
||||
_asDecimal = asDecimal;
|
||||
}
|
||||
|
||||
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 (_asDecimal && value.Equals((int)value))
|
||||
{
|
||||
writer.WriteNumberValue((decimal)value + 0.0m);
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.WriteNumberValue(MathF.Round(value, 3));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace CubismLive2DExtractor.CubismJsonConverters
|
||||
{
|
||||
public class MyJsonConverterFloatList : JsonConverter<List<float>>
|
||||
{
|
||||
public override List<float> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
return JsonSerializer.Deserialize<List<float>>(ref reader, new JsonSerializerOptions
|
||||
{
|
||||
NumberHandling = options.NumberHandling
|
||||
});
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, List<float> value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteStartArray();
|
||||
foreach (var n in value)
|
||||
{
|
||||
if (float.IsNaN(n) || float.IsInfinity(n))
|
||||
{
|
||||
if (options.NumberHandling == JsonNumberHandling.AllowNamedFloatingPointLiterals)
|
||||
{
|
||||
writer.WriteStringValue($"{n.ToString(CultureInfo.InvariantCulture)}");
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.WriteStringValue(JsonSerializer.Serialize(value));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.WriteNumberValue(MathF.Round(n, 3));
|
||||
}
|
||||
}
|
||||
writer.WriteEndArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user