c# - Deserialize two values into the same property -


i have client can call 2 different versions of service.

one service sends single value:

{   "value" : { ... } } 

the second service returns multiple values:

{   "values" : [      { ... },     { ... }   ] } 

ideally, i'd represent single object in client classes user never sees whether it's single value or multiple values.

public class myvalues {   public list<stuff> values { get; set; }   public thing other { get; set; } } 

i think way i'll able accomplish custom jsonconverter class apply myvalues, want custom when i'm deserializing property value. can't seem figure out if icontractresolver better way go (e.g. somehow attach phantom property myvalues deserializes value , puts values.

if create custom converter, how tell deserialize else (e.g. if other has properties make sure handled appropriately, etc.)

to make custom jsonconverter has special processing few properties of type uses default processing remainder, can load json jobject, detach , process custom properties, populate remainder jobject jsonserializer.populate(), so:

class myvaluesconverter : custompropertyconverterbase<myvalues> {     protected override void processcustomproperties(jobject obj, myvalues value, jsonserializer serializer)     {         // remove value property manual deserialization, , deserialize         var jvalue = obj.getvalue("value", stringcomparison.ordinalignorecase).removefromlowestpossibleparent();         if (jvalue != null)         {             (value.values = value.values ?? new list<stuff>()).clear();             value.values.add(jvalue.toobject<stuff>(serializer));         }     } }  public abstract class custompropertyconverterbase<t> : jsonconverter t : class {     public override bool canconvert(type objecttype)     {         return typeof(t).isassignablefrom(objecttype);     }      public override object readjson(jsonreader reader, type objecttype, object existingvalue, jsonserializer serializer)     {         if (reader.tokentype == jsontoken.null)             return null;         var jobj = jobject.load(reader);         var contract = (jsonobjectcontract)serializer.contractresolver.resolvecontract(objecttype);         var value = existingvalue t ?? (t)contract.defaultcreator();          processcustomproperties(jobj, value, serializer);          // populate remaining properties.         using (var subreader = jobj.createreader())         {             serializer.populate(subreader, value);         }          return value;     }      protected abstract void processcustomproperties(jobject obj, t value, jsonserializer serializer);      public override bool canwrite { { return false; } }      public override void writejson(jsonwriter writer, object value, jsonserializer serializer)     {         throw new notimplementedexception();     } }  public static class jsonextensions {     public static jtoken removefromlowestpossibleparent(this jtoken node)     {         if (node == null)             return null;         var contained = node.ancestorsandself().where(t => t.parent jcontainer && t.parent.type != jtokentype.property).firstordefault();         if (contained != null)             contained.remove();         // detach node immediate containing property -- remove() not though seems should         if (node.parent jproperty)             ((jproperty)node.parent).value = null;         return node;     } } 

Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -