/// <summary> /// Populate the correct types in components instead of just the BaseComponent /// </summary> /// <param name="reader"></param> /// <param name="objectType"></param> /// <param name="existingValue"></param> /// <param name="serializer"></param> /// <returns></returns> /// <exception cref="ArgumentOutOfRangeException"></exception> public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JArray array = JArray.Load(reader); if (!(existingValue is List<BaseComponent> components)) { components = new List<BaseComponent>(); } foreach (JToken token in array) { MessageComponentType type = (MessageComponentType)Enum.Parse(typeof(MessageComponentType), token["type"].ToString()); switch (type) { case MessageComponentType.Button: components.Add(token.ToObject<ButtonComponent>()); break; case MessageComponentType.SelectMenu: components.Add(token.ToObject<SelectMenuComponent>()); break; } } return components; }
private async ValueTask <byte[]?> SerializeAsync <TValue>( object?message, MessageComponentType componentType, MessageSerializationContext context) { if (message == null) { return(null); } if (message is Stream inputStream) { return(await inputStream.ReadAllAsync().ConfigureAwait(false)); } if (message is byte[] inputBytes) { return(inputBytes); } return(await new AvroSerializer <TValue>( SchemaRegistryClientFactory.GetClient(SchemaRegistryConfig), AvroSerializerConfig) .SerializeAsync( (TValue)message, GetConfluentSerializationContext(componentType, context)) .ConfigureAwait(false)); }
private async Task <TValue?> DeserializeAsync <TValue>( byte[]?message, MessageComponentType componentType, MessageSerializationContext context) where TValue : class { if (message == null) { return(null); } var avroDeserializer = new AvroDeserializer <TValue>( SchemaRegistryClientFactory.GetClient(SchemaRegistryConfig), AvroSerializerConfig); var confluentSerializationContext = GetConfluentSerializationContext(componentType, context); return(await avroDeserializer.DeserializeAsync( new ReadOnlyMemory <byte>(message), false, confluentSerializationContext) .ConfigureAwait(false)); }
private static SerializationContext GetConfluentSerializationContext( MessageComponentType componentType, MessageSerializationContext context) =>
/// <summary> /// Create a new SerializationContext object instance. /// </summary> /// <param name="component"> /// The component of the message the serialization operation relates to. /// </param> /// <param name="topic"> /// The topic the data is being written to or read from. /// </param> public SerializationContext(MessageComponentType component, string topic) { Component = component; Topic = topic; }
/// <summary> /// Create a new SerializationContext object instance. /// </summary> /// <param name="component"> /// The component of the message the serialization operation relates to. /// </param> /// <param name="topic"> /// The topic the data is being written to or read from. /// </param> /// <param name="headers"> /// The collection of message headers (or null). Specifying null or an /// empty list are equivalent. The order of headers is maintained, and /// duplicate header keys are allowed. /// </param> public SerializationContext(MessageComponentType component, string topic, Headers headers = null) { Component = component; Topic = topic; Headers = headers; }
private static SerializationContext GetConfluentSerializationContext( MessageComponentType componentType, MessageSerializationContext context) => new SerializationContext(componentType, context.ActualEndpointName);