c# - IIS-Hosted WCF REST service - JSON serialize enum as string -


is possible serialize enum either proper string value or value specified enummember attribute rather number? seems json serialization ignores value property of enummember attribute. if change webmessageformat xml works fine, need use json.

i have following iis-hosted rest service

[operationcontract] [webget(responseformat = webmessageformat.json,     uritemplate = "test")]     someobject test(); 

someobject:

[datacontract] public class someobject {     [datamember]     public someenum foobar     {         get;         set;     } } 

someenum:

[datacontract] public enum someenum {     [enummember(value = "foovalue")]     [description("foodescription")]     foo,      [enummember(value = "barvalue")]     [description("bardescription")]     bar, } 

what get:

{"foobar":0} 

i'd 1 of following (preferably first, either 1 works):

{"foobar": "foovalue"} {"foobar": "foo"} 

you'll need reference json.net use stringenumconverter attribute on property follows :

using newtonsoft.json; using newtonsoft.json.converters;  [jsonconverter(typeof(stringenumconverter))] public someenum foobar {get;set;} 

please refer newtonsoft docs more details hope helps !


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -