C#将AVRO转换为JSON



是否有C#实现来转换AVRO反序列化

<GenericRecord>

返回到常规JSON格式?我已经成功地阅读了Kafka中的主题,并使用了AvroDeserializer,但我想将其转换为JSON格式。

以下是一个示例:简单JSON:

{       
"value": {
"eventCreatedTimestamp": 1588694347577,
"iouDecision": "PLANE",
"iouDecisionReason": {
"value": "Optimal"
}
}
}

与AVRO反序列化的Generic相同。记录:

{Schema: 
{"type":"record","name":"TheCustomerDecisionEvent",
"namespace":"com.farmxxx.tst.property.service.options.iou.messaging.avro","fields":
[{"name":"eventCreatedTimestamp","type":["null","long"]},
{"name":"iouDecision","type":["null",{"type":"enum",
"name":"TheType",
"namespace":"com.farmxxx.tst.property.service.options.iou.messaging.avro",
"symbols":["HRMR","FIELD","PLANE"]}]},
{"name":"iouDecisionReason","type":["null",{"type":"record","name":"iouDecisionReason",
"namespace":"com.farmxxx.tst.property.service.options.iou.messaging.avro",
"fields":[{"name":"value","type":["null","string"]}]}]}]}
,
contents: { eventCreatedTimestamp: 1588694347577, 
iouDecision: Schema: {"type":"enum","name":"TheType",
"namespace":"com.farmxxx.tst.property.service.options.iou.messaging.avro",
"symbols":["HRMR","FIELD","PLANE"]}, value: PLANE,
iouDecisionReason: Schema: {"type":"record","name":"iouDecisionReason",
"namespace":"com.farmxxx.tst.property.service.options.iou.messaging.avro",
"fields":[{"name":"value","type":["null","string"]}]}, contents: { value: Optimal, }, }}

因此,任务是从AVRO Generic.Record.返回到Simple JSON

可以使用以下方法找到解决方案:

https://github.com/AdrianStrugala/AvroConvert

Avro2Json功能。

有了这个,你可以从Kafka中读取一个主题,并将其翻译成简单的JSON。我使用这个简单的JSON传递给其他不想知道AVRO记录格式等的客户。

非常感谢AvroConvert Github存储库的作者。

正如评论中所建议的,我使用了https://github.com/kristofferjalen/AvroConverter过去(我与该项目没有任何关系(。

该页面的自述文件表明它非常容易使用。只需在package Manager命令行上输入Install-Package AvroConverter(如果不使用Visual Studio,则输入CLI(,即可从NuGet安装其程序包,然后从代码中使用以下命令调用它:

var path = @"C:youravrofilepath.avro";
var fileInfo = new System.IO.FileInfo(path);
var json = AvroConvert.ToJson(fileInfo);

最新更新