>在Windows上的Visual Studio中尝试了代码以确保。
Mono框架似乎不尊重DataMemberAttribute
的EmitDefaultValue
论点。使用以下代码:
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();
specialK.TheValue="This is a what?";
var ser = new DataContractJsonSerializer(typeof(Cereal));
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!";
}
}
}
单声道输出的结果是:
{"default_export":null,"out_value":"This is a what?","set_on_serialize":""}
default_export 属性将导出为 null,但不应输出,因为它是类型 string
的默认值。
Windows 上的 VS 的正确输出是:
{"out_value":"This is a what?","set_on_serialize":"A brick!"}
这是 Mono 中的错误还是我错过了什么?
显然,此功能尚未在单声道中实现。请参阅单声道源代码中的 FIXME 注释(第 197 行)。