如何使用DataContractJsonSerializer将类类型而不是名称空间序列化为Json字符串 [英] How to serialize class type but not the namespace to a Json string using DataContractJsonSerializer
问题描述
我正在尝试在WCF服务中使用DataContractJsonSerializer
将类层次结构序列化为Json字符串.
序列化派生类的默认行为是将以下键值对添加到对象:
I'm trying to serialize a class hierarchy to a Json string using DataContractJsonSerializer
, in a WCF service.
the default behaviour for serializing a derived class is to add the following key value pair to the object:
"__type":"ClassName:#Namespace"
我的问题是名称空间很长,它们会膨胀Json字符串. 我想以某种方式干预序列化并将其输出:
My problem is that namespaces are long and they bloat the Json string. I would like to somehow intervene with the serialization and output this instead:
"__type":"ClassName"
并在反序列化时再次进行干预,以指向正确的名称空间(我在运行时知道).
and on deserialization intervene again to point to the correct namespace (which i know in runtime).
有什么办法做这种事吗?
Is there any way to do such a thing?
推荐答案
将namespace参数添加到数据协定即可解决问题.
[DataContract(Namespace = "")]
Adding the namespace parameter to the data contract does the trick.
[DataContract(Namespace = "")]
这篇关于如何使用DataContractJsonSerializer将类类型而不是名称空间序列化为Json字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!