尝试在Windows上的Visual Studio中的代码以确保。单声道不尊重System.Runtime.Serialization.DataMemberAttribute EmitDefaultValue设置
单声道框架似乎不兑现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属性被导出为空,但不应该是输出,因为它是string
类型的默认值。
正确的输出是:
{"out_value":"This is a what?","set_on_serialize":"A brick!"}
这是单声道的错误还是我失去了一些东西?
在http://www.mono-project.com/Bugs – pylover
上报告此错误另外,请在您的问题中包括版本号,特别是在指出可能的错误时。 – skolima
已记录。谢谢。 – Russell