Converter fixes
- Various nullability warnings removed - Made CanConvert on a few of them a bit more strict
This commit is contained in:
+3
-4
@@ -7,7 +7,7 @@ public class ArrayToObjectFactoryConverter : JsonConverterFactory
|
||||
{
|
||||
public override bool CanConvert(Type typeToConvert)
|
||||
{
|
||||
return true;
|
||||
return typeToConvert.IsClass;
|
||||
}
|
||||
|
||||
public override JsonConverter? CreateConverter(
|
||||
@@ -15,10 +15,9 @@ public class ArrayToObjectFactoryConverter : JsonConverterFactory
|
||||
JsonSerializerOptions options
|
||||
)
|
||||
{
|
||||
return (JsonConverter)
|
||||
Activator.CreateInstance(
|
||||
return Activator.CreateInstance(
|
||||
typeof(ArrayToObjectConverter<>).MakeGenericType(typeToConvert)
|
||||
);
|
||||
) as JsonConverter;
|
||||
}
|
||||
|
||||
private class ArrayToObjectConverter<T> : JsonConverter<T?>
|
||||
|
||||
+6
@@ -37,6 +37,12 @@ public class BaseInteractionRequestDataConverter : JsonConverter<BaseInteraction
|
||||
// Get the underlying 'type' of action the client is requesting we do
|
||||
var action = jsonDocument.RootElement.GetProperty("Action").GetString();
|
||||
|
||||
// Handle nullability here in case action's GetString is null
|
||||
if (action is null)
|
||||
{
|
||||
action = string.Empty;
|
||||
}
|
||||
|
||||
return ConvertToCorrectType(action, jsonDocument.RootElement, jsonText, options);
|
||||
}
|
||||
|
||||
|
||||
+2
-3
@@ -19,13 +19,12 @@ public class DictionaryOfListOrTConverter : JsonConverterFactory
|
||||
JsonSerializerOptions options
|
||||
)
|
||||
{
|
||||
return (JsonConverter)
|
||||
Activator.CreateInstance(
|
||||
return Activator.CreateInstance(
|
||||
typeof(DictionaryOfListOrTConverter<,>).MakeGenericType(
|
||||
typeToConvert.GenericTypeArguments[0],
|
||||
typeToConvert.GenericTypeArguments[1].GenericTypeArguments[0]
|
||||
)
|
||||
);
|
||||
) as JsonConverter;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,13 +16,12 @@ public class DictionaryOrListConverter : JsonConverterFactory
|
||||
JsonSerializerOptions options
|
||||
)
|
||||
{
|
||||
return (JsonConverter)
|
||||
Activator.CreateInstance(
|
||||
return Activator.CreateInstance(
|
||||
typeof(DictionaryOrListConverter<,>).MakeGenericType(
|
||||
typeToConvert.GenericTypeArguments[0],
|
||||
typeToConvert.GenericTypeArguments[1]
|
||||
)
|
||||
);
|
||||
) as JsonConverter;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,18 +16,13 @@ public class EftEnumConverterFactory : JsonConverterFactory
|
||||
JsonSerializerOptions options
|
||||
)
|
||||
{
|
||||
return (JsonConverter)
|
||||
Activator.CreateInstance(typeof(EftEnumConverter<>).MakeGenericType(typeToConvert));
|
||||
return Activator.CreateInstance(typeof(EftEnumConverter<>).MakeGenericType(typeToConvert))
|
||||
as JsonConverter;
|
||||
}
|
||||
}
|
||||
|
||||
public class EftEnumConverter<T> : JsonConverter<T>
|
||||
{
|
||||
private static readonly JsonSerializerOptions _options = new()
|
||||
{
|
||||
Converters = { new JsonStringEnumConverter() },
|
||||
};
|
||||
|
||||
public override T? Read(
|
||||
ref Utf8JsonReader reader,
|
||||
Type typeToConvert,
|
||||
@@ -56,17 +51,17 @@ public class EftEnumConverter<T> : JsonConverter<T>
|
||||
{
|
||||
if (typeof(T).GetFields().Any(f => f.FieldType == typeof(string)))
|
||||
{
|
||||
JsonSerializer.Serialize(writer, value as string, _options);
|
||||
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);
|
||||
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);
|
||||
JsonSerializer.Serialize(writer, Convert.ToByte(value), options);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -17,26 +17,27 @@ public class EftListEnumConverterFactory : JsonConverterFactory
|
||||
JsonSerializerOptions options
|
||||
)
|
||||
{
|
||||
return (JsonConverter)
|
||||
Activator.CreateInstance(
|
||||
return Activator.CreateInstance(
|
||||
typeof(EftListEnumConverter<>).MakeGenericType(
|
||||
typeToConvert.GenericTypeArguments[0]
|
||||
)
|
||||
);
|
||||
) as JsonConverter;
|
||||
}
|
||||
}
|
||||
|
||||
public class EftListEnumConverter<T> : JsonConverter<List<T>>
|
||||
{
|
||||
// We have to use these options here, because down below if we use the options passed we create a stack overflow
|
||||
// Due to the converter trying to use itself
|
||||
private static readonly JsonSerializerOptions _options = new()
|
||||
{
|
||||
Converters = { new JsonStringEnumConverter() },
|
||||
Converters = { new JsonStringEnumConverter(), new EftEnumConverterFactory() },
|
||||
};
|
||||
|
||||
public override List<T>? Read(
|
||||
ref Utf8JsonReader reader,
|
||||
Type typeToConvert,
|
||||
JsonSerializerOptions options
|
||||
JsonSerializerOptions _
|
||||
)
|
||||
{
|
||||
if (reader.TokenType == JsonTokenType.StartArray)
|
||||
@@ -47,7 +48,7 @@ public class EftListEnumConverter<T> : JsonConverter<List<T>>
|
||||
throw new JsonException();
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, List<T> value, JsonSerializerOptions options)
|
||||
public override void Write(Utf8JsonWriter writer, List<T> value, JsonSerializerOptions _)
|
||||
{
|
||||
writer.WriteStartArray();
|
||||
foreach (var x1 in value)
|
||||
|
||||
@@ -16,10 +16,9 @@ public class EnumerableConverterFactory : JsonConverterFactory
|
||||
JsonSerializerOptions options
|
||||
)
|
||||
{
|
||||
return (JsonConverter)
|
||||
Activator.CreateInstance(
|
||||
return Activator.CreateInstance(
|
||||
typeof(EnumerableConverter<>).MakeGenericType(typeToConvert.GenericTypeArguments[0])
|
||||
);
|
||||
) as JsonConverter;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,10 +16,9 @@ public class ListOrTConverterFactory : JsonConverterFactory
|
||||
JsonSerializerOptions options
|
||||
)
|
||||
{
|
||||
return (JsonConverter)
|
||||
Activator.CreateInstance(
|
||||
return Activator.CreateInstance(
|
||||
typeof(ListOrTConverter<>).MakeGenericType(typeToConvert.GenericTypeArguments[0])
|
||||
);
|
||||
) as JsonConverter;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+12
-5
@@ -10,7 +10,15 @@ public class StringToNumberFactoryConverter : JsonConverterFactory
|
||||
{
|
||||
public override bool CanConvert(Type typeToConvert)
|
||||
{
|
||||
return true;
|
||||
var type = Nullable.GetUnderlyingType(typeToConvert) ?? typeToConvert;
|
||||
|
||||
return type == typeof(byte)
|
||||
|| type == typeof(short)
|
||||
|| type == typeof(int)
|
||||
|| type == typeof(long)
|
||||
|| type == typeof(float)
|
||||
|| type == typeof(double)
|
||||
|| type == typeof(decimal);
|
||||
}
|
||||
|
||||
public override JsonConverter? CreateConverter(
|
||||
@@ -18,10 +26,9 @@ public class StringToNumberFactoryConverter : JsonConverterFactory
|
||||
JsonSerializerOptions options
|
||||
)
|
||||
{
|
||||
return (JsonConverter)
|
||||
Activator.CreateInstance(
|
||||
return Activator.CreateInstance(
|
||||
typeof(StringToNumberConverter<>).MakeGenericType(typeToConvert)
|
||||
);
|
||||
) as JsonConverter;
|
||||
}
|
||||
|
||||
private class StringToNumberConverter<T> : JsonConverter<T>
|
||||
@@ -66,7 +73,7 @@ public class StringToNumberFactoryConverter : JsonConverterFactory
|
||||
_stringParseMethod.Invoke(null, [value, CultureInfo.InvariantCulture]);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
catch (Exception)
|
||||
{
|
||||
Debug.WriteLine(
|
||||
$"Failed to parse '{value}' into {typeToConvert.Name}, returning null."
|
||||
|
||||
-59
@@ -1,59 +0,0 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace SPTarkov.Server.Core.Utils.Json.Converters;
|
||||
|
||||
public class StringToObjectFactoryConverter : JsonConverterFactory
|
||||
{
|
||||
public override bool CanConvert(Type typeToConvert)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public override JsonConverter? CreateConverter(
|
||||
Type typeToConvert,
|
||||
JsonSerializerOptions options
|
||||
)
|
||||
{
|
||||
return (JsonConverter)
|
||||
Activator.CreateInstance(
|
||||
typeof(StringToObjectConverter<>).MakeGenericType(typeToConvert)
|
||||
);
|
||||
}
|
||||
|
||||
public class StringToObjectConverter<T> : JsonConverter<T>
|
||||
{
|
||||
public override T? Read(
|
||||
ref Utf8JsonReader reader,
|
||||
Type typeToConvert,
|
||||
JsonSerializerOptions options
|
||||
)
|
||||
{
|
||||
switch (reader.TokenType)
|
||||
{
|
||||
case JsonTokenType.String:
|
||||
// start array
|
||||
reader.Read();
|
||||
return default;
|
||||
case JsonTokenType.StartObject:
|
||||
using (var jsonDocument = JsonDocument.ParseValue(ref reader))
|
||||
{
|
||||
var jsonText = jsonDocument.RootElement.GetRawText();
|
||||
return JsonSerializer.Deserialize<T>(jsonText, options);
|
||||
}
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, T? value, JsonSerializerOptions options)
|
||||
{
|
||||
if (EqualityComparer<T>.Default.Equals(value, default))
|
||||
{
|
||||
value = default;
|
||||
}
|
||||
|
||||
JsonSerializer.Serialize(writer, value, options);
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace SPTarkov.Server.Core.Utils.Json.Converters;
|
||||
namespace SPTarkov.Server.Core.Utils.Json;
|
||||
|
||||
public interface IJsonConverterRegistrator
|
||||
{
|
||||
+3
-4
@@ -1,9 +1,8 @@
|
||||
using System.Reflection;
|
||||
using System.Text.Json.Serialization;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Text.Json.Serialization;
|
||||
using SPTarkov.DI.Annotations;
|
||||
using SPTarkov.Server.Core.Utils.Json.Converters;
|
||||
|
||||
namespace SPTarkov.Server.Core.Utils.Json.Converters;
|
||||
namespace SPTarkov.Server.Core.Utils.Json;
|
||||
|
||||
[Injectable]
|
||||
public class SptJsonConverterRegistrator : IJsonConverterRegistrator
|
||||
@@ -2,7 +2,7 @@ using System.Text.Encodings.Web;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using SPTarkov.DI.Annotations;
|
||||
using SPTarkov.Server.Core.Utils.Json.Converters;
|
||||
using SPTarkov.Server.Core.Utils.Json;
|
||||
|
||||
namespace SPTarkov.Server.Core.Utils;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user