Skip to content
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

Remove serializer todos and add comments #37170

Merged
merged 2 commits into from
Jun 2, 2020
Merged
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 @@ -26,7 +26,11 @@ public static partial class JsonSerializer
/// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
/// for <typeparamref name="TValue"/> or its serializable members.
/// </exception>
public static Task SerializeAsync<TValue>(Stream utf8Json, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default)
public static Task SerializeAsync<TValue>(
Stream utf8Json,
TValue value,
JsonSerializerOptions? options = null,
CancellationToken cancellationToken = default)
{
if (utf8Json == null)
throw new ArgumentNullException(nameof(utf8Json));
Expand All @@ -53,7 +57,12 @@ public static Task SerializeAsync<TValue>(Stream utf8Json, TValue value, JsonSer
/// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
/// for <paramref name="inputType"/> or its serializable members.
/// </exception>
public static Task SerializeAsync(Stream utf8Json, object? value, Type inputType, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default)
public static Task SerializeAsync(
Stream utf8Json,
object? value,
Type inputType,
JsonSerializerOptions? options = null,
CancellationToken cancellationToken = default)
{
if (utf8Json == null)
{
Expand All @@ -73,8 +82,21 @@ public static Task SerializeAsync(Stream utf8Json, object? value, Type inputType
return WriteAsyncCore<object>(utf8Json, value!, inputType, options, cancellationToken);
}

private static async Task WriteAsyncCore<TValue>(Stream utf8Json, TValue value, Type inputType, JsonSerializerOptions? options, CancellationToken cancellationToken)
private static async Task WriteAsyncCore<TValue>(
Stream utf8Json,
TValue value,
Type inputType,
JsonSerializerOptions? options,
CancellationToken cancellationToken)
{
// We flush the Stream when the buffer is >=90% of capacity.
// This threshold is a compromise between buffer utilization and minimizing cases where the buffer
// needs to be expanded\doubled because it is not large enough to write the current property or element.
// We check for flush after each object property and array element is written to the buffer.
// Once the buffer is expanded to contain the largest single element\property, a 90% thresold
// means the buffer may be expanded a maximum of 4 times: 1-(1\(2^4))==.9375.
const float FlushThreshold = .9f;

if (options == null)
{
options = JsonSerializerOptions.s_defaultOptions;
Expand All @@ -100,9 +122,7 @@ private static async Task WriteAsyncCore<TValue>(Stream utf8Json, TValue value,

do
{
// todo: determine best value here
// https://github.com/dotnet/runtime/issues/32356
state.FlushThreshold = (int)(bufferWriter.Capacity * .9);
state.FlushThreshold = (int)(bufferWriter.Capacity * FlushThreshold);

isFinalBlock = WriteCore(converterBase, writer, value, options, ref state);

Expand All @@ -111,8 +131,6 @@ private static async Task WriteAsyncCore<TValue>(Stream utf8Json, TValue value,
bufferWriter.Clear();
} while (!isFinalBlock);
}

// todo: verify that we do want to call FlushAsync here (or above). It seems like leaving it to the caller would be best.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any notes on this one?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was added during the original design\coding when we discussed a possible callback model (caller must call flush) instead of auto-flush. I don't believe we received any further feedback to add that capability.

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,12 @@ public JsonPropertyInfo GetPolymorphicJsonPropertyInfo()
/// </summary>
public JsonConverter InitializeReEntry(Type type, JsonSerializerOptions options, string? propertyName = null)
{
JsonClassInfo newClassInfo = options.GetOrAddClass(type);

// todo: check if type==newtype and skip below?
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Skipping the logic below would only have a minimal impact on perf, so it is not worth the check \ risk of doing that.

// https://github.com/dotnet/runtime/issues/32358
JsonClassInfo classInfo = options.GetOrAddClass(type);

// Set for exception handling calculation of JsonPath.
JsonPropertyNameAsString = propertyName;

PolymorphicJsonPropertyInfo = newClassInfo.PropertyInfoForClassInfo;
PolymorphicJsonPropertyInfo = classInfo.PropertyInfoForClassInfo;
return PolymorphicJsonPropertyInfo.ConverterBase;
}

Expand Down