简体   繁体   English

序列化为JSON时将忽略DataMember

[英]DataMember ignored when serializing to JSON

I have added DataMemeber to my object properties to change settings when serializing to JSON, however it is not using them. 我已将DataMemeber添加到我的对象属性中,以在序列化为JSON时更改设置,但是未使用它们。 I have attempted to change the name, as well as emitting default values. 我试图更改名称以及发出默认值。

My reason for trying to do this is I want to ignore a property if it is at its default value. 我尝试执行此操作的原因是,如果属性为默认值,则我想忽略该属性。

I am attempting to use the Microsoft libraries and not the Newtonsoft ones. 我试图使用Microsoft库而不是Newtonsoft库。

    <DataMember(EmitDefaultValue:=True, IsRequired:=False, Name:="addressTable")> Public Property addressTable() As String
        Get
            Return _AddressTable
        End Get
        Set(ByVal value As String)
            _AddressTable = value
        End Set
    End Property


Public Function gObjToStr(ByVal InputObject As Object) As String

    Dim stream1 As New IO.MemoryStream
    Dim ser As Runtime.Serialization.Json.DataContractJsonSerializer = New Runtime.Serialization.Json.DataContractJsonSerializer(InputObject.GetType)

    ser.WriteObject(stream1, InputObject)
    stream1.Position = 0

    Dim sr As New IO.StreamReader(stream1)
    Dim OutString As String = Nothing

    Return sr.ReadToEnd

End Function

You need to remove the EmitDefaultValue attribute from the Property addressTable . 您需要从属性addressTable删除EmitDefaultValue属性。

Basically, EmitDefaultValue tells the serialization engine whether to serialize the default value for a field or property being serialized . 基本上, EmitDefaultValue 告诉序列化引擎是否要序列化要序列化的字段或属性的默认值

The default value for EmitDefaultValue is true, so even if a property has a default value it would be serialized. EmitDefaultValue的默认值为true,因此即使某个属性具有默认值,它也会被序列化。 As per your requirement, if you need to ignore a property, if it has default value then you need to add attribute EmitDefaultValue to the Property and set the value as false, [DataMember(EmitDefaultValue =false)] . 根据您的要求,如果需要忽略属性,如果它具有默认值,则需要向属性添加属性EmitDefaultValue并将该值设置为false [DataMember(EmitDefaultValue =false)] In the code posted above, you have set the EmitDefaultValue = true , hence it is generating the addressTable in Serialization. 在上面发布的代码中,您已设置EmitDefaultValue = true ,因此它将在序列化中生成addressTable

Also, IsRequired instructs the serialization engine that the member must be present when reading or deserializing . 同样, IsRequired 指示序列化引擎在读取或反序列化时必须存在成员 So you should be careful with usage of both Attributes EmitDefaultValue & IsRequired . 因此,应同时使用EmitDefaultValueIsRequired这两个属性。 The default value for IsRequired = false . IsRequired = false的默认值IsRequired = false So you can't have a combination like EmitDefaultValue=false and IsRequired=true , in this case Serialization engine would throw an exception. 因此,您不能使用EmitDefaultValue=falseIsRequired=true类的组合,在这种情况下,序列化引擎将引发异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM