c# - MVC - Http Post JSON - Serialization Dependency -


i using asp mvc jquery , posting json data controller.

var jsonpostdata = {       control: "personcontrol",      data: {             name: "craig",             age: 25      }  }; 

i using following perform serialization.

public override void onactionexecuting(actionexecutingcontext filtercontext) {     var contenttype = filtercontext.httpcontext.request.contenttype                    ?? string.empty;     if (contenttype.contains("application/json"))     {         object jsonobject = new datacontractjsonserializer(roottype)             .readobject(filtercontext.httpcontext.request.inputstream);         filtercontext.actionparameters[param] = jsonobject;     } } 

which serializes following model:

[datacontract] public class controlmodel    {     [datamember(name = "control", isrequired = true)]     public string control { get; set; }     [datamember(name = "data"]     public object data { get; set; } } 

this working fine.

the problem trying resolve type data dependent upon passed control value.

i use reflection create control instance control value. able call control instance dynamic type use serialize "data" separately.

customcontrol.getdatatype() here return typeof(personmodel)

object jsonobject = new datacontractjsonserializer(customcontrol.getdatatype())     .readobject(filtercontext.httpcontext.request.inputstream);    [datacontract] //this data serialized public class personmodel    {     [datamember(name="name", isrequired=true)]     public string name { get; set; }         [datamember(name="age", isrequired=true)]     public string age { get; set; } } 

so trying find if can parse json in 2 different partial chunks.

ideas? suggestions?


as per suggestion thabaddawg, ended going json.net route, allows me parse json items individually, allowing me first pull out control, , later in control implementation pull out needed custom data items.

here example above rewritten (and simplified example) use this:

public override void onactionexecuting(actionexecutingcontext filtercontext) {     if ((filtercontext.httpcontext.request.contenttype ?? string.empty).contains("application/json"))     {         var bytes = new byte[filtercontext.httpcontext.request.inputstream.length];          filtercontext.httpcontext.request.inputstream.read(bytes, 0, bytes.length);         filtercontext.httpcontext.request.inputstream.position = 0;          jobject jsonobject = jobject.parse(encoding.utf8.getstring(bytes));          filtercontext.actionparameters["control"] = (string) jsonobject["control"];         filtercontext.actionparameters["action"] = (string)jsonobject["action"];     } } 

off top of head suggest going json.net route using linq json parser , skip deserialization of top level (just query it's value) , have decide type deserialize nested object into. experience datacontractjsonserializer leads me believe unable find simple solution going route.

there's pretty example of using linq json here : http://james.newtonking.com/projects/json/help/linqtojson.html


Comments