2

I have a WCF service that returns database tables in JSON format. SeralizeObject adds unicode to my httpresponse, how can i remove this?

Code:

using (var db = new newTestDBContext())
        {
          var query = from b in db.Roads
                       orderby b.roadID
                     select b;
          Road rr = query.First();
          var serializerSettings = new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects };
          return JsonConvert.SerializeObject(rr, Formatting.Indented, serializerSettings);

Reponse:

"{\u000d\u000a  \"$id\": \"1\",\u000d\u000a  \"roadparts\": [\u000d\u000a    {\u000d\u000a      \"$id\": \"2\",\u000d\u000a      \"Road\": {\u000d\u000a       
user2049921
  • 81
  • 1
  • 8
  • 1
    I wonder if this is actually because you are returning a string via json, where that string happens to also *contain* json, so it is having to encode the newlines etc that json.net added because of `Formatting.Indented`. Frankly, the first thing I would do would be: don't add `Formatting.Indented`. The second thing I would do is check whether it is being converted to json too early (or: twice). – Marc Gravell Feb 07 '13 at 08:28
  • My WebGet returns json: ResponseFormat = WebMessageFormat.Json, my function GetData returns a JSON aswell from SerializeObject. Is this wrong? – user2049921 Feb 07 '13 at 09:03

2 Answers2

2

ResponseFormat = WebMessageFormat.Json

That will JSON encode the return value of the annotated method. If the return value is already a JSON string, then you are JSON encoding twice.. first the Road object and then the JSON string resulting from the former.

So just return the Road object and let the WebMessageFormat.json handle the json encoding.

Esailija
  • 138,174
  • 23
  • 272
  • 326
1

Remove the Formatting.Indented in your json call. That should fix it.

John Pramanti
  • 135
  • 1
  • 9