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

service - Android MediaPlayer calls onCompletion before it already finished -

javascript - Training Neural Network to play flappy bird with genetic algorithm - Why can't it learn? -

javascript - Create a stacked percentage column -