Skip to content

Commit

Permalink
backport opportunistic bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
eiriktsarpalis committed Jun 18, 2021
1 parent 5221db9 commit a858d3b
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,9 @@ internal sealed override bool OnTryWrite(

if (!jsonPropertyInfo.GetMemberAndWriteJson(objectValue!, ref state, writer))
{
Debug.Assert(jsonPropertyInfo.ConverterBase.ConverterStrategy != ConverterStrategy.Value);
Debug.Assert(jsonPropertyInfo.ConverterBase.ConverterStrategy != ConverterStrategy.Value ||
jsonPropertyInfo.ConverterBase.TypeToConvert == JsonTypeInfo.ObjectType);

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ internal bool TryWriteDataExtensionProperty(Utf8JsonWriter writer, T value, Json

// Ignore the naming policy for extension data.
state.Current.IgnoreDictionaryKeyPolicy = true;
state.Current.DeclaredJsonPropertyInfo = state.Current.JsonTypeInfo.ElementTypeInfo!.PropertyInfoForTypeInfo;

success = dictionaryConverter.OnWriteResume(writer, value, options, ref state);
if (success)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,6 @@ private static string WriteUsingMetadata<TValue>(in TValue value, JsonTypeInfo?
throw new ArgumentNullException(nameof(jsonTypeInfo));
}

WriteStack state = default;
state.Initialize(jsonTypeInfo, supportContinuation: false);

JsonSerializerOptions options = jsonTypeInfo.Options;

using (var output = new PooledByteBufferWriter(options.DefaultBufferSize))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,7 @@ static void AppendStackFrame(StringBuilder sb, in ReadStackFrame frame)

if (frame.JsonTypeInfo != null && frame.IsProcessingEnumerable())
{
IEnumerable? enumerable = (IEnumerable?)frame.ReturnValue;
if (enumerable == null)
if (frame.ReturnValue is not IEnumerable enumerable)
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,10 @@ public void DisposePendingDisposablesOnException()
DisposeFrame(Current.CollectionEnumerator, ref exception);

int stackSize = Math.Max(_count, _continuationCount);
if (stackSize > 1)
for (int i = 0; i < stackSize - 1; i++)
{
for (int i = 0; i < stackSize - 1; i++)
{
Debug.Assert(_previous[i].AsyncEnumerator is null);
DisposeFrame(_previous[i].CollectionEnumerator, ref exception);
}
Debug.Assert(_previous[i].AsyncEnumerator is null);
DisposeFrame(_previous[i].CollectionEnumerator, ref exception);
}

if (exception is not null)
Expand Down Expand Up @@ -294,12 +291,9 @@ public async ValueTask DisposePendingDisposablesOnExceptionAsync()
exception = await DisposeFrame(Current.CollectionEnumerator, Current.AsyncEnumerator, exception).ConfigureAwait(false);

int stackSize = Math.Max(_count, _continuationCount);
if (stackSize > 1)
for (int i = 0; i < stackSize - 1; i++)
{
for (int i = 0; i < stackSize - 1; i++)
{
exception = await DisposeFrame(_previous[i].CollectionEnumerator, _previous[i].AsyncEnumerator, exception).ConfigureAwait(false);
}
exception = await DisposeFrame(_previous[i].CollectionEnumerator, _previous[i].AsyncEnumerator, exception).ConfigureAwait(false);
}

if (exception is not null)
Expand Down Expand Up @@ -353,7 +347,7 @@ public string PropertyPath()

return sb.ToString();

void AppendStackFrame(StringBuilder sb, in WriteStackFrame frame)
static void AppendStackFrame(StringBuilder sb, in WriteStackFrame frame)
{
// Append the property name.
string? propertyName = frame.DeclaredJsonPropertyInfo?.MemberInfo?.Name;
Expand All @@ -366,7 +360,7 @@ void AppendStackFrame(StringBuilder sb, in WriteStackFrame frame)
AppendPropertyName(sb, propertyName);
}

void AppendPropertyName(StringBuilder sb, string? propertyName)
static void AppendPropertyName(StringBuilder sb, string? propertyName)
{
if (propertyName != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace System.Text.Json.Serialization.Tests
public class ReferenceHandlerTests_IgnoreCycles
{
private static readonly JsonSerializerOptions s_optionsIgnoreCycles =
new JsonSerializerOptions { ReferenceHandler = ReferenceHandler.IgnoreCycles };
new JsonSerializerOptions { ReferenceHandler = ReferenceHandler.IgnoreCycles, DefaultBufferSize = 1 };

[Fact]
public async Task IgnoreCycles_OnObject()
Expand Down Expand Up @@ -401,6 +401,25 @@ public async void IgnoreCycles_BoxedValueShouldNotBeIgnored()
await Test_Serialize_And_SerializeAsync_Contains(root, expectedSubstring: @"""DayOfBirth"":15", expectedTimes: 2, s_optionsIgnoreCycles);
}

[Fact]
public async Task CycleDetectionStatePersistsAcrossContinuations()
{
string expectedValueJson = @"{""LargePropertyName"":""A large-ish string to force continuations"",""Nested"":null}";
var recVal = new RecursiveValue { LargePropertyName = "A large-ish string to force continuations" };
recVal.Nested = recVal;

var value = new List<RecursiveValue> { recVal, recVal };
string expectedJson = $"[{expectedValueJson},{expectedValueJson}]";

await Test_Serialize_And_SerializeAsync(value, expectedJson, s_optionsIgnoreCycles);
}

public class RecursiveValue
{
public string LargePropertyName { get; set; }
public RecursiveValue? Nested { get; set; }
}

private async Task Test_Serialize_And_SerializeAsync<T>(T obj, string expected, JsonSerializerOptions options)
{
string json;
Expand Down

0 comments on commit a858d3b

Please sign in to comment.