parent
950b3873b1
commit
39c052b8bc
|
|
@ -19,7 +19,7 @@ namespace Microsoft.JSInterop
|
||||||
{
|
{
|
||||||
if (reader.TokenType == JsonTokenType.PropertyName)
|
if (reader.TokenType == JsonTokenType.PropertyName)
|
||||||
{
|
{
|
||||||
if (reader.ValueTextEquals(DotNetObjectRefKey.EncodedUtf8Bytes))
|
if (dotNetObjectId == 0 && reader.ValueTextEquals(DotNetObjectRefKey.EncodedUtf8Bytes))
|
||||||
{
|
{
|
||||||
reader.Read();
|
reader.Read();
|
||||||
dotNetObjectId = reader.GetInt64();
|
dotNetObjectId = reader.GetInt64();
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,36 @@ namespace Microsoft.JSInterop.Tests
|
||||||
Assert.Equal("Unexcepted JSON property foo.", ex.Message);
|
Assert.Equal("Unexcepted JSON property foo.", ex.Message);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public Task Read_Throws_IfJsonIsIncomplete() => WithJSRuntime(_ =>
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var input = new TestModel();
|
||||||
|
var dotNetObjectRef = DotNetObjectRef.Create(input);
|
||||||
|
var objectId = dotNetObjectRef.ObjectId;
|
||||||
|
|
||||||
|
var json = $"{{\"__dotNetObject\":{objectId}";
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
var ex = Record.Exception(() => JsonSerializer.Deserialize<DotNetObjectRef<TestModel>>(json));
|
||||||
|
Assert.IsAssignableFrom<JsonException>(ex);
|
||||||
|
});
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public Task Read_Throws_IfDotNetObjectIdAppearsMultipleTimes() => WithJSRuntime(_ =>
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var input = new TestModel();
|
||||||
|
var dotNetObjectRef = DotNetObjectRef.Create(input);
|
||||||
|
var objectId = dotNetObjectRef.ObjectId;
|
||||||
|
|
||||||
|
var json = $"{{\"__dotNetObject\":{objectId},\"__dotNetObject\":{objectId}}}";
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
var ex = Record.Exception(() => JsonSerializer.Deserialize<DotNetObjectRef<TestModel>>(json));
|
||||||
|
Assert.IsAssignableFrom<JsonException>(ex);
|
||||||
|
});
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public Task Read_ReadsJson() => WithJSRuntime(_ =>
|
public Task Read_ReadsJson() => WithJSRuntime(_ =>
|
||||||
{
|
{
|
||||||
|
|
@ -54,26 +84,25 @@ namespace Microsoft.JSInterop.Tests
|
||||||
Assert.Equal(objectId, deserialized.ObjectId);
|
Assert.Equal(objectId, deserialized.ObjectId);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public Task Read_ReturnsTheCorrectInstance() => WithJSRuntime(_ =>
|
public Task Read_ReturnsTheCorrectInstance() => WithJSRuntime(_ =>
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
// Track a few instances and verify that the deserialized value returns the corect value.
|
// Track a few instances and verify that the deserialized value returns the correct value.
|
||||||
DotNetObjectRef.Create(new TestModel());
|
var instance1 = new TestModel();
|
||||||
DotNetObjectRef.Create(new TestModel());
|
var instance2 = new TestModel();
|
||||||
|
var ref1 = DotNetObjectRef.Create(instance1);
|
||||||
|
var ref2 = DotNetObjectRef.Create(instance2);
|
||||||
|
|
||||||
var input = new TestModel();
|
var json = $"[{{\"__dotNetObject\":{ref2.ObjectId}}},{{\"__dotNetObject\":{ref1.ObjectId}}}]";
|
||||||
var dotNetObjectRef = DotNetObjectRef.Create(input);
|
|
||||||
var objectId = dotNetObjectRef.ObjectId;
|
|
||||||
|
|
||||||
var json = $"{{\"__dotNetObject\":{objectId}}}";
|
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var deserialized = JsonSerializer.Deserialize<DotNetObjectRef<TestModel>>(json);
|
var deserialized = JsonSerializer.Deserialize<DotNetObjectRef<TestModel>[]>(json);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Same(input, deserialized.Value);
|
Assert.Same(instance2, deserialized[0].Value);
|
||||||
Assert.Equal(objectId, deserialized.ObjectId);
|
Assert.Same(instance1, deserialized[1].Value);
|
||||||
});
|
});
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue