title: How can I return json from my WCF rest service (.NET 4), using Json.Net, without it being a string, wrapped in quotes?
body: <p><strong>UPDATE 10/19/2010</strong>
  I know I asked this question a while ago, but the workarounds shown in these answers are hardly satisfactory, and this is still a common problem for many.  WCF just isn't flexible.  I started my own open source C# library for creating REST services without WCF.  Check <a href="http://www.restcake.net" rel="noreferrer">restcake.net</a> or <a href="http://rest.codeplex.com" rel="noreferrer">rest.codeplex.com</a> for info on said library.
  <strong>END UPDATE</strong></p>
  
  <p><strong>UPDATE 8/2/2012</strong>
  <a href="http://www.asp.net/web-api" rel="noreferrer">ASP.NET Web API</a> (previously WCF Web API, the replacement for REST WCF) uses <a href="http://james.newtonking.com/projects/json-net.aspx" rel="noreferrer">Json.NET</a> by default
  <strong>END UPDATE</strong></p>
  
  <p>The <code>DataContractJsonSerializer</code> is unable to handle many scenarios that <a href="http://json.codeplex.com/" rel="noreferrer">Json.Net</a> handles just fine when properly configured (specifically, cycles).</p>
  
  <p>A service method can either return a specific object type (in this case a <a href="http://en.wikipedia.org/wiki/Data_transfer_object" rel="noreferrer">DTO</a>), in which case the <code>DataContractJsonSerializer</code> will be used, or I can have the method return a string, and do the serialization myself with Json.Net.  The problem is that when I return a json string as opposed to an object, the json that is sent to the client is wrapped in quotes.</p>
  
  <p>Using <code>DataContractJsonSerializer</code>, returning a specific object type, the response is:<br>
  <strong><code>{"Message":"Hello World"}</code></strong></p>
  
  <p>Using Json.Net to return a json string, the response is:<br>
  <strong><code>"{\"Message\":\"Hello World\"}"</code></strong></p>
  
  <p>I do not want to have to eval() or JSON.parse() the result on the client, which is what I would have to do if the json comes back as a string, wrapped in quotes.  I realize that the behavior is correct; it's just not what I want/need.  I need the raw json; the behavior when the service method's return type is an object, not a string.</p>
  
  <p>So, how can I have my method return an object type, but <strong>not</strong> use the DataContractJsonSerializer?  How can I tell it to use the Json.Net serializer instead?</p>
  
  <p>Or, is there someway to directly write to the response stream?  So I can just return the raw json myself?  Without the wrapping quotes?</p>
  
  <p>Here is my contrived example, for reference:</p>
  
  <pre><code>[DataContract]
  public class SimpleMessage
  {
      [DataMember]
      public string Message { get; set; }
  }
  
  [ServiceContract]
  [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
  [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
  public class PersonService
  {
      // uses DataContractJsonSerializer
      // returns {"Message":"Hello World"}
      [WebGet(UriTemplate = "helloObject")]
      public SimpleMessage SayHelloObject()
      {
          return new SimpleMessage("Hello World");
      }
  
      // uses Json.Net serialization, to return a json string
      // returns "{\"Message\":\"Hello World\"}"
      [WebGet(UriTemplate = "helloString")]
      public string SayHelloString()
      {
          SimpleMessage message = new SimpleMessage() { Message = "Hello World" };
          string json = JsonConvert.Serialize(message);
          return json;
      }
  
      // I need a mix of the two.  Return an object type, but use the Json.Net serializer.
  }
  </code></pre>