ChoiSerializer 0.1.5.1

.NET Standard 2.0
There is a newer version of this package available.
See the version list below for details.
dotnet add package ChoiSerializer --version 0.1.5.1
NuGet\Install-Package ChoiSerializer -Version 0.1.5.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ChoiSerializer" Version="0.1.5.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ChoiSerializer --version 0.1.5.1
#r "nuget: ChoiSerializer, 0.1.5.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ChoiSerializer as a Cake Addin
#addin nuget:?package=ChoiSerializer&version=0.1.5.1

// Install ChoiSerializer as a Cake Tool
#tool nuget:?package=ChoiSerializer&version=0.1.5.1

NuGet NuGet

ChoiSerializer

Serialization library based on data location and length for the .NET platform. This turns your instance into a lightweight binary file.

Usage

Using SDK library

Just install package in the Nuget

Install-Package ChoiSerializer

Let's getting started using the library

Create model class

Inherit the Serializable class to the target class to be serialized. Add the SerializableCulumn attribute to the property to be serialized. You can specify the length according to your needs. If the type and length of the property must be determined dynamically, the MappedForType and MappedForLength attributes can be used. (The example is the contents of creating a binary file that contains several image files in a container.)

[Serializable]
public class ContentContainer : Serializable
{
    public override ISerializationContext Context { get; set; }

    public ContentContainer(SerializationContext context)
    {
        Context = context;
    }

    [SerializableCulumn(Length = 2)]
    public string ParcelStart { get; set; } = "PS";

    [MappedForLength(Target = "ContentContainer.Attribute")]
    [SerializableCulumn]
    public int AttributeSize { get; set; } = 16;

    [SerializableCulumn]
    public byte[] Attribute { get; set; }

    [SerializableCulumn]
    public short SerialNumber { get; set; } = 0;

    [MappedForType(Target = "ContentContainer.Data", ValueConverter = "ContentContainer.GetDataTypeFromChunkName")]
    [SerializableCulumn(Length = 10)]
    public string ChunkName { get; set; }

    public static Type GetDataTypeFromChunkName(object name)
    {
        switch ((string)name)
        {
            case "IMAGE":
                return typeof(List<ImageContent>);
            case "DATA":
                return typeof(byte[]);
        }
        return null;
    }

    [MappedForLength(Target = "ContentContainer.Data")]
    [SerializableCulumn]
    public int DataCount { get; set; }

    [SerializableCulumn]
    public object Data { get; set; }

    [SerializableCulumn(Length = 2)]
    public string ParcelTail { get; set; } = "PE";
}
[Serializable]
public class ImageContent : Serializable
{
    public override ISerializationContext Context { get; set; }

    public ImageContent(SerializationContext context)
    {
        Context = context;
    }

    [SerializableCulumn(Length = 2)]
    public string ContentStart { get; set; } = "CS";

    [SerializableCulumn(Length = 100)]
    public string Name { get; set; }

    [MappedForLength(Target = "ImageContent.Data")]
    [SerializableCulumn]
    public int DataSize { get; set; }

    [SerializableCulumn]
    public byte[] Data { get; set; }

    [SerializableCulumn]
    public short Checksum { get; set; } = 0;

    [SerializableCulumn(Length = 2)]
    public string ContentTail { get; set; } = "CE";
}
static Serializable BuildSerializableEntity(SerializationContext context)
{
    var images = Directory.EnumerateFiles(@"~\images");
    var container = new ContentContainer(context);
    container.ChunkName = "IMAGE";
    container.DataCount = images.Count();
    var imageContents = new List<ImageContent>();
    foreach (var image in images)
    {
        var imageBytes = File.ReadAllBytes(image);
        var imageContent = new ImageContent(context);
        imageContent.Name = Path.GetFileName(image);
        imageContent.DataSize = imageBytes.Length;
        imageContent.Data = imageBytes;
        imageContents.Add(imageContent);
    }
    container.Data = imageContents;
    container.AttributeSize = 16;
    container.Attribute = new byte[16];
    container.SerialNumber = 123;
    return container;
}

Serialization using formatter

string imageContentFile = @"~\image_content_using_formatter.data";

ChoiFormatter formatter = new ChoiFormatter(typeof(ContentContainer));

// Example of reading an image file and serializing it into a single binary file
using (var stream = new FileStream(imageContentFile, FileMode.Create, FileAccess.Write, FileShare.None))
using (var buffer = new StreamByteBuffer(stream))
using (var context = new SerializationContext(buffer))
{
    formatter.Serialize(stream, BuildSerializableEntity(context));
}

// Example of deserializing an binary file
using (var stream = new FileStream(imageContentFile, FileMode.Open, FileAccess.Read, FileShare.None))
{
    ContentContainer data2 = (ContentContainer)formatter.Deserialize(stream);
}

Serializing without formatter

string imageContentFile = @"~\image_content.data";

// Example of reading an image file and serializing it into a single binary file
using (var buffer = new StreamByteBuffer(new FileStream(imageContentFile, FileMode.Create, FileAccess.Write, FileShare.None)))
using (var context = new SerializationContext(buffer))
{
    var container = BuildSerializableEntity(context);
    container.Serialize();
}

// Example of deserializing an binary file
using (var buffer = new StreamByteBuffer(new FileStream(imageContentFile, FileMode.Open, FileAccess.Read, FileShare.None)))
using (var context = new SerializationContext(buffer))
{
    var container = new ContentContainer(context);
    container.Deserialize();
}
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48 net481
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.1.5.2 290 3/19/2021
0.1.5.1 289 3/19/2021
0.1.5 294 3/19/2021
0.1.4 329 3/16/2021
0.1.3 291 3/15/2021