当前位置: 首页 > 面试题库 >

如何告诉DataContractJsonSerializer不包含“ __type”属性

卫焕
2023-03-14
问题内容

我需要将KnownType添加到以下代码中,以便成功进行序列化。当我这样做时,生成的JSON如下:

JSON form of Adult with 1 child: {"age":42,"name":"John","children":[{"__type":"
Child:#TestJson","age":4,"name":"Jane","fingers":10}]}

我如何不包含“ __type”:“ Child:#TestJson”?在某些查询中,我们返回了数百个这些元素,这些额外的文本将加在一起。

完整代码:

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;
    }
}

问题答案:

正如我在上一个问题中告诉您的那样,我不知道,但是一些研究使我相信以下内容可能会达到您想要的目标:

var settings = new DataContractJsonSerializerSettings();
settings.EmitTypeInformation = EmitTypeInformation.Never;

var serializer = new DataContractJsonSerializer(yourType, settings);


 类似资料:
  • 问题内容: 我有一个click事件发生在我的自定义指令范围之外,因此,我使用jQuery.click()侦听器并在我的范围内调用一个函数,而不是使用“ ng- click”属性,如下所示: close()是一个简单的函数,如下所示: 在我看来,我有一个绑定到isOpen的“ ng-show”元素,如下所示: 调试时,我发现正在调用close(),isOpen被更新为false,但是AngularJ

  • 我希望创建的XML在头元素中具有xsi: noNamespaceSchemaPlace和xmls: xsi属性(xsi: noNamespaceSchemaPlace="Something.xsd"xmls: xsi="http://www.w3.org/2001/XMLSchema-instance")。以任何方式使用模式——只存储这些属性和值。 对此有具体的注释吗?我可以创建一个恒定的XmlA

  • 我使用Apache Maven来管理我的Java库(jar)和一些使用这些库的项目。为了方便起见,我使用生成Eclipse项目文件,这些文件可以导入Eclipse工作区进行编辑。 当我在同一个Eclipse工作区中编辑主项目和Java库项目时会出现问题。也就是说,在文件中包含了src路径依赖项,而不是预期的依赖项。 和工作完美,其中是从我的repo自动下载的。但是,当尝试时,我发现生成的文件没有像

  • 问题内容: 我想用Cython包装一个包含C ++和OpenMP代码的测试项目,并通过文件与distutils一起构建它。我文件的内容如下所示: 该标志与gcc一起用于针对OpenMP进行编译和链接。但是,如果我只是调用 由于编译器是clang,因此无法识别此标志: 我尝试指定gcc失败: 如何告诉distutils使用gcc? 问题答案: 尝试使用os.environ从setup.py内部设置“

  • 我想用Cython包装一个包含C和OpenMP代码的测试项目,并通过一个文件用distutils构建它。我的文件内容是这样的: 标志与gcc一起用于编译和链接OpenMP。然而,如果我只是调用 无法识别此标志,因为编译器为clang: 我尝试指定gcc失败: 如何告诉distutils使用gcc?

  • 我对编程很陌生。我想检查字符串s是否包含a-z字符。我使用: 但有没有办法用更短的代码来完成这一点?非常感谢