Skip to content

Commit f710bd0

Browse files
committed
Revert ByteArrayJsonConverter Impl
1 parent a66eac6 commit f710bd0

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

src/JSInterop/Microsoft.JSInterop/src/Infrastructure/ByteArrayJsonConverter.cs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,39 @@ public ByteArrayJsonConverter(JSRuntime jSRuntime)
2929
throw new JsonException("ByteArraysToBeRevived is empty.");
3030
}
3131

32-
var byteArrayRef = JsonSerializer.Deserialize<ByteArrayRef>(ref reader, options);
32+
int? byteArrayRef = null;
3333

34-
if (byteArrayRef.Id is null)
34+
while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
35+
{
36+
if (reader.TokenType == JsonTokenType.PropertyName)
37+
{
38+
if (byteArrayRef is null && reader.ValueTextEquals(ByteArrayRefKey.EncodedUtf8Bytes))
39+
{
40+
reader.Read();
41+
byteArrayRef = reader.GetInt32();
42+
}
43+
else
44+
{
45+
throw new JsonException($"Unexpected JSON property {reader.GetString()}.");
46+
}
47+
}
48+
else
49+
{
50+
throw new JsonException($"Unexpected JSON Token {reader.TokenType}.");
51+
}
52+
}
53+
54+
if (byteArrayRef is null)
3555
{
3656
throw new JsonException($"Required property {ByteArrayRefKey} not found.");
3757
}
3858

39-
if (byteArrayRef.Id >= JSRuntime.ByteArraysToBeRevived.Count || byteArrayRef.Id < 0)
59+
if (byteArrayRef >= JSRuntime.ByteArraysToBeRevived.Count || byteArrayRef < 0)
4060
{
41-
throw new JsonException($"Byte array {byteArrayRef.Id} not found.");
61+
throw new JsonException($"Byte array {byteArrayRef} not found.");
4262
}
4363

44-
var byteArray = JSRuntime.ByteArraysToBeRevived.Buffer[byteArrayRef.Id.Value];
64+
var byteArray = JSRuntime.ByteArraysToBeRevived.Buffer[byteArrayRef.Value];
4565
return byteArray;
4666
}
4767

src/JSInterop/Microsoft.JSInterop/test/Infrastructure/ByteArrayJsonConverterTest.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void Read_Throws_IfJsonContainsUnknownContent()
5151

5252
// Act & Assert
5353
var ex = Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<byte[]>(json, JsonSerializerOptions));
54-
Assert.Equal("Required property __byte[] not found.", ex.Message);
54+
Assert.Equal("Unexpected JSON property foo.", ex.Message);
5555
}
5656

5757
[Fact]
@@ -84,18 +84,19 @@ public void Read_ReadsJson()
8484
}
8585

8686
[Fact]
87-
public void Read_IfByteArraysIdAppearsMultipleTimesUseLastProperty()
87+
public void Read_ByteArraysIdAppearsMultipleTimesThrows()
8888
{
8989
// Arrange
9090
var byteArray = new byte[] { 1, 5, 7 };
9191
JSRuntime.ByteArraysToBeRevived.Append(byteArray);
9292

9393
var json = $"{{\"__byte[]\":9120,\"__byte[]\":0}}";
9494

95-
var deserialized = JsonSerializer.Deserialize<byte[]>(json, JsonSerializerOptions)!;
95+
// Act
96+
var ex = Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<byte[]>(json, JsonSerializerOptions));
9697

97-
// Act & Assert
98-
Assert.Equal(byteArray, deserialized);
98+
// Assert
99+
Assert.Equal("Unexpected JSON property __byte[].", ex.Message);
99100
}
100101

101102
[Fact]

0 commit comments

Comments
 (0)