Skip to content

Commit

Permalink
Improved DeserializeWithHeaders when the input is null (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
masesdevelopers authored Oct 5, 2023
1 parent b8324e5 commit c555eec
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/net/KNet.Serialization.Avro/AvroSerDes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/

using Org.Apache.Kafka.Common.Header;
using static System.Net.WebRequestMethods;

namespace MASES.KNet.Serialization.Avro
{
Expand All @@ -31,14 +30,25 @@ public class AvroSerDes<T> : KNetSerDes<T>
/// The extension uses <see cref="Headers"/>
/// </summary>
public override bool UseHeaders => true;
/// <inheritdoc cref="KNetSerDes{T}.Serialize(string, T)"/>
public override byte[] Serialize(string topic, T data)
{
return SerializeWithHeaders(topic, null, data);
}
/// <inheritdoc cref="KNetSerDes{T}.SerializeWithHeaders(string, Headers, T)"/>
public override byte[] SerializeWithHeaders(string topic, Headers headers, T data)
{
throw new System.NotImplementedException();
}
/// <inheritdoc cref="KNetSerDes{T}.Deserialize(string, byte[])"/>
public override T Deserialize(string topic, byte[] data)
{
return DeserializeWithHeaders(topic, null, data);
}
/// <inheritdoc cref="KNetSerDes{T}.DeserializeWithHeaders(string, Headers, byte[])"/>
public override T DeserializeWithHeaders(string topic, Headers headers, byte[] data)
{
if (data == null) return default;
throw new System.NotImplementedException();
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/net/KNet.Serialization.Json/JsonSerDes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public class JsonSerDes<T> : KNetSerDes<T>
/// The extension uses <see cref="Headers"/>
/// </summary>
public override bool UseHeaders => true;
/// <inheritdoc cref="KNetSerDes{T}.Serialize(string, T)"/>
public override byte[] Serialize(string topic, T data)
{
return SerializeWithHeaders(topic, null, data);
}
/// <inheritdoc cref="KNetSerDes{T}.SerializeWithHeaders(string, Headers, T)"/>
public override byte[] SerializeWithHeaders(string topic, Headers headers, T data)
{
Expand All @@ -42,9 +47,15 @@ public override byte[] SerializeWithHeaders(string topic, Headers headers, T dat
return Encoding.UTF8.GetBytes(jsonStr);
#endif
}
/// <inheritdoc cref="KNetSerDes{T}.Deserialize(string, byte[])"/>
public override T Deserialize(string topic, byte[] data)
{
return DeserializeWithHeaders(topic, null, data);
}
/// <inheritdoc cref="KNetSerDes{T}.DeserializeWithHeaders(string, Headers, byte[])"/>
public override T DeserializeWithHeaders(string topic, Headers headers, byte[] data)
{
if (data == null) return default;
#if NET462_OR_GREATER
var jsonStr = Encoding.UTF8.GetString(data);
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(jsonStr);
Expand Down
11 changes: 11 additions & 0 deletions src/net/KNet.Serialization.MessagePack/MessagePackSerDes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,25 @@ public class MessagePackSerDes<T> : KNetSerDes<T>
/// The extension uses <see cref="Headers"/>
/// </summary>
public override bool UseHeaders => true;
/// <inheritdoc cref="KNetSerDes{T}.Serialize(string, T)"/>
public override byte[] Serialize(string topic, T data)
{
return SerializeWithHeaders(topic, null, data);
}
/// <inheritdoc cref="KNetSerDes{T}.SerializeWithHeaders(string, Headers, T)"/>
public override byte[] SerializeWithHeaders(string topic, Headers headers, T data)
{
return MessagePackSerializer.Serialize(data);
}
/// <inheritdoc cref="KNetSerDes{T}.Deserialize(string, byte[])"/>
public override T Deserialize(string topic, byte[] data)
{
return DeserializeWithHeaders(topic, null, data);
}
/// <inheritdoc cref="KNetSerDes{T}.DeserializeWithHeaders(string, Headers, byte[])"/>
public override T DeserializeWithHeaders(string topic, Headers headers, byte[] data)
{
if (data == null) return default;
using (MemoryStream stream = new MemoryStream(data))
{
return MessagePackSerializer.Deserialize<T>(stream);
Expand Down
11 changes: 11 additions & 0 deletions src/net/KNet.Serialization.Protobuf/ProtobufSerDes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public class ProtobufSerDes<T> : KNetSerDes<T>
/// The extension uses <see cref="Headers"/>
/// </summary>
public override bool UseHeaders => true;
/// <inheritdoc cref="KNetSerDes{T}.Serialize(string, T)"/>
public override byte[] Serialize(string topic, T data)
{
return SerializeWithHeaders(topic, null, data);
}
/// <inheritdoc cref="KNetSerDes{T}.SerializeWithHeaders(string, Headers, T)"/>
public override byte[] SerializeWithHeaders(string topic, Headers headers, T data)
{
Expand All @@ -43,9 +48,15 @@ public override byte[] SerializeWithHeaders(string topic, Headers headers, T dat
return stream.ToArray();
}
}
/// <inheritdoc cref="KNetSerDes{T}.Deserialize(string, byte[])"/>
public override T Deserialize(string topic, byte[] data)
{
return DeserializeWithHeaders(topic, null, data);
}
/// <inheritdoc cref="KNetSerDes{T}.DeserializeWithHeaders(string, Headers, byte[])"/>
public override T DeserializeWithHeaders(string topic, Headers headers, byte[] data)
{
if (data == null) return default;
return _parser.ParseFrom(data);
}
}
Expand Down

0 comments on commit c555eec

Please sign in to comment.