Avro.Cadabra.Core
1.0.1.2
See the version list below for details.
dotnet add package Avro.Cadabra.Core --version 1.0.1.2
NuGet\Install-Package Avro.Cadabra.Core -Version 1.0.1.2
<PackageReference Include="Avro.Cadabra.Core" Version="1.0.1.2" />
paket add Avro.Cadabra.Core --version 1.0.1.2
#r "nuget: Avro.Cadabra.Core, 1.0.1.2"
// Install Avro.Cadabra.Core as a Cake Addin #addin nuget:?package=Avro.Cadabra.Core&version=1.0.1.2 // Install Avro.Cadabra.Core as a Cake Tool #tool nuget:?package=Avro.Cadabra.Core&version=1.0.1.2
AvroCadabra
A tiny library to convert a user-defined C# object to an AvroRecord and back.
Useful when you need to use Avro to serialize your DTOs (data transfer objects) but can't afford to decorate it with attributes that may be required for Avro to work.
Another is for instances when you only want to serialize certain properties of your model which is driven by the schema provided.
Syntax:
// To convert an object instance to AvroRecord
myObject.ToAvroRecord(schema);
// To convert an AvroRecord to an object instance
avroRecord.FromAvroRecord<MyObjectClass>();
// or if you a newer schema
avroRecord.FromAvroRecord<MyObjectClass>(newerSchema);
Example:
public class ShapeBasket
{
public IList<IShape> Shapes { get; set; }
}
void main()
{
var instance = new ShapeBasket
{
Shapes = new List<IShape>
{
new Circle { Name = "Red Dot", Radius = 15, Color = BasicColor.Red },
new Square { Name = "Blue Square", Width = 20, Color = BasicColor.Blue }
}
};
var avroFile = Path.GetTempFileName();
// assuming you have a schema stored as a resource
var schema_v1 = Encoding.Default.GetString(Resources.ShapeBasket_v1_0);
// serialize to file
using (var fs = new FileStream(avroFile, FileMode.Create))
{
using var writer = AvroContainer.CreateGenericWriter(schema_v1, fs, Codec.Deflate);
using var sequentialWriter = new SequentialWriter<object>(writer, 1);
// convert the instance to an AvroRecord
sequentialWriter.Write(instance.ToAvroRecord(schema));
}
// assuming you want to deserialize using an updated schema
var schema_v2 = Encoding.Default.GetString(Resources.ShapeBasket_v2_0);
// deserialize to target type
var target = new ShapeBasket();
using (var fs = new FileStream(avroFile, FileMode.Open))
{
using var reader = AvroContainer.CreateGenericReader(fs);
using var sequentialReader = new SequentialReader<object>(reader);
// convert the AvroRecord to the actual instance
target = sequentialReader.Objects.Cast<AvroRecord>().Select(r => r.FromAvroRecord<ShapeBasket>()).FirstOrDefault();
}
}
Custom Field Processor
For instances when you need to serialize the private state of your objects (TF?! IKR?) or do pre field processing before serializing/deserializing.
Example:
using System;
using System.Reflection;
using System.Text;
using Gooseman.Avro.Utility;
using Microsoft.Hadoop.Avro.Schema;
namespace TestAvro
{
class Program
{
static void Main(string[] args)
{
var schema = "{\"type\":\"record\",\"name\":\"TestAvro.SecretMessage\",\"fields\":[{\"name\":\"_id\",\"type\":\"string\"},{\"name\":\"Message\",\"type\":\"string\"}]}";
var secretMessage = new SecretMessage { Message = "Hello There!" };
var secretMessageFieldProcessor = new SecretMessageFieldProcessor();
var avro = secretMessage.ToAvroRecord(schema, secretMessageFieldProcessor);
var secretMessageReveal = avro.FromAvroRecord<SecretMessage>(customFieldProcessor: secretMessageFieldProcessor);
Console.WriteLine($"Message: {secretMessage.Message}, Encrypted Message: {avro[1]}, Restored Message: {secretMessageReveal.Message}");
Console.ReadLine();
}
}
public class SecretMessage
{
private Guid _id = Guid.NewGuid();
public Guid Id => _id;
public string Message { get; set; }
}
public class SecretMessageFieldProcessor : BaseCustomFieldProcessor
{
public override MemberInfo GetMatchingMemberInfo<T>(RecordField recordField)
{
return recordField.Name == "_id"
? typeof(T).GetField(recordField.Name, BindingFlags.NonPublic | BindingFlags.Instance)
: base.GetMatchingMemberInfo<T>(recordField);
}
public override object PreFieldSerialization<T>(T obj, string fieldName)
{
switch (fieldName)
{
case "_id":
var id = obj.GetFieldValue<T, Guid>(fieldName);
return id;
case "Message":
var sm = ((dynamic)obj).Message;
return Convert.ToBase64String(Encoding.Default.GetBytes(sm));
default:
return base.PreFieldSerialization(obj, fieldName);
}
}
public override object PreFieldDeserialization(string fieldName, object value)
{
switch (fieldName)
{
case "Message":
return Encoding.Default.GetString(Convert.FromBase64String(value.ToString()));
default:
return base.PreFieldDeserialization(fieldName, value);
}
}
}
}
Available in Nuget
PM> Install-Package Avro.Cadabra.Core
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 is compatible. netcoreapp3.1 is compatible. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net451 is compatible. net452 was computed. net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETCoreApp 3.0
- Microsoft.Avro.Core (>= 0.1.0)
-
.NETCoreApp 3.1
- Microsoft.Avro.Core (>= 0.1.0)
-
.NETFramework 4.5.1
- Microsoft.Avro.Core (>= 0.1.0)
-
.NETStandard 2.0
- Microsoft.Avro.Core (>= 0.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.