Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a C# model be serialized as an AVRO JSON Schema?

I have found some code here https://docs.microsoft.com/en-us/azure/hdinsight/hdinsight-dotnet-avro-serialization#Scenario2 that does the reverse of what I need:

//Define the schema in JSON
const string Schema = @"{
    ""type"":""record"",
    ""name"":""Microsoft.Hadoop.Avro.Specifications.SensorData"",
    ""fields"":
        [
            {
                ""name"":""Location"",
                ""type"":
                    {
                        ""type"":""record"",
                        ""name"":""Microsoft.Hadoop.Avro.Specifications.Location"",
                        ""fields"":
                            [
                                { ""name"":""Floor"", ""type"":""int"" },
                                { ""name"":""Room"", ""type"":""int"" }
                            ]
                    }
            },
            { ""name"":""Value"", ""type"":""bytes"" }
        ]
}";

//Create a generic serializer based on the schema
var serializer = AvroSerializer.CreateGeneric(Schema);

I would like to take a model that I have created:

[DataContract(Name = "Demo", Namespace = "pubsub.demo")]
public class Demo
{
    [DataMember(Name = "value")]
    public long Value { get; set; }
}

...and serialize this C# model into a JSON AVRO Schema string.

Reason:

I only want to maintain C# models and automatically register these models with Confluent's Schema Registry. To register with schema registry the schema needs to be in a JSON AVRO format (Just like Schema above).

I would prefer not to have both the JSON defined and the C# model. If I had to maintian one, I would prefer to have a C# model.

like image 201
joelnet Avatar asked Jul 26 '17 23:07

joelnet


1 Answers

I found what I was looking for in Microsoft.Hadoop.Avro.AvroSerializer.

AvroSerializer.Create<Demo>().WriterSchema.ToString();
// > {"type":"record","name":"pubsub.demo.Demo","fields"[{"name":"value","type":"long"}]}
like image 54
joelnet Avatar answered Sep 19 '22 00:09

joelnet