Bug 15028 - DataMember - EmitDefaultValue is not working.
Summary: DataMember - EmitDefaultValue is not working.
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System (show other bugs)
Version: 3.2.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2013-09-26 19:00 UTC by gtas
Modified: 2015-09-23 14:56 UTC (History)
7 users (show)

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


Attachments
Sample project showing the problems (14.50 KB, application/zip)
2013-09-27 09:40 UTC, gtas
Details


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 on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description gtas 2013-09-26 19:00:17 UTC
If a property decorated with [DataMember(EmitDefaultValue = false)] attribute, and using the DataContractJsonSerializer the property is still serialized to the exported JSON.
Comment 1 Rolf Bjarne Kvinge [MSFT] 2013-09-27 06:35:47 UTC
Can you please attach a self-contained test case?
Comment 2 gtas 2013-09-27 09:40:38 UTC
Created attachment 5006 [details]
Sample project showing the problems

Pressing the button with No LogData will result to a serialized string and the log_data property which is null included.
Since the EmitDefaultValue=false it shouldn't behave this way.
Comment 3 Rolf Bjarne Kvinge [MSFT] 2013-09-27 10:06:03 UTC
We have a FIXME, which is probably why this is happening:

https://github.com/mono/mono/blob/master/mcs/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/TypeMap.cs#L196
Comment 4 gtas 2013-09-27 10:14:22 UTC
So, it will be fixed in the next version release of Xamarin iOS?
Comment 5 Rolf Bjarne Kvinge [MSFT] 2013-09-27 10:22:10 UTC
No, it has not been fixed yet, so I can't say in which release a fix will be included.
Comment 7 Jon Goldberger [MSFT] 2013-11-22 20:38:35 UTC
From 52777:

DataContractJsonSerializer ignores EmitDefaultValue property of the DataMember attribute in Xamarin.Android 4.10.1. This is creating inconsistent serialization when compared to .NET 4.5. It creates issues with services that rely on JSON but does input validation and does not accept empty values (such as Azure Active Directory service) and may have a big impact.

I have included the test code below. Below are the results of serialization using Xamarin.Android 4.10.1 and .NET 4.5. You will notice the extra line that gets serialized in Xamarin.

Output - Xamarin.Android 4.10.1
-------------------
{
"PropEmitDefaultValueOffAndEmpty": "",
"PropEmitDefaultValueOffAndNull": null,
"PropEmitDefaultValueOnAndEmpty": "",
"PropEmitDefaultValueOnAndNull": null
}

Output - .NET 4.5
-------------------
{
"PropEmitDefaultValueOffAndEmpty": "",
"PropEmitDefaultValueOnAndEmpty": "",
"PropEmitDefaultValueOnAndNull": null
}

JsonTests.cs
-------------------
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;

namespace AndroidTestApp
{
[DataContract]
public class Model
{
[DataMember(EmitDefaultValue = false)]
public string PropEmitDefaultValueOffAndNull { get; private set; }

[DataMember(EmitDefaultValue = false)]
public string PropEmitDefaultValueOffAndEmpty { get; private set; }

[DataMember(EmitDefaultValue = true)]
public string PropEmitDefaultValueOnAndNull { get; private set; }

[DataMember(EmitDefaultValue = true)]
public string PropEmitDefaultValueOnAndEmpty { get; private set; }

public Model()
{
PropEmitDefaultValueOffAndEmpty = string.Empty;
PropEmitDefaultValueOnAndEmpty = string.Empty;
}
}

class JsonTest
{
public void Test()
{
var instance = new Model();
string output = Serialize(instance);
}

static public string Serialize<T>(T instance)
{
var serializer = new DataContractJsonSerializer(instance.GetType());
using (var stream = new MemoryStream())
{
serializer.WriteObject(stream, instance);
stream.Position = 0;
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
}
}
Comment 8 Alek Slater 2014-01-16 06:15:48 UTC
Rolf Bjarne Kvinge: I just submitted this pull request to mono master: https://github.com/mono/mono/pull/873

Do you think this will fix it, at least partially?

I keep bumping into this issue when I interact with JSON, and its getting a bit annoying having to work around it all the time.
Comment 9 Darrel 2014-06-11 16:59:43 UTC
I created a new pull request to mono master, with tests: https://github.com/mono/mono/pull/1096
Comment 10 Miguel de Icaza [MSFT] 2015-09-23 14:56:41 UTC
Fixed with our switch to reference source