Guille
Guille

Reputation: 39

Avro .Net Serializer ignores attributes

I am using .Net library for Avro I have the next class in 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; }   

    }
}

When I wrote the class as an avro file using SequentialWriter as stayed in Serialization using object container files and serialization with reflection( http://azure.microsoft.com/en-us/documentation/articles/hdinsight-dotnet-avro-serialization/ ) I get the following json in the avro file

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

What I would like to have is

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

Many thanks for your help. PS: the only relevant information that I could find was https://hadoopsdk.codeplex.com/workitem/53

Upvotes: 3

Views: 1832

Answers (1)

Giacomo Citi
Giacomo Citi

Reputation: 308

You can make a property nullable by adding the attribute [NullableSchema] (which is defined in the Avro library). The IsRequired value in DataMember is ignored by the Avro library.

Upvotes: 1

Related Questions