Skip to content

prevent the encounter of endarray to incorrectly throw an exception #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 25, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Magic.IndexedDb/Models/MagicContractResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ internal class MagicContractResolver<T> : JsonConverter<T>
{
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
// ✅ Return default(T) if null is encountered
if (reader.TokenType == JsonTokenType.Null)
return default; // ✅ Return default(T) if null is encountered
{
return default;
}

// ✅ Handle primitive types before assuming it's complex
if (PropertyMappingCache.IsSimpleType(typeToConvert))
Expand Down Expand Up @@ -54,6 +57,12 @@ internal class MagicContractResolver<T> : JsonConverter<T>
return (T?)ReadComplexObject(ref reader, typeToConvert, options);
}

// ✅ Return default(T) if EndArray is encountered
if (reader.TokenType == JsonTokenType.EndArray)
{
return default;
}

throw new JsonException($"Unexpected JSON token: {reader.TokenType} when deserializing {typeToConvert.Name}.");
}

Expand Down