c# - Custom Newtonsoft.Json serialization of objects in a seperate library -


i want write custom json serializer controller when encounters non-primitive(excluding string/datetime) looks in object id field/property , instead of serializing entire object, writes {id, name}(where id value of id, , name name of data object) instead.

the purpose behind writing cache viewer cache items can huge. take id field , ignore rest in ui, might have issues sending way data in response.

i'm having trouble figuring out should start, excuse question if little broad.

extra notes: data items need serialize, in separate library, , don't have access them, guaranteed use id field. it's ok if miss few data-points because non-primitive, non-datetime, non-string field.

this have far write primitives:

public class fieldsonlyformatterconfig : attribute, icontrollerconfiguration     {         public void initialize(httpcontrollersettings settings,             httpcontrollerdescriptor descriptor)         {             // clear formatters list.             settings.formatters.clear();             var formatter = new jsonmediatypeformatter();             formatter.serializersettings.referenceloophandling = newtonsoft.json.referenceloophandling.ignore;             formatter.serializersettings.contractresolver = new customresolver();             // add custom media-type formatter.             settings.formatters.add(formatter);         }       }      class customresolver : defaultcontractresolver     {         protected override jsonproperty createproperty(memberinfo member, memberserialization memberserialization)         {             jsonproperty prop = base.createproperty(member, memberserialization);              if (member.membertype == membertypes.field && (prop.propertytype.isprimitive || prop.propertytype == typeof(string)))             {                 prop.shouldserialize = obj => true;             }             else             {                 prop.shouldserialize = obj => false;             }              return prop;         }     } 

so more fiddling around able figure out how accomplish wanted.

i changed custom resolver detect if property type dataitem, , set prop.converter use custom jsonconverter:

class customresolver : defaultcontractresolver     {         protected override jsonproperty createproperty(memberinfo member, memberserialization memberserialization)         {             jsonproperty prop = base.createproperty(member, memberserialization);              if (member.membertype == membertypes.field && (prop.propertytype.isprimitive || prop.propertytype == typeof(string)))             {                 prop.shouldserialize = obj => true;             }             else if (prop.propertytype.issubclassof(typeof(dataitem)))             {                 prop.shouldserialize = obj => true;                 prop.converter = new dataitemconverter();             }             else             {                 prop.shouldserialize = obj => false;             }              return prop;         }     } 

in custom json converter, on writejson casted object dataitem(a class knew wanted inhertited from, has "id" field im looking for) , did few custom writes:

class dataitemconverter: jsonconverter     {          public override bool canconvert(type objecttype)         {             return true;         }          public override object readjson(jsonreader reader, type objecttype, object existingvalue, jsonserializer serializer)         {             throw new notimplementedexception();         }          public override void writejson(jsonwriter writer, object value, jsonserializer serializer)         {             var di = value dataitem;             writer.writestartobject();             writer.writepropertyname("id");             writer.writevalue(di.id);             writer.writeendobject();         }     } 

Comments

Popular posts from this blog

android - Pass an Serializable object in AIDL -

How to provide Authorization & Authentication using Asp.net, C#? -

How to use Authorization & Authentication in Asp.net, C#? -