c# - How to serialize field of type class as main object fields with NewtonSoft json? -
assume have these classes:
public class baseentity { public int id; } public class : baseentity { public int afield; public c c; } public class b : baseentity { public string bfield; public c c; } public class c : baseentity { public string cfield; } now, want use json.net jsonconverter annotation serialize c below when want load a or b objects. example this:
[jsonconverter(typeof(jsonconvertercustomimpl))] public class c { public string cfield; } and result of serialized a or b objects should this:
// object { id: 0, afield: 0, cfield: '' } // b object { id: 0, bfield: 0, cfield: '' } i dont know how shoud=ld implement jsonconvertercustomimpl class.
update
i'm used this answer, defined flattenjsonconverter class , set jsonannotation of a , b, when run project, thi exception thrown:
an unhandled exception of type 'system.stackoverflowexception' occurred in newtonsoft.json.dll

update
see below diagram, c class attachment in application , many of models may contains that. attachment class have byte[] filecontent field contains uploaded file content. want serialize class flatten container class have access filecontent in ui side.
i found this way serialize c class flatten, throws exception when using jsonconverter in annotation.
note i'm serialize filecontent base64 string.

found way serialize object newtonsoft.json (http://blog.bitlinkit.com/custom-json/)
public actionresult hello() { dynamic d = new jobject(); d.metricid = 11; string json = newtonsoft.json.jsonconvert.serializeobject(d); return content(json,"json"); } no need implement jsonconvertercustomimpl class
Comments
Post a Comment