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)

See Also:
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

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

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