.net - ASP.NET Web API Custom JsonConverter is never called -


so here's situation. i'm implementing web api in webforms app. have bunch of dynamic classes dictionaries need use custom json serialization formatter in order work (because default converter shows mess of key value pairings).

so first created custom json converter:

/// <summary> /// class convert entities json /// </summary> public class entityjsonconverter : jsonconverter {     public override bool canconvert(type objecttype)     {         return objecttype.issubclassof(typeof(entity));     }      public override bool canread     {         { return true; }     }      public override bool canwrite     {         { return true; }     }      public override void writejson(jsonwriter writer, object value, jsonserializer serializer)     {         // details not important. code called , works perfectly.     }      public override object readjson(jsonreader reader, type objecttype, object existingvalue, jsonserializer serializer)     {         // details not important. code *never* called reason.     } } 

once have defined insert global json media type formatter:

        // add custom converter entities.         foreach (var formatter in globalconfiguration.configuration.formatters)         {             var jsonformatter = formatter jsonmediatypeformatter;             if (jsonformatter == null)                 continue;              jsonformatter.serializersettings.converters.add(new entityjsonconverter());         } 

and lastly, test api (there many more added in future, i'm trying test out system now, "contact" inherits "entity"):

public class contactcontroller : apicontroller {     public ienumerable<contact> get()     {         // details not important. works perfectly.     }      [httppost]     public bool update(contact contact)     {         // details not important. contact "null".     } } 

so here's i'm seeing when debug:

web site calls "get":

  1. controller.get called. returns list of contacts.
  2. converter.canconvert called enumeration type. returns false.
  3. converter.canconvert called contact type. returns true.
  4. converter.canwrite called. returns true.
  5. converter.writejson called. writes proper json stream
  6. website receives proper json , able use object.

web site calls "update":

  1. converter.canconvert called contact type. returns true.
  2. controller.update called. "contact" parameter "null".

i'm utterly perplexed. don't understand why works when serializing, entire process seems skip custom converter when trying deserialize. have ideas i'm doing wrong?

thanks!

ah geez. feel dumb.

... wasn't sending json in post data. accidentally sending jumble of text. whoops...

nevermind!


Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -