Skip to content

Commit

Permalink
Avoid skips instead of reads
Browse files Browse the repository at this point in the history
Break out common exception method
  • Loading branch information
jzebedee committed Oct 8, 2019
1 parent c8f3c93 commit 7e57926
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions src/Chronicler/Converters/Internal/CK2PlayerCharacterConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ internal class CK2PlayerCharacterConverter : JsonConverter<CK2PlayerCharacter>

public override CK2PlayerCharacter Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
static void ThrowMissingTokenException() => throw new JsonException("expected token to parse");

if (PlayerID == null)
throw new ArgumentNullException(nameof(PlayerID));

Expand All @@ -19,9 +21,7 @@ public override CK2PlayerCharacter Read(ref Utf8JsonReader reader, Type typeToCo
case JsonTokenType.None:
case JsonTokenType.PropertyName:
if (!reader.Read())
{
throw new JsonException("expected token to parse");
}
ThrowMissingTokenException();
break;
}

Expand All @@ -31,23 +31,17 @@ public override CK2PlayerCharacter Read(ref Utf8JsonReader reader, Type typeToCo
do
{
if (!reader.Read())
{
throw new JsonException("expected token to parse");
}
ThrowMissingTokenException();
else if (result == null)
{
switch (reader.TokenType)
{
case JsonTokenType.PropertyName when reader.ValueTextEquals(pid):
if (!reader.Read())
{
throw new JsonException("expected token to parse");
}
ThrowMissingTokenException();

result = JsonSerializer.Deserialize<CK2PlayerCharacter>(ref reader); /* exclude options or we loop */
break;
default:
reader.TrySkip();
break;
}
}
}
Expand Down

0 comments on commit 7e57926

Please sign in to comment.