我在我的web API使用2或DataContractJsonSerializer的Json.net 1 [英] Am I using DataContractJsonSerializer or the Json.net one in my web api 2

查看:175
本文介绍了我在我的web API使用2或DataContractJsonSerializer的Json.net 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问这个<后href=\"http://stackoverflow.com/questions/20340045/custom-json-contractresolver-net-web-api-falling-back-to-camelcase-in-some-case\">question我试图让所有的Web API 2这个序列化的东西的交手,我读这
http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization

但现在我很困惑

1在我webapiconfig我没有这样的行

  VAR JSON = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.UseDataContractJsonSerializer =真;

所以我asume我使用Json.net串行这是违约的。无论如何,我仍然可以在一个类中使用DataContract,因此仅饰有数据成员属性属性将被序列化。在这两个假设是否正确?

2。如果我不带装饰一DataContract类的所有属性都会被序列化。这种事发生在这两个Json.net和DataContractJsonSerializer

3。如果我改变(如问题我联系)串行仍然使用它,解析器是不是因为它是Json.net一个或一件事是没有关系的另一个?因为如果我在全球使用

  VAR JSON = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.UseDataContractJsonSerializer =真;
json.SerializerSettings.ContractResolver =新CamelCasePropertyNamesContractResolver();

即使不提高它似乎并没有把它捡起来的任何问题(也没有使用自定义contractresolver)

3万一我有一个从另一个派生的类,我装点家长一个用DataContract在我看来,我有来装点孩子一个人与DataMember属性序列化它。还是我做错了什么?

感谢


解决方案

  

所以我asume我使用Json.net串行这是违约的。无论如何,我仍然可以在一个类中使用DataContract,因此仅饰有数据成员属性属性将被序列化。在这两个假设是否正确?


该Json.Net是你是正确的默认值。数据成员被两个串行的支持。 Json.Net不需要他们。


  

2。如果我不带装饰一DataContract类的所有属性都会被序列化。这种事发生在这两个Json.net和DataContractJsonSerializer


该DataContractJsonSerializer旨在与那里的序列类型通常POCO类施加给他们的DataContract属性WCF客户端应用。没有DataContract,无序列化。 WCF的映射机制,使发送和接收非常简单,但只有当你的平台是均匀的。如果你开始在不同的工具集搅拌,你的程序可能会侧身走了。

的的JavaScriptSerializer可以序列的任何类型,包括匿名类型(单程),并以更符合的方式这样做。你失去了WCF的AUTOMAGIC,但你获得更多的集成选项。


  

3。如果我改变(如问题我联系)串行仍然使用它,解析器是不是因为它是Json.net一个或一件事是没有关系的另一个?因为如果我在全球使用


将在Global.asax中的code和DataContractJsonSerializer应该被使用。你怎么知道它不是拿起?


  

4万一我有一个从另一个派生的类,我装点家长一个用DataContract在我看来,我有来装点孩子一个人与DataMember属性序列化了。


是的。如果您使用DataContractJsonSerializer。

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

解决方案

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.

这篇关于我在我的web API使用2或DataContractJsonSerializer的Json.net 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆