Silverlight passing JSON object incorrectly? Silverlight passing JSON object incorrectly? json json

Silverlight passing JSON object incorrectly?


JSON.stringify enumerates over enumerable properties of an object using for...in. If the properties aren't enumerable, they won't be included in the resulting string.

The Silverlight object is an external object, not a native javascript object. Just like an ActiveXObject, the properties are not discoverable/enumerable. I'm not sure if there's a way around this. A couple of pages I found hint towards implementing IEnumerable to be able to iterate using a foreach in the native language but I'm not sure if this would carry over to JavaScript.

I wouldn't rely on it being possible but you never know. If you require an object to be enumerable, the only way might be to serialize it using System.Json and call eval on the document to unserialize it in JavaScript.


I've managed to solve the problem!

Instead of declaring an object like this for passing from Silverlight to javascript:

[ScriptableType()] public class MyEvent {     [ScriptableMember(ScriptAlias = "eventContent")]     public int EventContent { get; set; } } 

I use the System.Json namepsace and create a JsonObject like this:

  var ob = new JsonObject  {       {"eventContent", 1}  };

Check out the documentation of System.Json.JsonObject for more info.

Cheers

AWC