2

確かに、Windows上のVisualStudioでコードを試してみました。

EmitDefaultValueモノフレームワークは、の引数を尊重していないようDataMemberAttributeです。次のコードを使用します。

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!";
        }
    }
}

Monoでの出力は次のようになります。

{"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のバグですか、それとも何かが足りませんか?

4

1 に答える 1

4

どうやら、この機能は mono にはまだ実装されていないようです。mono ソース コードの FIXME コメント(197 行目)を参照してください。

于 2012-11-21T13:47:30.967 に答える