Mono不支持System.Runtime.Serialization.DataMemberAttribute EmitDefaultValue设置

时间:2012-11-20 22:14:36

标签: c# json serialization mono portability

在Windows上尝试使用Visual Studio中的代码。

Mono框架似乎不尊重EmitDefaultValue的{​​{1}}参数。使用以下代码:

DataMemberAttribute

Mono中的输出结果为:

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但不应输出,因为它是类型{"default_export":null,"out_value":"This is a what?","set_on_serialize":""} 的默认值。

Windows上VS的正确输出是:

string

这是Mono中的错误还是我错过了什么?

1 个答案:

答案 0 :(得分:4)

显然此功能尚未在单声道中实现。请参阅FIXME comment in the mono source code(第197行)。