Skip to content
This repository has been archived by the owner on Jun 5, 2023. It is now read-only.

Fix for #97. #98

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion src/WebWindow.Blazor/DesktopJSRuntime.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.JSInterop;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using Microsoft.JSInterop.Infrastructure;
using System;
using System.Collections.Generic;
Expand All @@ -16,6 +17,7 @@ internal class DesktopJSRuntime : JSRuntime
public DesktopJSRuntime(IPC ipc)
{
_ipc = ipc ?? throw new ArgumentNullException(nameof(ipc));
JsonSerializerOptions.Converters.Add(new ElementReferenceJsonConverter());
}

protected override void BeginInvokeJS(long asyncHandle, string identifier, string argsJson)
Expand Down
52 changes: 52 additions & 0 deletions src/WebWindow.Blazor/SharedSource/ElementReferenceJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Microsoft.AspNetCore.Components
{
internal sealed class ElementReferenceJsonConverter : JsonConverter<ElementReference>
{
private static readonly JsonEncodedText IdProperty = JsonEncodedText.Encode("__internalId");

public override ElementReference Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
string id = null;
while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
{
if (reader.TokenType == JsonTokenType.PropertyName)
{
if (reader.ValueTextEquals(IdProperty.EncodedUtf8Bytes))
{
reader.Read();
id = reader.GetString();
}
else
{
throw new JsonException($"Unexpected JSON property '{reader.GetString()}'.");
}
}
else
{
throw new JsonException($"Unexpected JSON Token {reader.TokenType}.");
}
}

if (id is null)
{
throw new JsonException("__internalId is required.");
}

return new ElementReference(id);
}

public override void Write(Utf8JsonWriter writer, ElementReference value, JsonSerializerOptions options)
{
writer.WriteStartObject();
writer.WriteString(IdProperty, value.Id);
writer.WriteEndObject();
}
}
}