-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAvroExtensions.cs
52 lines (46 loc) · 1.89 KB
/
AvroExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System.IO;
using System.Text;
using Avro;
using Avro.Generic;
using Avro.IO;
namespace Snd.Sdk.Storage.Streaming.MessageProtocolExtensions;
/// <summary>
/// Convert AVRO messages from and to json.
/// </summary>
public static class AvroExtensions
{
/// <summary>
/// Converts Avro-encoded data to JSON format.
/// </summary>
/// <param name="avroBytes">The Avro-encoded data to convert.</param>
/// <param name="schema">The schema used to encode the data.</param>
/// <param name="includeNamespace">Whether to include the namespace in the JSON output.</param>
/// <returns>The JSON representation of the Avro-encoded data.</returns>
public static string AvroToJson(byte[] avroBytes, Schema schema, bool includeNamespace)
{
var reader = new GenericDatumReader<object>(schema, schema);
var decoder = new BinaryDecoder(new MemoryStream(avroBytes));
var datum = reader.Read(null, decoder);
return DatumToJson(datum, schema, includeNamespace);
}
/// <summary>
/// Converts a datum encoded with the specified schema to JSON format.
/// </summary>
/// <param name="datum">The datum to convert.</param>
/// <param name="schema">The schema used to encode the datum.</param>
/// <param name="includeNamespace">Whether to include the namespace in the JSON output.</param>
/// <returns>The JSON representation of the datum.</returns>
public static string DatumToJson(object datum, Schema schema, bool includeNamespace)
{
var writer = new GenericDatumWriter<object>(schema);
var output = new MemoryStream();
var encoder = new JsonEncoder(schema, output)
{
IncludeNamespace = includeNamespace
};
writer.Write(datum, encoder);
encoder.Flush();
output.Flush();
return Encoding.UTF8.GetString(output.ToArray());
}
}