Skip to content

Commit

Permalink
Add JSON null support for the built-in (ReadOnly)Memory converters. (#…
Browse files Browse the repository at this point in the history
…95275)

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

* Address feedback
  • Loading branch information
eiriktsarpalis authored Nov 28, 2023
1 parent 05bc701 commit fe79ce8
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 6 deletions.
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,
out Memory<T> value)
{
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(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options,
scoped ref ReadStack state,
out ReadOnlyMemory<T> value)
{
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;
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

0 comments on commit fe79ce8

Please sign in to comment.