// Copyright (c) Cloud Native Foundation. // Licensed under the Apache 2.0 license. // See LICENSE file in the project root for full license information. using Avro.Generic; using Avro.IO; using CloudNative.CloudEvents.Avro.Interfaces; using System; using System.IO; namespace CloudNative.CloudEvents.Avro; /// /// The default implementation of the . /// /// /// Makes use of the Avro and /// together with the embedded Avro schema. /// internal sealed class BasicGenericRecordSerializer : IGenericRecordSerializer { private readonly DefaultReader avroReader; private readonly DefaultWriter avroWriter; public BasicGenericRecordSerializer() { avroReader = new DefaultReader(AvroEventFormatter.AvroSchema, AvroEventFormatter.AvroSchema); avroWriter = new DefaultWriter(AvroEventFormatter.AvroSchema); } /// public ReadOnlyMemory Serialize(GenericRecord record) { var memStream = new MemoryStream(); var encoder = new BinaryEncoder(memStream); avroWriter.Write(record, encoder); return memStream.ToArray(); } /// public GenericRecord Deserialize(Stream rawMessagebody) { var decoder = new BinaryDecoder(rawMessagebody); // The reuse parameter *is* allowed to be null... return avroReader.Read(reuse: null!, decoder); } }