I am using DataContractJsonSerializer or Json.net alone in my web api 2

after asking this question I tried to grab all this stuff for serialization in web api 2 and I read this http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml -serialization

But now I'm embarrassed

1 In my webapiconfig I do not have these lines

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter; json.UseDataContractJsonSerializer = true; 

So, I assume that I am using the Json.net serializer, which is the default. Anyway, I can still use the DataContract in the class, and therefore only properties decorated with the DataMember attribute will be serialized. Are these two assumptions correct?

2 If I do not decorate the DataContract class, all properties will be serialized. This happens with both Json.net and DataContractJsonSerializer

3 If I changed (as in the question I linked), then the converter that uses it still uses it because this one or one of Json.net is not related to the other? because if i use this in global

 var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter; json.UseDataContractJsonSerializer = true; json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); 

even if it does not cause any problems, it does not seem to pick it up (and does not use its own contract resource)

3 In case I have a class that comes from another, and I decorate the parent DataContract, it seems to me that I should decorate the child properties of DataMember to serialize it. Or am I doing something wrong?

thanks

+5
c # serialization asp.net-web-api
source share
1 answer

So, I assume that I am using the Json.net serializer, which is the default. Anyway, I can still use the DataContract in the class, and therefore only properties decorated with the DataMember attribute will be serialized. Are these two assumptions correct?

Json.Net by default you're right. DataMembers are supported as serializers. Json.Net does not require them.

2 If I do not decorate the DataContract class, all properties will be serialized. This happens with both Json.net and DataContractJsonSerializer

The DataContractJsonSerializer is intended for use with WCF client applications, where serialized types are typically POCO classes with the DataContract attribute applied to them. No DataContract, no serialization. The WCF mapping mechanism makes sending and receiving very simple, but only if your platform is uniform. If you start mixing in different toolkits, your program may go sideways.

JavaScriptSerializer can serialize any type, including anonymous types (one way), and does so in a more consistent way. You lose the “automatic” WCF, but get more integration options.

3 If I changed (as in the question I linked), then the converter that uses it still uses it because this one or one of Json.net is not related to the other? because if i use this in global

Put your code in Global.asax and you should use DataContractJsonSerializer. How do you know that he is not raised?

4 In case I have a class that comes from another, and I decorate the parent DataContract, it seems to me that I should decorate the child properties of DataMember to serialize it.

Yes. If you are using DataContractJsonSerializer.

0
source share

All Articles