Blazor javascript interop - object conversion from js to .NET - Stack Overflow

I am using Blazor javascript interop (js call to .NET), as injsdotNetObjRef.invokeMethodAsync("

I am using Blazor javascript interop (js call to .NET), as in

//js
dotNetObjRef.invokeMethodAsync("DotNetMethod", jsObject);
---------------------------------------------------------
//C#
[JSInvokable]
public void DotNetMethod(object jsObject)
{
    Console.WriteLine($"jsObject type is {jsObject.GetType()}");
}

in the browser console, I get:

'jsObject type is SimpleJson.JsonObject'

Now I would like to cast jsObject to a concrete SimpleJson.JsonObject, as in

[JSInvokable]
public void DotNetMethod(object jsObject)
{
    JsonObject jsObject = (JsonObject)jsObject; //error
}

but all my trials using C# munity implementations of SimpleJson (like ) fail plaining that the cast is not valid.

As a workaround I go through strings:

//js
dotNetObjRef.invokeMethodAsync("DotNetMethod", JSON.stringify(jsObject));
.
//C#
[JSInvokable]
public void DotNetMethod(string jsObjectJSON)
{
    JsonObject jsObject = SimpleJson.DeserializeObject<JsonObject>(jsObjectJSON);
}

Does anyone know whether it is possible (and how) to use the received jsObject directly, i.e. avoiding the serialization/deserialization (and without reflection)?

I am using Blazor javascript interop (js call to .NET), as in

//js
dotNetObjRef.invokeMethodAsync("DotNetMethod", jsObject);
---------------------------------------------------------
//C#
[JSInvokable]
public void DotNetMethod(object jsObject)
{
    Console.WriteLine($"jsObject type is {jsObject.GetType()}");
}

in the browser console, I get:

'jsObject type is SimpleJson.JsonObject'

Now I would like to cast jsObject to a concrete SimpleJson.JsonObject, as in

[JSInvokable]
public void DotNetMethod(object jsObject)
{
    JsonObject jsObject = (JsonObject)jsObject; //error
}

but all my trials using C# munity implementations of SimpleJson (like https://github./facebook-csharp-sdk/simple-json) fail plaining that the cast is not valid.

As a workaround I go through strings:

//js
dotNetObjRef.invokeMethodAsync("DotNetMethod", JSON.stringify(jsObject));
.
//C#
[JSInvokable]
public void DotNetMethod(string jsObjectJSON)
{
    JsonObject jsObject = SimpleJson.DeserializeObject<JsonObject>(jsObjectJSON);
}

Does anyone know whether it is possible (and how) to use the received jsObject directly, i.e. avoiding the serialization/deserialization (and without reflection)?

Share Improve this question asked May 31, 2019 at 3:33 Franco TiveronFranco Tiveron 2,9361 gold badge27 silver badges44 bronze badges 2
  • Typically JSInterop does require you to go through string serialization. In fact, that you want to interact with the parameters as a JsonObject seems to imply that's exactly what you're trying to do anyway. So I'm a bit confused why you think your workaround isn't perfectly proper? – Kirk Woll Commented Jun 1, 2019 at 2:03
  • Blazor provides JS interop, including implicit transformations CLI <=> JSON. If I know the class signature of the type I can pass instances between the two worlds and blazor automatically transforms them. When I saw that the type of the object ing from js to C# is JsonObject I hoped I could rely on Blazor built-in capabilities and simply cast to JsonObject instead of manually serializing/deserializing it, but apparently Blazor looks for a strongly typed destination. In other words, If I want a weakly type representation like JsonObject, I have to be explicit. Not a big issue, though. – Franco Tiveron Commented Jun 14, 2019 at 0:53
Add a ment  | 

1 Answer 1

Reset to default 7

Instead of directly receiving a JSON, why not receive a previously defined object?

let x = {
    "data": "test",
    "moreData": "another test"
}

dotnetHelper.invokeMethodAsync('selectionChanged', x);

And then

//C#
public class InteropHelper
{
    public event Action OnSelectionChanged;

    [JSInvokable]
    public void selectionChanged(TestItem data)
    {
        OnSelectionChanged?.Invoke();
    }

    class TestItem
    {
        public string data { get; set; }
        public string moreData { get; set; }
    }
}

Blazor would automatically know which fields from the JSON go with which fields from TestItem.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744270933a4566097.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信