/// <summary> /// Serialize an instance of type <typeparamref name="T"/> to a byte array in Avro format. The serialized /// data is preceded by a "magic byte" (1 byte) and the id of the schema as registered /// in Confluent's Schema Registry (4 bytes, network byte order). This call may block or throw /// on first use for a particular topic during schema registration. /// </summary> /// <param name="value"> /// The value to serialize. /// </param> /// <param name="context"> /// Context relevant to the serialize operation. /// </param> /// <returns> /// A <see cref="System.Threading.Tasks.Task" /> that completes with /// <paramref name="value" /> serialized as a byte array. /// </returns> public async Task <byte[]> SerializeAsync(T value, SerializationContext context) { try { // null needs to treated specially since the client most likely just wants to send // an individual null value instead of making the subject a null type. Also, null in // Kafka has a special meaning for deletion in a topic with the compact retention policy. // Therefore, we will bypass schema registration and return a null value in Kafka, instead // of an Avro encoded null. if (value == null && typeof(T) != typeof(Null)) { return(null); } if (serializerImpl == null) { serializerImpl = typeof(T) == typeof(GenericRecord) ? (IAvroSerializerImpl <T>) new GenericSerializerImpl(schemaRegistryClient, autoRegisterSchema, normalizeSchemas, useLatestVersion, initialBufferSize, subjectNameStrategy) : new SpecificSerializerImpl <T>(schemaRegistryClient, autoRegisterSchema, normalizeSchemas, useLatestVersion, initialBufferSize, subjectNameStrategy); } return(await serializerImpl.Serialize(context.Topic, value, context.Component == MessageComponentType.Key).ConfigureAwait(continueOnCapturedContext: false)); } catch (AggregateException e) { throw e.InnerException; } }
/// <summary> /// Serialize an instance of type <typeparamref name="T"/> to a byte array in avro format. The serialized /// data is preceeded by a "magic byte" (1 byte) and the id of the schema as registered /// in Confluent's Schema Registry (4 bytes, network byte order). This call may block or throw /// on first use for a particular topic during schema registration. /// </summary> /// <param name="topic"> /// The topic associated wih the data. /// </param> /// <param name="data"> /// The object to serialize. /// </param> /// <returns> /// <paramref name="data" /> serialized as a byte array. /// </returns> public byte[] Serialize(string topic, T data) { if (serializerImpl == null) { serializerImpl = typeof(T) == typeof(GenericRecord) ? (IAvroSerializerImpl <T>) new GenericSerializerImpl(schemaRegistryClient, autoRegisterSchema, initialBufferSize, isKeySerializer) : new SpecificSerializerImpl <T>(schemaRegistryClient, autoRegisterSchema, initialBufferSize, isKeySerializer); } return(serializerImpl.Serialize(topic, data)); }
/// <summary> /// Serialize an instance of type <typeparamref name="T"/> to a byte array in Avro format. The serialized /// data is preceeded by a "magic byte" (1 byte) and the id of the schema as registered /// in Confluent's Schema Registry (4 bytes, network byte order). This call may block or throw /// on first use for a particular topic during schema registration. /// </summary> /// <param name="value"> /// The value to serialize. /// </param> /// <param name="context"> /// Context relevant to the serialize operation. /// </param> /// <returns> /// A <see cref="System.Threading.Tasks.Task" /> that completes with /// <paramref name="value" /> serialized as a byte array. /// </returns> public async Task <byte[]> SerializeAsync(T value, SerializationContext context) { try { if (serializerImpl == null) { serializerImpl = typeof(T) == typeof(GenericRecord) ? (IAvroSerializerImpl <T>) new GenericSerializerImpl(schemaRegistryClient, autoRegisterSchema, initialBufferSize) : new SpecificSerializerImpl <T>(schemaRegistryClient, autoRegisterSchema, initialBufferSize); } return(await serializerImpl.Serialize(context.Topic, value, context.Component == MessageComponentType.Key)); } catch (AggregateException e) { throw e.InnerException; } }
/// <summary> /// Serialize an instance of type <typeparamref name="T"/> to a byte array in Avro format. The serialized /// data is preceded by a "magic byte" (1 byte) and the id of the schema as registered /// in Confluent's Schema Registry (4 bytes, network byte order). This call may block or throw /// on first use for a particular topic during schema registration. /// </summary> /// <param name="value"> /// The value to serialize. /// </param> /// <param name="context"> /// Context relevant to the serialize operation. /// </param> /// <returns> /// A <see cref="System.Threading.Tasks.Task" /> that completes with /// <paramref name="value" /> serialized as a byte array. /// </returns> public async Task <byte[]> SerializeAsync(T value, SerializationContext context) { try { if (serializerImpl == null) { serializerImpl = typeof(T) == typeof(GenericRecord) ? (IAvroSerializerImpl <T>) new GenericSerializerImpl(schemaRegistryClient, autoRegisterSchema, useLatestVersion, initialBufferSize, subjectNameStrategy) : new SpecificSerializerImpl <T>(schemaRegistryClient, autoRegisterSchema, useLatestVersion, initialBufferSize, subjectNameStrategy); } return(await serializerImpl.Serialize(context.Topic, value, context.Component == MessageComponentType.Key).ConfigureAwait(continueOnCapturedContext: false)); } catch (AggregateException e) { throw e.InnerException; } }
/// <summary> /// Serialize an instance of type <typeparamref name="T"/> to a byte array in Avro format. The serialized /// data is preceeded by a "magic byte" (1 byte) and the id of the schema as registered /// in Confluent's Schema Registry (4 bytes, network byte order). This call may block or throw /// on first use for a particular topic during schema registration. /// </summary> /// <param name="value"> /// The value to serialize. /// </param> /// <param name="messageMetadata"> /// Properties of the message the data is associated with in /// addition to the key or value. /// </param> /// <param name="destination"> /// The TopicPartition to which the message is to be sent /// (partition may be Partition.Any). /// </param> /// <param name="isKey"> /// True if deserializing the message key, false if deserializing the /// message value. /// <returns> /// A <see cref="System.Threading.Tasks.Task" /> that completes with /// <paramref name="value" /> serialized as a byte array. /// </returns> public async Task <byte[]> SerializeAsync(T value, bool isKey, MessageMetadata messageMetadata, TopicPartition destination) { try { if (serializerImpl == null) { serializerImpl = typeof(T) == typeof(GenericRecord) ? (IAvroSerializerImpl <T>) new GenericSerializerImpl(schemaRegistryClient, autoRegisterSchema, initialBufferSize) : new SpecificSerializerImpl <T>(schemaRegistryClient, autoRegisterSchema, initialBufferSize); } return(await serializerImpl.Serialize(destination.Topic, value, isKey)); } catch (AggregateException e) { throw new SerializationException("Error occured serializing Avro data.", e.InnerException); } catch (Exception e) { throw new SerializationException("Error occured serializing Avro data.", e); } }