12

Is it possible to replace the default JSON serialization of WCF (I'm currently testing with the webHttp behaviour), and passing application/json as the MIME type. In particular, I don't like that by default every property is a key/value pair like:

{"Key":"PropertyName", "Value":"PropertyValue"}

I'm using the service only for JSON-enabled endpoints (requesting data with jQuery + WCF).

1

2 Answers 2

13

You can use a message formatter to change the serializer used to deal with JSON. The post at https://learn.microsoft.com/en-us/archive/blogs/carlosfigueira/wcf-extensibility-message-formatters shows an example on how to change the default serializer (DataContractJsonSerializer) to another one (JSON.NET).

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

2 Comments

Thanks Carlos. It wasn't easy but I finally got it working.(I forgot the contentTypeMapper :)) )
Brilliant blog post. Making it super easy to support both json and XML formatting even using NewtonSoft's variation. :)
-1

Consider creating classes corresponding to your JSON object structure. In that case you don't have to use Dictionary<> like:

[DataContract]
public class Customer
{
    [DataMember(Name="name")]
    public string Name{get;set;}

    [DataMember(Name="id")]
    public int ID{get;set;}
}

This get serialized as:

{"name": "name-value", "id": "id-value"}

Of course, this is just an alternative to what you already have and may not be applicable.

Comments

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.