开发者

Parsing enumeration member values when using JSON

开发者_如何转开发Microsoft reports that Enumeration member values are treated as numbers in JSON.

We're building the WebMethod output into a table by hand, so converting the member number into it's name on the client is possible, but it smells and would be less robust than converting it on the server, using Enum.GetName. For example, if we add or change the enumeration it would mean changing our Javascript.

Is there an (easy) way to do this in the WebMethod? The object serializes correctly, and was working when we were using XML to transport the result.


If you just need to display it on the client, I'd add an extra string-typed property getter to your wire entity that does the Enum.GetName call. If it has to round-trip, you could add a setter on the same property that parses the enum value.

Kinda nasty, but not as bad as hardcoding a list on the client...


You can use https://github.com/jlarsson/Kiwi.Json which has support for enum serialization.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜