1

I have a restful service based on WCF like below: (The FeedbackInfo class has only one enum member - ServiceCode.)

[OperationContract]
[WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public List<FeedbackInfo> GetFeedbackInfoList(ServiceCode code)
{
    return ALLDAO.GetFeedbackInfoList(code);
}

[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public int? CreateFeedback(FeedbackInfo feedback)
{
    return ALLDAO.CreateFeedback(feedback);
}

I will use jquery ajax to invoke these two method like below:

$.ajax({
    type: "GET",
    url: "/Service/ALL.svc/GetFeedbackInfoList",
    datatype: "text/json",
    data: { "code": "Info"},
});



var feedbackInfo = { feedback: {
    ServiceCode: "Info"
}};
$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "/Service/ALL.svc/CreateFeedback",
    datatype: "text/json",
    data: JSON.stringify(feedbackInfo),
});

The first calling will be excuted succesfully, whereas the second one give me an error: The value "Info" cannot be parsed as the type 'Int64'. I'm wondering why the same one enum can not be parsed in the second calling? Just because of the enum type being used as a member of class?

EDIT: The FeedbackInfo and ServiceCode are looked like below:

public class FeedbackInfo
{
    public int ID { get; set; }
    public string Title { get; set; }
    public ServiceCode ServiceCode { get; set; }
}
[DataContract]
public enum ServiceCode
{
    [EnumMember]
    Info,
    [EnumMember]
    Error
}
Domi.Zhang
  • 1,675
  • 4
  • 21
  • 27
  • Can you post on how your FeedbackInfo class looks like? – Rajesh Jun 11 '12 at 10:02
  • see the answers on http://stackoverflow.com/questions/2441290/json-serialization-of-enum-as-string -- specifically http://stackoverflow.com/a/8654683/1037948 – drzaus Mar 20 '15 at 16:58

2 Answers2

1

I have put together a better solution that uses the Newtonsoft.Json library. It fixes the enum issue and also makes the error handling much better. It's quite a lot of code, so you can find it on GitHub here: https://github.com/jongrant/wcfjsonserializer/blob/master/NewtonsoftJsonFormatter.cs

You have to add some entries to your Web.config to get it to work, you can see an example file here: https://github.com/jongrant/wcfjsonserializer/blob/master/Web.config

Jon Grant
  • 11,369
  • 2
  • 37
  • 58
0

Enums are serialized as integers, so you'd need to use ServiceCode: 1 (or whatever), or alternatively, add a custom property in the FeedbackInfo class to deserialize the enum from a given string value. Ie, something like this:

public string ServiceCode { 
    get {
        return ServiceCodeEnum.ToString();
    }
    set {
        MyEnum enumVal;
        if (Enum.TryParse<MyEnum>(value, out enumVal))
            ServiceCodeEnum = enumVal;
        else
            ServiceCodeEnum = default(MyEnum);
    }
}

private MyEnum ServiceCodeEnum { get; set; }
McGarnagle
  • 101,349
  • 31
  • 229
  • 260