mirror of
https://github.com/goatcorp/Dalamud.git
synced 2025-12-12 10:17:22 +01:00
Add tests for GameVersionConverter and fix edge case (#1726)
- Adds tests for GameVersionConverter - Refactors GameVersionConverter to reduce nesting - Fixes an edge case in GameVersion deserialization in which the JsonConstructor will be invoked even if no properties match - Adds a test for the GameVersion deserialization edge case
This commit is contained in:
parent
7fcd10ecd8
commit
d393fa64b6
4 changed files with 171 additions and 22 deletions
|
|
@ -109,26 +109,31 @@ public sealed class GameVersion : ICloneable, IComparable, IComparable<GameVersi
|
|||
/// <summary>
|
||||
/// Gets the year component.
|
||||
/// </summary>
|
||||
[JsonRequired]
|
||||
public int Year { get; } = -1;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the month component.
|
||||
/// </summary>
|
||||
[JsonRequired]
|
||||
public int Month { get; } = -1;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the day component.
|
||||
/// </summary>
|
||||
[JsonRequired]
|
||||
public int Day { get; } = -1;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the major version component.
|
||||
/// </summary>
|
||||
[JsonRequired]
|
||||
public int Major { get; } = -1;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the minor version component.
|
||||
/// </summary>
|
||||
[JsonRequired]
|
||||
public int Minor { get; } = -1;
|
||||
|
||||
public static implicit operator GameVersion(string ver)
|
||||
|
|
|
|||
|
|
@ -15,17 +15,16 @@ public sealed class GameVersionConverter : JsonConverter
|
|||
/// <param name="serializer">The calling serializer.</param>
|
||||
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
|
||||
{
|
||||
if (value == null)
|
||||
switch (value)
|
||||
{
|
||||
writer.WriteNull();
|
||||
}
|
||||
else if (value is GameVersion)
|
||||
{
|
||||
writer.WriteValue(value.ToString());
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new JsonSerializationException("Expected GameVersion object value");
|
||||
case null:
|
||||
writer.WriteNull();
|
||||
break;
|
||||
case GameVersion:
|
||||
writer.WriteValue(value.ToString());
|
||||
break;
|
||||
default:
|
||||
throw new JsonSerializationException("Expected GameVersion object value");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -43,24 +42,20 @@ public sealed class GameVersionConverter : JsonConverter
|
|||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
|
||||
if (reader.TokenType == JsonToken.String)
|
||||
{
|
||||
if (reader.TokenType == JsonToken.String)
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
return new GameVersion((string)reader.Value!);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new JsonSerializationException($"Error parsing GameVersion string: {reader.Value}", ex);
|
||||
}
|
||||
return new GameVersion((string)reader.Value!);
|
||||
}
|
||||
else
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new JsonSerializationException($"Unexpected token or value when parsing GameVersion. Token: {reader.TokenType}, Value: {reader.Value}");
|
||||
throw new JsonSerializationException($"Error parsing GameVersion string: {reader.Value}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
throw new JsonSerializationException($"Unexpected token or value when parsing GameVersion. Token: {reader.TokenType}, Value: {reader.Value}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue