Skip to content

Commit

Permalink
fix ByteArrayConverter null handling inconsistencies. (#56390)
Browse files Browse the repository at this point in the history
Fixes #49728.
  • Loading branch information
eiriktsarpalis authored Jul 27, 2021
1 parent eeb4f36 commit 59e7258
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,26 @@ namespace System.Text.Json.Serialization.Converters
{
internal sealed class ByteArrayConverter : JsonConverter<byte[]>
{
public override byte[] Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
public override byte[]? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
{
return null;
}

return reader.GetBytesFromBase64();
}

public override void Write(Utf8JsonWriter writer, byte[] value, JsonSerializerOptions options)
public override void Write(Utf8JsonWriter writer, byte[]? value, JsonSerializerOptions options)
{
writer.WriteBase64StringValue(value);
if (value == null)
{
writer.WriteNullValue();
}
else
{
writer.WriteBase64StringValue(value);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,22 @@ public static void ReadObjectArray()
[Fact]
public static void ReadNullByteArray()
{
string json = @"null";
byte[] arr = JsonSerializer.Deserialize<byte[]>(json);
byte[] arr = JsonSerializer.Deserialize<byte[]>("null");
Assert.Null(arr);

PocoWithByteArrayProperty poco = JsonSerializer.Deserialize<PocoWithByteArrayProperty>(@"{""Value"":null}");
Assert.Null(poco.Value);

byte[][] jaggedArr = JsonSerializer.Deserialize<byte[][]>(@"[null]");
Assert.Null(jaggedArr[0]);

Dictionary<string, byte[]> dict = JsonSerializer.Deserialize<Dictionary<string, byte[]>>(@"{""key"":null}");
Assert.Null(dict["key"]);
}

public class PocoWithByteArrayProperty
{
public byte[] Value { get; set; }
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,27 @@ public static void WriteEmptyByteArray()
Assert.Equal(@"""""", json);
}

[Fact]
public static void WriteByteArray()
[Theory]
[InlineData(null, "null")]
[InlineData(new byte[] { }, "\"\"")]
[InlineData(new byte[] { 1, 2 }, "\"AQI=\"")]
public static void WriteByteArray(byte[]? input, string expectedEncoding)
{
var input = new byte[] { 1, 2 };
// root-level serialization
string json = JsonSerializer.Serialize(input);
Assert.Equal($"\"{Convert.ToBase64String(input)}\"", json);
Assert.Equal(expectedEncoding, json);

// object property
json = JsonSerializer.Serialize(new { Property = input });
Assert.Equal(@$"{{""Property"":{expectedEncoding}}}", json);

// array element
json = JsonSerializer.Serialize(new[] { input });
Assert.Equal($"[{expectedEncoding}]", json);

// dictionary entry
json = JsonSerializer.Serialize(new Dictionary<string, byte[]> { ["key"] = input });
Assert.Equal(@$"{{""key"":{expectedEncoding}}}", json);
}

[Fact]
Expand Down

0 comments on commit 59e7258

Please sign in to comment.