Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Improved collection deserialization in System.Text.Json #38797

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ private static void HandleStartArray(
}
else
{
// If current property is already set (from a constructor, for example) leave as-is.
if (jsonPropertyInfo.GetValueAsObject(state.Current.ReturnValue) == null)
if (jsonPropertyInfo.HasSetter)
{
// Create the enumerable.
object value = ReadStackFrame.CreateEnumerableValue(ref reader, ref state, options);
Expand All @@ -82,6 +81,22 @@ private static void HandleStartArray(
}
}
}
else
{
// Ignore read only and fixed size collections
bool skip = jsonPropertyInfo.GetValueAsObject(state.Current.ReturnValue) switch
{
IList list => list.IsFixedSize || list.IsReadOnly,
IDictionary list => list.IsFixedSize || list.IsReadOnly,
_ => true
};

if (skip)
{
state.Push();
state.Current.Drain = true;
}
}
}
}

Expand Down Expand Up @@ -184,7 +199,7 @@ internal static void ApplyObjectToEnumerable(
else
{
IList list = (IList)state.Current.JsonPropertyInfo.GetValueAsObject(state.Current.ReturnValue);
if (list == null)
if (list == null || list.IsFixedSize)
{
state.Current.JsonPropertyInfo.SetValueAsObject(state.Current.ReturnValue, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ public static void ReadPrimitiveArray()
Assert.Equal(0, i.Length);
}

[ActiveIssue(38435)]
[Fact]
public static void ReadInitializedArrayTest()
{
Expand Down