8

My problem is this:

This is the response being sent back from my WebAPI controller.

"[
   [
      {\"id\":\"identifier\"},
      {\"name\":\"foobar\"}
   ]
]"

Notice that the response is wrapped in quotations and all of the embedded quotations are escaped. This is obviously a problem. Are there any settings I can provide to the JSON.NET Serializer to prevent this from occurring?

Edit

As p.s.w.g guessed in his response, I was using JSON.NET's

JsonConvert.SerializeObject(instance)

to perform my serialization.

I did this because as I was building out my custom Converters, I had included them in the JsonConvert.DefaultSettings within my WepApiConfig (and I obviously thought this would not be a problem)

I had previously tried to swap the return type of my HttpGets to "my object type" and the response was a json representation of my object's ToString() method...which let me know that serialization was not passing through my converters.

Changing the return type of my HttpGets from string to "my object type" and plugging those converters straight into WebAPi's default HttpConfiguration did the trick.

config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new FooConverter());
config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new BarConverter());

Easy peasy.

1
  • 1
    Can you show the API Action? Commented Aug 28, 2014 at 22:48

3 Answers 3

10

You probably have something like this:

public string GetFoobars()
{
    var foobars = ...
    return JsonConvert.SerializeObject(foobars);
}

In this case, you're serializing the object into string with Json.NET, then by returning the result as a string, the API controller will serialize the string as a JavaScript string literal—which will cause the string to be wrapped in double quotes and cause any other special characters inside the string to escaped with a backslash.

The solution is to simply return the objects by themselves:

public IEnumerable<Foobar> GetFoobars()
{
    var foobars = ...
    return foobars;
}

This will cause the API controller to serialize the objects using it's default settings, meaning it will serialize the result as XML or JSON depending on the parameters passed in from the client.

Further Reading

Sign up to request clarification or add additional context in comments.

8 Comments

Your top snippet is exactly what I am doing. I am calling JsonConvert.SerializeObject on my objects. (I'm not at my dev machine or I'd just test this directly) Will returning the object pass through the JSON.NET converter before WebAPI puts it in the response?
@K.AlanBates Yes, if you refer to the linked documentation, you'll see that this will use the JsonMediaTypeFormatter, which in turn uses Json.NET by default.
At this point, I think it might be important for me to mention that I have to use my own JsonConverters for this task. I removed them from my initial description because I didn't want to give irrelevant information. I need to be able to call JsonConvert.SerializeObject(or an equivalent method that will use my converters) and have it return json instead of a string. Returning an object does nothing more than serialize the object's ToString() and I cannot have that. I also have no DataContract to work with.
@K.AlanBates If you annotate the classes with a [JsonConverter] attribute that shouldn't be a problem. If you don't control these classes and can't add any attributes, can tweak the global JsonMediaTypeFormatter directly in your WebApiConfig.cs file (or similar)
I've been trying to remote into my dev box for the past few minutes to look at my WebApiConfig settings because I could swear that I have my JsonMediaTypeFormatter already configured. I've also added the custom converters to the SerializerSettings and calling JsonConvert.SerializeObject(instance) invokes my converters but returns string escaped JSON. I'm having trouble connecting, so I'll have to put this on hold until tomorrow morning when I can confirm my current config. Thanks for your help thus far.
|
0

the API serializes before sending the resonse. example : return Ok(yourUnSerializedObject);

No need to serailize for you.

Comments

0

Check your properties modifiers. by example:

  public List<Yourobjectclass> columnaLista { get; set; }

    public List<Yourobjectclass> ColumnaLista
    {
        get
        {
            return this.columnaLista;
        }
        set
        {
            this.columnaLista = value;
        }

the serializer will add all objects of columnaLista regarless the name because you have two properties with public modifier.

to fix this, you must change the property modifier like this:

      private List<Yourobjectclass> columnaLista { get; set; }

1 Comment

This problem was resolved 6 years ago. The core issue was that the Custom Converter had not been incorporated into the SerializerSettings of the JsonFormatter within the HttpConfiguration. config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new CustomConverter()); corrected the issue. (This was all detailed in the Edit portion of the question after I accepted an answer)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.