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)

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

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report for Bug 8605 on GitHub or Developer Community if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: GitHub Markdown or Developer Community HTML
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
NEW

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