-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a5d3ffb
commit 335d19f
Showing
5 changed files
with
74 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright 2023 MASES s.r.l. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* Refer to LICENSE for more information. | ||
*/ | ||
|
||
#nullable enable | ||
|
||
using Avro.IO; | ||
using Avro.Specific; | ||
using MASES.EntityFrameworkCore.KNet.Serialization.Avro.Storage; | ||
using MASES.KNet.Serialization; | ||
using Org.Apache.Kafka.Common.Header; | ||
|
||
namespace MASES.EntityFrameworkCore.KNet.Serialization.Avro; | ||
|
||
/// <summary> | ||
/// Avro Json encoder extension of <see cref="KNetSerDes{T}"/>, for example <see href="https://masesgroup.github.io/KNet/articles/usageSerDes.html"/> | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
public class KEFCoreSerDesAvroJson<T> : KNetSerDes<T> | ||
{ | ||
static readonly SpecificDefaultWriter SpecificWriter = new SpecificDefaultWriter(AvroValueContainer._SCHEMA); | ||
static readonly SpecificDefaultReader SpecificReader = new SpecificDefaultReader(AvroValueContainer._SCHEMA, AvroValueContainer._SCHEMA); | ||
|
||
/// <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) | ||
{ | ||
using MemoryStream memStream = new(); | ||
JsonEncoder encoder = new(AvroValueContainer._SCHEMA, memStream); | ||
SpecificWriter.Write(data, encoder); | ||
encoder.Flush(); | ||
return memStream.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) | ||
{ | ||
using MemoryStream memStream = new(data); | ||
JsonDecoder decoder = new(AvroValueContainer._SCHEMA, memStream); | ||
T t = (T)Activator.CreateInstance(typeof(T))!; | ||
t = SpecificReader.Read(t!, decoder); | ||
return t; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters