Rectangle 27 0

How to set Json.Net as the default serializer for WCF REST service?


CreateStreamResponse
CreateTextResponse
HttpStatusCode.Conflict
HttpStatusCode.Created
HttpStatusCode.Unauthorized
Message
Stream
WebOperationContext.Current.OutgoingResponse.StatusCode
public Message UpdateCity(string code, City city)
{
    MyResponseDataClass message = CreateMyResponse();
    // use JSON.NET to serialize the response data
    string myResponseBody = JsonConvert.Serialize(message);
    return WebOperationContext.Current.CreateTextResponse (myResponseBody,
                "application/json; charset=utf-8",
                Encoding.UTF8);
}

From that link, it looks like UpdateCity would have to take a Message parameter to be able to deserialize with JSON.NET. Is that correct? Any way around it?

How does that handle deserializing the incoming City object?

I know it's old, but solved my issue with TimeSpan and DateTime formats.

If you already use Message type to return the results an use WCF4 you can do something like following:

Oleg, many thanks, it works like a charm by returning Stream type, it stops Microsft serializer. I am aware of DataContractSerializerOperationBehavior, but it takes me to inheret my own Serializer from XmlObjectSerializer which is not an easy task. Your proposal is much simpler and straight forward, Thank you once more.

The usage of Extending Encoders and Serializers (see http://msdn.microsoft.com/en-us/library/ms733092.aspx) or other methods of Extending WCF like usage of DataContractSerializerOperationBehavior is very interesting, but for your special problem there are easier solution ways.

Note
Rectangle 27 0

How to set Json.Net as the default serializer for WCF REST service?


CreateStreamResponse
CreateTextResponse
HttpStatusCode.Conflict
HttpStatusCode.Created
HttpStatusCode.Unauthorized
Message
Stream
WebOperationContext.Current.OutgoingResponse.StatusCode
public Message UpdateCity(string code, City city)
{
    MyResponseDataClass message = CreateMyResponse();
    // use JSON.NET to serialize the response data
    string myResponseBody = JsonConvert.Serialize(message);
    return WebOperationContext.Current.CreateTextResponse (myResponseBody,
                "application/json; charset=utf-8",
                Encoding.UTF8);
}

From that link, it looks like UpdateCity would have to take a Message parameter to be able to deserialize with JSON.NET. Is that correct? Any way around it?

How does that handle deserializing the incoming City object?

I know it's old, but solved my issue with TimeSpan and DateTime formats.

If you already use Message type to return the results an use WCF4 you can do something like following:

Oleg, many thanks, it works like a charm by returning Stream type, it stops Microsft serializer. I am aware of DataContractSerializerOperationBehavior, but it takes me to inheret my own Serializer from XmlObjectSerializer which is not an easy task. Your proposal is much simpler and straight forward, Thank you once more.

The usage of Extending Encoders and Serializers (see http://msdn.microsoft.com/en-us/library/ms733092.aspx) or other methods of Extending WCF like usage of DataContractSerializerOperationBehavior is very interesting, but for your special problem there are easier solution ways.

Note
Rectangle 27 0

How to set Json.Net as the default serializer for WCF REST service?


[WebGet(UriTemplate = "", ResponseFormat = WebMessageFormat.Json)]

Correct, IsReference is the only issue. This is reported in many posts DataContractJsonSerializer can not serialize entities marked with IsReference=true. That's why I'm looking to use another Serializer. Many Thanks

DataContractJsonSerializer also have very bad serialization of DateTime.

Is there some reason why you want to use the Json.NET library specifically. If you want to return JSON, why not just use the ResponseFormat property from the WebGet and WebInvoke attributes?

It's WCF4. In my web.config, my <standardEndpoint> has defaultOutgoingResponseFormat="Json", so I do not have to decorate the service methods. By design all of my Entities have [IsReference=true] and cannot get serialized with the default DataContractSerializer. So I have to use onther Serializer like Json.net that can handle Entities with [IsReference=true]. I'm returning the Message type to not get my response serialzed twice, once by json.net and then by DataContractSerializer. If I returned the actuall type I'd get invalid json like "{\"City\":\"Cairo\"}" Thanx for your reply.

So this works for you if you do not use the IsReference attribute but you have to use it by design for another reason?

That should for most cases. What version of WCF are you running? Any reason you're returning a Message type rather than the actual type?

The default JSON serializers lack any kind of support for embedding and reviving type information and preserving object references and allowing self-referencing loops. JSON.NET handles them well via the inclusion of $type, $id, and $ref properties.

Note
Rectangle 27 0

How to set Json.Net as the default serializer for WCF REST service?


[WebGet(UriTemplate = "", ResponseFormat = WebMessageFormat.Json)]

Correct, IsReference is the only issue. This is reported in many posts DataContractJsonSerializer can not serialize entities marked with IsReference=true. That's why I'm looking to use another Serializer. Many Thanks

DataContractJsonSerializer also have very bad serialization of DateTime.

Is there some reason why you want to use the Json.NET library specifically. If you want to return JSON, why not just use the ResponseFormat property from the WebGet and WebInvoke attributes?

It's WCF4. In my web.config, my <standardEndpoint> has defaultOutgoingResponseFormat="Json", so I do not have to decorate the service methods. By design all of my Entities have [IsReference=true] and cannot get serialized with the default DataContractSerializer. So I have to use onther Serializer like Json.net that can handle Entities with [IsReference=true]. I'm returning the Message type to not get my response serialzed twice, once by json.net and then by DataContractSerializer. If I returned the actuall type I'd get invalid json like "{\"City\":\"Cairo\"}" Thanx for your reply.

So this works for you if you do not use the IsReference attribute but you have to use it by design for another reason?

That should for most cases. What version of WCF are you running? Any reason you're returning a Message type rather than the actual type?

The default JSON serializers lack any kind of support for embedding and reviving type information and preserving object references and allowing self-referencing loops. JSON.NET handles them well via the inclusion of $type, $id, and $ref properties.

Note