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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With