public void Null() { var protoSerializer = new ProtobufSerializer <UInt32Value>(schemaRegistryClient); var protoDeserializer = new ProtobufDeserializer <UInt32Value>(); var bytes = protoSerializer.SerializeAsync(null, new SerializationContext(MessageComponentType.Value, testTopic)).Result; Assert.Null(bytes); Assert.Null(protoDeserializer.DeserializeAsync(bytes, true, new SerializationContext(MessageComponentType.Value, testTopic)).Result); }
public void UInt32SerDe() { var protoSerializer = new ProtobufSerializer <UInt32Value>(schemaRegistryClient); var protoDeserializer = new ProtobufDeserializer <UInt32Value>(); var v = new UInt32Value { Value = 1234 }; var bytes = protoSerializer.SerializeAsync(v, new SerializationContext(MessageComponentType.Value, testTopic)).Result; Assert.Equal(v.Value, protoDeserializer.DeserializeAsync(bytes, false, new SerializationContext(MessageComponentType.Value, testTopic)).Result.Value); }
/// <summary> /// Initialize method with a current context which contains <see cref="IStreamConfig"/>. /// Can be used to initialize the serdes according to some parameters present in the configuration such as the schema.registry.url /// </summary> /// <param name="context">SerDesContext with stream configuration</param> public override void Initialize(SerDesContext context) { if (!isInitialized) { if (context.Config is ISchemaRegistryConfig schemaConfig) { registryClient = GetSchemaRegistryClient(GetConfig(schemaConfig)); deserializer = new ProtobufDeserializer <T>(schemaConfig as Config); serializer = new ProtobufSerializer <T>(registryClient, GetSerializerConfig(schemaConfig)); isInitialized = true; } else { throw new StreamConfigException($"Configuration must inherited from ISchemaRegistryConfig for SchemaProtobufSerDes<{typeof(T).Name}"); } } }
public void Test_Untyped() { var rnd = new Random(); var record = new TestRecord { Id = Guid.NewGuid().ToString(), Integer = DateTime.UtcNow.Millisecond, Floating = rnd.NextDouble(), EnumeratedValue = MyEnum.Val2, TextValues = { rnd.Next().ToString(), rnd.Next().ToString(), rnd.Next().ToString(), rnd.Next().ToString() } }; byte[] bytes; // serialize to bytes var ser = new ProtobufSerializer(); bytes = ser.Serialize(record, default); TestRecord testing; // back to object var deSer = new ProtobufDeserializer <TestRecord>(); testing = deSer.Deserialize(bytes, false, default); Assert.AreEqual(record.Id, testing.Id); Assert.AreEqual(record.Integer, testing.Integer); Assert.AreEqual(record.Floating, testing.Floating); Assert.AreEqual(record.EnumeratedValue, testing.EnumeratedValue); Assert.AreEqual(record.TextValues.Count, testing.TextValues.Count); for (int i = 0; i < record.TextValues.Count; i++) { Assert.AreEqual(record.TextValues[i], testing.TextValues[i]); } }
private bool TryDeserialize(byte[] data, out object result) { if (_deserializeFormat.HasValue) { bool shouldLogError = _isNewFormat ?? false; switch (_deserializeFormat.Value) { case SerializationFormat.Json: return(JsonDeserializer.TryDeserialize( data, _messageType, shouldLogError ? _log : null, out result)); case SerializationFormat.MessagePack: return(MessagePackDeserializer.TryDeserialize( data, _messageType, shouldLogError ? _log : null, out result)); case SerializationFormat.Protobuf: return(ProtobufDeserializer.TryDeserialize( data, _messageType, shouldLogError ? _log : null, out result)); default: throw new NotSupportedException($"Serialization format {_deserializeFormat.Value} is not supported"); } } bool success = JsonDeserializer.TryDeserialize( data, _messageType, null, out result); if (success) { _deserializeFormat = SerializationFormat.Json; return(true); } success = MessagePackDeserializer.TryDeserialize( data, _messageType, null, out result); if (success) { _deserializeFormat = SerializationFormat.MessagePack; return(true); } success = ProtobufDeserializer.TryDeserialize( data, _messageType, null, out result); if (!success) { _log.WriteWarning(nameof(TryDeserialize), null, $"Couldn't deserialize message with length {data.Length}"); } return(success); }