WCF and REST endpoint

S

Scott

Hello,

I am working on a project that has a REST endpoint that is created with WCF
version 3.5. The object that is being returned has the necessary
[DataControl] & [DataMember] attributes and is serialized to JSON. This is
working correctly.

In the code that gets the object, it was found that it is actually already a
serialized JSON string. When the WCF endpoint was updated to return a
"string" to prevent the overhead of deserializing the object in the first
place. The issue is that the serialized JSON string was serialized again and
created invalid JSON; the client would have to deserialize it twice.

Is there a way to prevent WCF from serializing the object that is being
returned so that the string that is already JSON is returned? If this is not
possible, I'll just return the code to the way it was.

Thanks,
 
S

Steven Cheng

Hi,

From your description, you're developing a WCF rest service which will
return JSON format data objects, however, you found that the object to
return has been already in Json =string format, so you're wondering how to
prevent the data be serialized as Json repeatedly, correct?

For the data object, is it always pregenerated as Json string before the
WCF engine serialize it? If so, I think you should define your REST
service(the serviceContract) to use normal XML response format instead of
json. e.g.

[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Xml
)]

If there is some other objects still need to be serialized as Json
format(not pregenerated as Json string), you can use
DataContractJsonSerializer to generate Json format output in code:

#How to: Serialize and Deserialize JSON Data
http://msdn.microsoft.com/en-us/library/bb412179.aspx

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.




--------------------
From: =?Utf-8?B?U2NvdHQ=?= <[email protected]>
Subject: WCF and REST endpoint
Date: Thu, 28 Jan 2010 16:41:01 -0800
Hello,

I am working on a project that has a REST endpoint that is created with WCF
version 3.5. The object that is being returned has the necessary
[DataControl] & [DataMember] attributes and is serialized to JSON. This is
working correctly.

In the code that gets the object, it was found that it is actually already a
serialized JSON string. When the WCF endpoint was updated to return a
"string" to prevent the overhead of deserializing the object in the first
place. The issue is that the serialized JSON string was serialized again and
created invalid JSON; the client would have to deserialize it twice.

Is there a way to prevent WCF from serializing the object that is being
returned so that the string that is already JSON is returned? If this is not
possible, I'll just return the code to the way it was.

Thanks,
 
S

Scott

Hello Steven,

Thank you for the information.

Changing the ResponseFormat to XML allows the string to be sent from the
endpoint within out being serialized. The only issue is that before it is
deserialized within the client of the endpoint, the XML tags must be removed.
Ideally there would be a WebMessageFormat of "None" which would do no
serialization but the solution provided will work.

Thanks,
Scott
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,920
Messages
2,570,038
Members
46,449
Latest member
onedumbsquirrel

Latest Threads

Top