Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CefSharp crashing when javascript tries to parse an object containing a list sent from C#

I'm using CefSharp to have a webbrowser run angularjs code inside a winforms application.

I am able to send c# objects to the js side and then use them if they only contain strings, ints, and so on. But when I try to send an object containing a list of values in it, CefSharp crashes when it trys to parse it on the js side.

An example of the c# code:

public class TestClass
{
    public string name { get; set; }
    public string other { get; set; }
    public List<int> ints { get; set; } 
}

Working obj:

TestClass tc = new TestClass()
{
    name = "Bobby Test",
    other = "Hello"
};

Obj causing crashes:

TestClass tc = new TestClass()
{
    name = "Bobby Test",
    other = "Hello",
    ints = new List<int>(){0,1}
};

How I pass to the js code:

browser.RegisterJsObject("testObj", tc);

My angular code setting it up for use:

$scope.$watch(function ()
{ return window.testObj },
function () {
    $scope.someField = window.testObj;
});

I tried doing a JsonConvert.SerializeObject on the object before passing it but it ended up just being an empty object on the js side.

EDIT - Solution

C# to JS:

Changed TestClass to:

TestClass tc = new TestClass()
{
    name = "Bobby Test",
    other = "Hello",
    ints = new int[] {0,1}
};

And it works correctly with the code above without having to call any serializing or deserializing methods directly.

JS to C#:

Although it wasn't in my question:

I am passing an object to the js side with a callback function to c#, the callback function accepts a serialized string from the js side which I then deserialize on the c# side.

like image 892
Asguard Avatar asked Sep 25 '22 11:09

Asguard


1 Answers

Chromium can only handle javascript simple types (Arrays, Numbers, Strings, etc)

What I normally do on the server side with any complex data is return a JSON string:

JavaScriptSerializer().Serialize(myObject);

And then on the client side reconstitute it using

JSON.parse(myObjectAsString);

Works for me

like image 115
Dave Bush Avatar answered Oct 21 '22 01:10

Dave Bush