more stuff

This commit is contained in:
Alex
2025-01-08 23:22:23 +00:00
parent cb305a4d54
commit 9be88e76ef
5 changed files with 70 additions and 4 deletions
+1 -2
View File
@@ -308,8 +308,7 @@ public class BaseJsonSkills
public class Skills public class Skills
{ {
[JsonConverter(typeof(DictionaryOfListOrTConverter))] public DictionaryOrList<string, Common>? Common { get; set; }
public Dictionary<SkillTypes, ListOrT<Common>>? Common { get; set; }
[JsonConverter(typeof(ArrayToObjectFactoryConverter))] [JsonConverter(typeof(ArrayToObjectFactoryConverter))]
public Dictionary<string, Mastering>? Mastering { get; set; } public Dictionary<string, Mastering>? Mastering { get; set; }
+2
View File
@@ -1,3 +1,4 @@
using System.Runtime.CompilerServices;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using Core.Models.Eft.Health; using Core.Models.Eft.Health;
using Core.Models.Enums; using Core.Models.Enums;
@@ -166,6 +167,7 @@ public class SkillRequirement : QteRequirement
public RequirementType? Type { get; set; } = Models.Enums.Hideout.RequirementType.Skill; public RequirementType? Type { get; set; } = Models.Enums.Hideout.RequirementType.Skill;
[JsonPropertyName("skillName")] [JsonPropertyName("skillName")]
[JsonConverter(typeof(JsonStringEnumConverter))]
public SkillTypes? SkillName { get; set; } public SkillTypes? SkillName { get; set; }
[JsonPropertyName("skillLevel")] [JsonPropertyName("skillLevel")]
+1 -1
View File
@@ -15,7 +15,7 @@ public class ImporterUtil
private readonly JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions private readonly JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions
{ {
UnmappedMemberHandling = JsonUnmappedMemberHandling.Disallow, Converters = { new ListOrTConverterFactory() } UnmappedMemberHandling = JsonUnmappedMemberHandling.Disallow, Converters = { new ListOrTConverterFactory(), new DictionaryOrListConverter() }
}; };
public ImporterUtil(FileUtil fileUtil) public ImporterUtil(FileUtil fileUtil)
@@ -0,0 +1,55 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Core.Utils.Json.Converters;
public class DictionaryOrListConverter: JsonConverterFactory
{
public override bool CanConvert(Type typeToConvert)
{
return typeToConvert.IsGenericType && typeToConvert.GetGenericTypeDefinition() == typeof(DictionaryOrList<,>);
}
public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options)
{
return (JsonConverter)Activator.CreateInstance(typeof(DictionaryOrListConverter<,>).MakeGenericType(typeToConvert.GenericTypeArguments[0], typeToConvert.GenericTypeArguments[1]));
}
}
public class DictionaryOrListConverter<K,V> : JsonConverter<DictionaryOrList<K,V>?>
{
public override DictionaryOrList<K,V>? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
switch (reader.TokenType)
{
case JsonTokenType.StartArray:
using (var jsonDocument = JsonDocument.ParseValue(ref reader))
{
var jsonText = jsonDocument.RootElement.GetRawText();
var list = JsonSerializer.Deserialize<List<V>>(jsonText, options);
return new DictionaryOrList<K,V>(null, list);
}
case JsonTokenType.StartObject:
using (var jsonDocument = JsonDocument.ParseValue(ref reader))
{
var jsonText = jsonDocument.RootElement.GetRawText();
var dictionary = JsonSerializer.Deserialize<Dictionary<K,V>>(jsonText, options);
return new DictionaryOrList<K,V>(dictionary, null);
}
default:
throw new Exception($"Unable to translate object type {reader.TokenType} to ListOrT<T>.");
}
}
public override void Write(Utf8JsonWriter writer, DictionaryOrList<K,V> value, JsonSerializerOptions options)
{
if (value.IsList)
{
JsonSerializer.Serialize(writer, value.List, options);
}
else
{
JsonSerializer.Serialize(writer, value.Dictionary, options);
}
}
}
+10
View File
@@ -0,0 +1,10 @@
namespace Core.Utils.Json;
public class DictionaryOrList<K, V>(Dictionary<K, V>? dictionary, List<V>? list)
{
public Dictionary<K, V>? Dictionary { get; } = dictionary;
public List<V>? List { get; } = list;
public bool IsList => List != null;
public bool IsDictionary => Dictionary != null;
}