Avro.Net序列化程序忽略属性

本文关键字:属性 程序 Net 序列化 Avro | 更新日期: 2023-09-27 18:28:25

我正在为Avro使用.Net库我在C#中有下一个类

namespace Test.Avro.Model
{
    [DataContract(Name = "SensorDataValue", Namespace = "Sensors")]
    public class TestNm
    {
        [DataMember(Name = "name")]
        public string name { get; set; }
        [DataMember(Name = "surname", IsRequired = true)] //test to see if IsRequired works
        public string surname { get; set; }
        [DataMember(Name = "country", IsRequired = false)]  //test to see if IsRequired works
        public string country { get; set; }   
    }
}

当我使用SequentialWriter将类编写为avro文件时,由于使用对象容器文件进行序列化和使用反射进行序列化(http://azure.microsoft.com/en-us/documentation/articles/hdinsight-dotnet-avro-serialization/)我在avro文件中得到以下json

Objavro.codecdeflateavro.schemaÆ{
  "type": "record",
  "name": "Sensors.SensorDataValue",
  "fields": [
    {
      "name": "name",
      "type": "string"
    },
    {
      "name": "surname",
      "type": "string"
    },
    {
      "name": "country",
      "type": "string"
    }
  ]
} ...

我想要的是

Objavro.codecdeflateavro.schemaÆ{
  "type": "record",
  "name": "Sensors.SensorDataValue",
  "fields": [
    {
      "name": "name",
      "type": "string"
    },
    {
      "name": "surname",
      "type": [
               "string",
                "null"
              ]
    },
    {
      "name": "country",
      "type": [
               "string",
                "null"
              ]
    }
  ]
}...

非常感谢你的帮助。附言:我能找到的唯一相关信息是https://hadoopsdk.codeplex.com/workitem/53

Avro.Net序列化程序忽略属性

您可以通过添加属性[NullableSchema](在Avro库中定义)使属性可以为null。Avro库将忽略DataMember中的IsRequired值。