more stuff
This commit is contained in:
@@ -308,8 +308,7 @@ public class BaseJsonSkills
|
||||
|
||||
public class Skills
|
||||
{
|
||||
[JsonConverter(typeof(DictionaryOfListOrTConverter))]
|
||||
public Dictionary<SkillTypes, ListOrT<Common>>? Common { get; set; }
|
||||
public DictionaryOrList<string, Common>? Common { get; set; }
|
||||
|
||||
[JsonConverter(typeof(ArrayToObjectFactoryConverter))]
|
||||
public Dictionary<string, Mastering>? Mastering { get; set; }
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text.Json.Serialization;
|
||||
using Core.Models.Eft.Health;
|
||||
using Core.Models.Enums;
|
||||
@@ -166,6 +167,7 @@ public class SkillRequirement : QteRequirement
|
||||
public RequirementType? Type { get; set; } = Models.Enums.Hideout.RequirementType.Skill;
|
||||
|
||||
[JsonPropertyName("skillName")]
|
||||
[JsonConverter(typeof(JsonStringEnumConverter))]
|
||||
public SkillTypes? SkillName { get; set; }
|
||||
|
||||
[JsonPropertyName("skillLevel")]
|
||||
@@ -254,4 +256,4 @@ public class BodyPartBuffRequirement : QteRequirement
|
||||
|
||||
[JsonPropertyName("excluded")]
|
||||
public bool? Excluded { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ public class ImporterUtil
|
||||
|
||||
private readonly JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions
|
||||
{
|
||||
UnmappedMemberHandling = JsonUnmappedMemberHandling.Disallow, Converters = { new ListOrTConverterFactory() }
|
||||
UnmappedMemberHandling = JsonUnmappedMemberHandling.Disallow, Converters = { new ListOrTConverterFactory(), new DictionaryOrListConverter() }
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user