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

Add JSON null support for the built-in (ReadOnly)Memory converters. #95275

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -2,12 +2,30 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace System.Text.Json.Serialization.Converters
{
internal sealed class MemoryConverter<T> : JsonCollectionConverter<Memory<T>, T>
{
internal override bool CanHaveMetadata => false;
public override bool HandleNull => true;

internal override bool OnTryRead(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options,
scoped ref ReadStack state,
[MaybeNullWhen(false)] out Memory<T> value)
eiriktsarpalis marked this conversation as resolved.
Show resolved Hide resolved
{
if (reader.TokenType is JsonTokenType.Null)
{
value = default;
return true;
}

return base.OnTryRead(ref reader, typeToConvert, options, ref state, out value);
}

protected override void Add(in T value, ref ReadStack state)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,30 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace System.Text.Json.Serialization.Converters
{
internal sealed class ReadOnlyMemoryConverter<T> : JsonCollectionConverter<ReadOnlyMemory<T>, T>
{
internal override bool CanHaveMetadata => false;
public override bool HandleNull => true;

internal override bool OnTryRead(
jozkee marked this conversation as resolved.
Show resolved Hide resolved
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options,
scoped ref ReadStack state,
[MaybeNullWhen(false)] out ReadOnlyMemory<T> value)
eiriktsarpalis marked this conversation as resolved.
Show resolved Hide resolved
{
if (reader.TokenType is JsonTokenType.Null)
{
value = default;
return true;
}

return base.OnTryRead(ref reader, typeToConvert, options, ref state, out value);
}

protected override void Add(in T value, ref ReadStack state)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ internal sealed class JsonMetadataServicesConverter<T> : JsonResumableConverter<

internal override Type? KeyType => Converter.KeyType;
internal override Type? ElementType => Converter.ElementType;
public override bool HandleNull { get; }

internal override bool ConstructorIsParameterized => Converter.ConstructorIsParameterized;
internal override bool SupportsCreateObjectDelegate => Converter.SupportsCreateObjectDelegate;
Expand All @@ -29,8 +30,16 @@ internal sealed class JsonMetadataServicesConverter<T> : JsonResumableConverter<

public JsonMetadataServicesConverter(JsonConverter<T> converter)
{
ConverterStrategy = converter.ConverterStrategy;
Converter = converter;
ConverterStrategy = converter.ConverterStrategy;
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 change adds some wiring that was being dropped so far.

Copy link
Member

Choose a reason for hiding this comment

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

I was about to ask :) Does that mean there's another bug that was lurking and this was fixing?

Copy link
Member Author

Choose a reason for hiding this comment

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

Correct, it was surfaced with the newly added tests.

Copy link
Member Author

Choose a reason for hiding this comment

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

For context this is a converter wrapper type that is being used by the source generator APIs. The logic I just added was copied from a similar wrapper type being used internally:

_sourceConverter = sourceConverter;
IsInternalConverter = sourceConverter.IsInternalConverter;
IsInternalConverterForNumberType = sourceConverter.IsInternalConverterForNumberType;
ConverterStrategy = sourceConverter.ConverterStrategy;
CanBePolymorphic = sourceConverter.CanBePolymorphic;
// Ensure HandleNull values reflect the exact configuration of the source converter
HandleNullOnRead = sourceConverter.HandleNullOnRead;
HandleNullOnWrite = sourceConverter.HandleNullOnWrite;
HandleNull = sourceConverter.HandleNullOnWrite;

IsInternalConverter = converter.IsInternalConverter;
IsInternalConverterForNumberType = converter.IsInternalConverterForNumberType;
CanBePolymorphic = converter.CanBePolymorphic;

// Ensure HandleNull values reflect the exact configuration of the source converter
HandleNullOnRead = converter.HandleNullOnRead;
HandleNullOnWrite = converter.HandleNullOnWrite;
HandleNull = converter.HandleNullOnWrite;
}

internal override bool OnTryRead(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options, scoped ref ReadStack state, out T? value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ namespace System.Text.Json.Serialization.Converters
{
internal sealed class MemoryByteConverter : JsonConverter<Memory<byte>>
{
public override bool HandleNull => true;

public override Memory<byte> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return reader.GetBytesFromBase64();
return reader.TokenType is JsonTokenType.Null ? default : reader.GetBytesFromBase64();
}

public override void Write(Utf8JsonWriter writer, Memory<byte> value, JsonSerializerOptions options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ namespace System.Text.Json.Serialization.Converters
{
internal sealed class ReadOnlyMemoryByteConverter : JsonConverter<ReadOnlyMemory<byte>>
{
public override bool HandleNull => true;

public override ReadOnlyMemory<byte> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return reader.GetBytesFromBase64();
return reader.TokenType is JsonTokenType.Null ? default : reader.GetBytesFromBase64();
}

public override void Write(Utf8JsonWriter writer, ReadOnlyMemory<byte> value, JsonSerializerOptions options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace System.Text.Json.Serialization
/// <typeparam name="T"></typeparam>
internal abstract class JsonResumableConverter<T> : JsonConverter<T>
{
public override bool HandleNull => false;

public sealed override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (options is null)
Expand Down Expand Up @@ -51,7 +53,5 @@ public sealed override void Write(Utf8JsonWriter writer, T value, JsonSerializer
throw;
}
}

public sealed override bool HandleNull => false;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json.Tests;
using System.Threading.Tasks;
using Xunit;

Expand Down Expand Up @@ -104,6 +103,22 @@ public async Task DeserializeMemoryByteAsync()
AssertExtensions.SequenceEqual(s_testData, readOnlyMemory.Span);
}

[Fact]
public async Task DeserializeNullAsMemory()
{
ReadOnlyMemory<int> readOnlyMemOfInt = await Serializer.DeserializeWrapper<ReadOnlyMemory<int>>("null");
Assert.True(readOnlyMemOfInt.IsEmpty);

Memory<int> memOfInt = await Serializer.DeserializeWrapper<Memory<int>>("null");
Assert.True(memOfInt.IsEmpty);

ReadOnlyMemory<byte> readOnlyMemOfByte = await Serializer.DeserializeWrapper<ReadOnlyMemory<byte>>("null");
Assert.True(readOnlyMemOfByte.IsEmpty);

Memory<byte> memOfByte = await Serializer.DeserializeWrapper<Memory<byte>>("null");
Assert.True(memOfByte.IsEmpty);
}

[Fact]
public async Task SerializeMemoryByteClassAsync()
{
Expand Down
Loading