Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

c# - Am I using DataContractJsonSerializer or the Json.net one in my web api 2

after asking this question I was trying to get the grips of all of this serialization stuff 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 confused

1 In my webapiconfig I don't have this lines

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

So I asume I'm using the Json.net serializer which is the defaulted one. Anyway I still can use the DataContract in a class and therefore only the properties decorated with DataMember attribute will be serialized. Are these two assumptions correct?

2 If I don't decorate a class with DataContract all of the properties will be serialized. This happen to both the Json.net and the DataContractJsonSerializer

3 If I change (as in the question I linked) the resolver the serializer still uses it, is it because it's the Json.net one or one thing is not related to another? because if I use this in the Global

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

even if it doesn't raise any issue it doesn't seem to pick it up (nor using a custom contractresolver)

3 In case I have a class that derives from another one and I decorate the parent one with DataContract it seems to me that I have to decorate the child one's properties with DataMember for serializing it. Or am I doing something wrong?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

So I asume I'm using the Json.net serializer which is the defaulted one. Anyway I still can use the DataContract in a class and therefore only the properties decorated with DataMember attribute will be serialized. Are these two assumptions correct?

The Json.Net is the default you're right. DataMembers are supported by both serializers. Json.Net does not require them.

2 If I don't decorate a class with DataContract all of the properties will be serialized. This happen to both the Json.net and the DataContractJsonSerializer

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

The JavaScriptSerializer can serialize any type, including anonymous types (one way), and does so in a more conformant way. You lose the "automagic" of WCF, but you gain more integration options.

3 If I change (as in the question I linked) the resolver the serializer still uses it, is it because it's the Json.net one or one thing is not related to another? because if I use this in the Global

Place your code in Global.asax and the DataContractJsonSerializer should be used. How do you know that the it is not picked up ?

4 In case I have a class that derives from another one and I decorate the parent one with DataContract it seems to me that I have to decorate the child one's properties with DataMember for serializing it.

Yes. If you use DataContractJsonSerializer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...