Fixed enum serialization
This commit is contained in:
@@ -932,6 +932,7 @@ public class Props
|
||||
|
||||
[JsonPropertyName("effects_damage")]
|
||||
[JsonConverter(typeof(ArrayToObjectFactoryConverter))]
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.Never)]
|
||||
public Dictionary<string, EffectDamageProps>? EffectsDamage { get; set; }
|
||||
|
||||
[JsonPropertyName("maximumNumberOfUsage")]
|
||||
|
||||
@@ -39,11 +39,9 @@ public class ArrayToObjectFactoryConverter : JsonConverterFactory
|
||||
public override void Write(Utf8JsonWriter writer, T? value, JsonSerializerOptions options)
|
||||
{
|
||||
if (value == null)
|
||||
JsonSerializer.Serialize(writer, null, options);
|
||||
JsonSerializer.Serialize(writer, new List<object>(), options);
|
||||
else
|
||||
{
|
||||
JsonSerializer.Serialize(writer, value, options);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,19 @@ public class EftEnumConverter<T> : JsonConverter<T>
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
|
||||
{
|
||||
JsonSerializer.Serialize(writer, value, _options);
|
||||
if (typeof(T).GetFields().Any(f => f.FieldType == typeof(string)))
|
||||
{
|
||||
JsonSerializer.Serialize(writer, value as string, _options);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (typeof(T).GetFields().Any(f => f.FieldType == typeof(int)))
|
||||
JsonSerializer.Serialize(writer, Convert.ToInt32(value), _options);
|
||||
else if (typeof(T).GetFields().Any(f => f.FieldType == typeof(byte)))
|
||||
JsonSerializer.Serialize(writer, Convert.ToByte(value), _options);
|
||||
else
|
||||
throw new Exception($"Could not convert enum {value.GetType()} with value {value}");
|
||||
}
|
||||
}
|
||||
|
||||
public override T ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
using Core.Models.Logging;
|
||||
using Core.Models.Utils;
|
||||
|
||||
namespace UnitTests.Mock;
|
||||
|
||||
public class MockLogger<T> : ISptLogger<T>
|
||||
{
|
||||
public void LogWithColor(
|
||||
string data,
|
||||
Exception? ex = null,
|
||||
LogTextColor? textColor = null,
|
||||
LogBackgroundColor? backgroundColor = null
|
||||
)
|
||||
{
|
||||
Console.WriteLine(data);
|
||||
}
|
||||
|
||||
public void Success(string data, Exception? ex = null)
|
||||
{
|
||||
Console.WriteLine(data);
|
||||
}
|
||||
|
||||
public void Error(string data, Exception? ex = null)
|
||||
{
|
||||
Console.WriteLine(data);
|
||||
}
|
||||
|
||||
public void Warning(string data, Exception? ex = null)
|
||||
{
|
||||
Console.WriteLine(data);
|
||||
}
|
||||
|
||||
public void Info(string data, Exception? ex = null)
|
||||
{
|
||||
Console.WriteLine(data);
|
||||
}
|
||||
|
||||
public void Debug(string data, Exception? ex = null)
|
||||
{
|
||||
Console.WriteLine(data);
|
||||
}
|
||||
|
||||
public void Critical(string data, Exception? ex = null)
|
||||
{
|
||||
Console.WriteLine(data);
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,30 @@
|
||||
using Core.Models.Spt.Server;
|
||||
using Core.Models.Spt.Templates;
|
||||
using Core.Utils;
|
||||
using UnitTests.Mock;
|
||||
|
||||
namespace UnitTests.Tests;
|
||||
|
||||
[TestClass]
|
||||
public class Test
|
||||
{
|
||||
private Templates _templates;
|
||||
|
||||
[TestInitialize]
|
||||
public void Setup()
|
||||
{
|
||||
|
||||
var importer = new ImporterUtil(new MockLogger<ImporterUtil>(), new FileUtil(), new JsonUtil());
|
||||
var loadTask = importer.LoadRecursiveAsync<Templates>("./TestAssets/");
|
||||
loadTask.Wait();
|
||||
_templates = loadTask.Result;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestMethod1()
|
||||
{
|
||||
var result = new JsonUtil().Serialize(_templates);
|
||||
Console.WriteLine(result);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -20,7 +20,9 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Tests\Mock\" />
|
||||
<Content Include="TestAssets\**">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user