我该如何告诉DataContractJsonSerializer不包括" __类型"属性 [英] How do I tell DataContractJsonSerializer to not include the "__type" property
本文介绍了我该如何告诉DataContractJsonSerializer不包括" __类型"属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要添加KnownType到低于code,才能成功序列化。当我这样做,生成的JSON如下:
成人的JSON形式与1名儿童:{时代:42,名:约翰,孩子:[{__类型:
儿童:#TestJson,时代:4,名:简,手指:10}]}
我怎么有它不包括__type:孩子:#TestJson?回到数百一些查询和多余的文字,这些元素加起来。
全部code:
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Runtime.Serialization;
使用System.Runtime.Serialization.Json;
命名空间TestJson
{
类节目
{
静态无效的主要(字串[] args)
{
成人父=新成人{名称=约翰,年龄= 42};
MemoryStream的流1 =新的MemoryStream();
DataContractJsonSerializer SER =新DataContractJsonSerializer(typeof运算(成人));
ser.WriteObject(STREAM1,父);
stream1.Position = 0;
StreamReader的SR =新的StreamReader(流1);
Console.Write(成人JSON形式的无子女:);
Console.WriteLine(sr.ReadToEnd());
孩童=新的儿童{名称=简,年龄= 4,手指= 10};
流1 =新的MemoryStream();
SER =新DataContractJsonSerializer(typeof运算(儿童));
ser.WriteObject(STREAM1,子女);
stream1.Position = 0;
SR =新的StreamReader(流1);
Console.Write(JSON形式儿童没有父母:);
Console.WriteLine(sr.ReadToEnd());
现在//连接两个
parent.children.Add(子);
流1 =新的MemoryStream();
SER =新DataContractJsonSerializer(typeof运算(成人));
ser.WriteObject(STREAM1,父);
stream1.Position = 0;
SR =新的StreamReader(流1);
Console.Write(JSON成人的形式与1名儿童:);
Console.WriteLine(sr.ReadToEnd());
}
}
[DataContract]
[KnownType(typeof运算(成人))]
[KnownType(typeof运算(儿童))]
类Person
{
[数据成员]
内部字符串名称;
[数据成员]
内部INT年龄;
}
[DataContract]
类成人:人
{
[数据成员]
内部列表<人>孩子=新的名单,其中,人物>();
}
[DataContract]
类儿童:人
{
[数据成员]
内部INT手指;
}
}
解决方案
正如我在最后一个问题跟你说,我不知道,但一些研究使我相信,以下可能实现你想要的:
VAR设置=新DataContractJsonSerializerSettings();
settings.EmitTypeInformation = EmitTypeInformation.Never;
无功序列化=新DataContractJsonSerializer(yourType,设置);
I need to add KnownType to the below code for it to serialize successfully. When I do, the generated JSON is as follows:
JSON form of Adult with 1 child: {"age":42,"name":"John","children":[{"__type":"
Child:#TestJson","age":4,"name":"Jane","fingers":10}]}
How do I have it not include the "__type":"Child:#TestJson"? We return hundreds of these elements on some queries and that extra text will add up.
Full code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
namespace TestJson
{
class Program
{
static void Main(string[] args)
{
Adult parent = new Adult {name = "John", age = 42};
MemoryStream stream1 = new MemoryStream();
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Adult));
ser.WriteObject(stream1, parent);
stream1.Position = 0;
StreamReader sr = new StreamReader(stream1);
Console.Write("JSON form of Adult with no children: ");
Console.WriteLine(sr.ReadToEnd());
Child child = new Child { name = "Jane", age = 4, fingers=10 };
stream1 = new MemoryStream();
ser = new DataContractJsonSerializer(typeof(Child));
ser.WriteObject(stream1, child);
stream1.Position = 0;
sr = new StreamReader(stream1);
Console.Write("JSON form of Child with no parent: ");
Console.WriteLine(sr.ReadToEnd());
// now connect the two
parent.children.Add(child);
stream1 = new MemoryStream();
ser = new DataContractJsonSerializer(typeof(Adult));
ser.WriteObject(stream1, parent);
stream1.Position = 0;
sr = new StreamReader(stream1);
Console.Write("JSON form of Adult with 1 child: ");
Console.WriteLine(sr.ReadToEnd());
}
}
[DataContract]
[KnownType(typeof(Adult))]
[KnownType(typeof(Child))]
class Person
{
[DataMember]
internal string name;
[DataMember]
internal int age;
}
[DataContract]
class Adult : Person
{
[DataMember]
internal List<Person> children = new List<Person>();
}
[DataContract]
class Child : Person
{
[DataMember]
internal int fingers;
}
}
解决方案
As I told you in the last question, I don't know, but some research leads me to believe that the following might achieve what you want:
var settings = new DataContractJsonSerializerSettings();
settings.EmitTypeInformation = EmitTypeInformation.Never;
var serializer = new DataContractJsonSerializer(yourType, settings);
这篇关于我该如何告诉DataContractJsonSerializer不包括&QUOT; __类型&QUOT;属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文