Bug 8605 - Framework 3.0.1 does not honor EmitDefaultValue member of DataMemberAttribute
Summary: Framework 3.0.1 does not honor EmitDefaultValue member of DataMemberAttribute
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: System (show other bugs)
Version: unspecified
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Miguel de Icaza [MSFT]
URL:
Depends on:
Blocks:
 
Reported: 2012-11-26 17:09 UTC by Russell
Modified: 2013-06-10 13:10 UTC (History)
4 users (show)

See Also:
Tags:
Is this bug a regression?: ---
Last known good build:


Attachments

Description Russell 2012-11-26 17:09:46 UTC
Description of Problem:
Setting the EmitDefaultValue argument of System.Runtime.Serialization.DataMemberAttribute on a class property does not prevent Mono from serializing the property if it is the default value for that type.

Steps to reproduce the problem:
1. Create a class and add the DataContract attribute to it
2. Add a string property and set attribute [DataMember(EmitDefaultValue=false)]
3. Create a function and create a new instance of your class type
4. DO NOT set the value of the string property
5. Serialize the object it using DataContractJsonSerializer


Actual Results:
Property is created in JSON output for the class with the value ""

Expected Results:
Property is excluded from JSON output

How often does this happen? 
Always

Sample Code:

using System;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;

namespace MyApp
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            Cereal specialK = new Cereal();

            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Cereal));

            specialK.TheValue="This is a what?";

            MemoryStream stm = new MemoryStream();
            ser.WriteObject(stm, specialK);
            string json = System.Text.Encoding.UTF8.GetString(stm.ToArray());

            Console.WriteLine(json);
            Console.ReadLine();

        }
    }

    [DataContract]
    class Cereal
    {
        [DataMember(Name="set_on_serialize")]
        private string _setOnSerialize = string.Empty;

        [DataMember(Name = "default_export", EmitDefaultValue = false)]
        private string _default_null;

        public Cereal() { }

        [DataMember(Name = "out_value")]
        public string TheValue
        {
            get;
            set;
        }

        [OnSerializing]
        void OnSerializing(StreamingContext content)
        {
            this._setOnSerialize = "A brick!";
        }
    }
}

Note You need to log in before you can comment on or make changes to this bug.