c# - Deserialize a dynamic property in an ASP.net web-api -
i have element
class contains anoptions
property, option class can in turn change properties example have these 2
element1 = { "id": "1", "options": { "printable": "true", "stackoverflow": "great" } } element2 = { "id": "2", "options": { "question": "awsome", "propertydiferent": "empty" } }
on web api have method this:
public object post ([frombody] element element) { savetomongo (element); }
the element class:
public class element { public dynamic options {get; set; } public string id {get; set; } }
when pick element mongo have no problems. when send using post method of api web, not deserialize itself, in expando object since mongo. how similar behavior @ both ends?
edit: tried change options dynamic newtonsoft jobject, didn't work. options saved, generate father don't want.
"options" : { "_t" : "newtonsoft.json.linq.jobject, newtonsoft.json, version=6.0.0.0, culture=neutral, publickeytoken=30ad4fe6b2a6aeed", "_v" : { "padding" : { "_t" : "jarray", "_v" : [ { "_t" : "jvalue", "_v" : [ ] }, { "_t" : "jvalue", "_v" : [ ] }, { "_t" : "jvalue", "_v" : [ ] }, { "_t" : "jvalue", "_v" : [ ] } ] }, "image" : { "_t" : "jvalue", "_v" : [ ] }, "alt" : { "_t" : "jvalue", "_v" : [ ] }, "url" : { "_t" : "jvalue", "_v" : [ ] }, "width" : { "_t" : "jvalue", "_v" : [ ] }, "backgroundcolor" : { "_t" : "jvalue", "_v" : [ ] }, "text" : { "_t" : "jvalue", "_v" : [ ] } } },
there more properties because extracted example directly mongo.
first step, make ajax
call javascript pass json string, otherwise dynamic property options not populated.
element.options = element.options.tostring ();
so have json string although stored in mongo can recover without being modified. able return in api have deserialize before deserialization done controller itself.
element.options = jobject.parse (element.options);
it uncool system, few disadvantages, not allow treatment of options in mongo. has been way in have managed passage mongo did not modify object.
Comments
Post a Comment