Serializing passed object

Dec 10, 2014 at 2:50 PM
hi

i wrote a hosted function :
public void myUpdate(string collectionName, DynamicObject entity)
{
  //somehow serialize the object to json
}
engine.Execute("myObjdate({name:"hello"});
And i didnt found any way to serialize the object to json/bson any idea on how to do it?
Dec 10, 2014 at 3:35 PM
Check out Json.NET (http://james.newtonking.com/json), if your DynamicObject class is json-serializable all it takes is a JsonConvert.Serialize(entity) to get a JSON representation.
Coordinator
Dec 10, 2014 at 3:52 PM
Edited Dec 10, 2014 at 3:53 PM
Another possibility, if your function has access to the script engine, might be to use its JSON object:
public void myUpdate(string collectionName, DynamicObject entity)
{
    var json = Convert.ToString(engine.Script.JSON.stringify(entity));
    // ...
}
Good luck!