I have a Silverlight class marked with the ScriptableType & ScriptableMember and I expect to be able to pass the object from Silverlight to javascript. When I call JSON.stringify (in javascript) I expect to receive a JSON representation of the object but all I get is {}
The class is defined as:
[ScriptableType()]
public class MyEvent
{
[ScriptableMember(ScriptAlias = "eventContent")]
public int EventContent { get; set; }
}
I pass the object from Silverlight like this:
var jsonObject = new MyEvent { EventContent = 1 };
HtmlPage.Window.Invoke("publishValue", topic, jsonObject);
And in javascript I’m doing the following:
alert(topic);
alert(jsonObject);
alert(JSON.stringify(jsonObject));
When I use the debugger I only see the jsonObject as of type Object but the call alert(jsonObject) returns the correct type and if I access the property jsonObject.eventContent I get the correct value back, but it doesn’t serialize correctly with JSON.stringify.
Anyone tell what I’m doing wrong?
I don’t want to have to serialize the object in Silverlight before sending to javascript.
Cheers
AWC
I’ve managed to solve the problem!
Instead of declaring an object like this for passing from Silverlight to javascript:
I use the
System.Jsonnamepsace and create aJsonObjectlike this:Check out the documentation of
System.Json.JsonObjectfor more info.Cheers
AWC