Monday 26 December 2016

My First NuGet package: A serializer for C#

I've written a simple serializer for C#. There are no native dependencies, so that it can run on any architecture. And, as it is based on .NET Standard 1.1 it is also fairly portable.

Getting the sources

The sources are as usual available at GitHub (though but one should clone the solution repo if they want to build it).

Installing it as a NuGet package

One can install it as the NuGet package Albite.Serialization.

What can it serialiaze?

  • Primitives
  • Arrays
  • Standard collections
  • Classes

How can I use it?

Simply throw something at  ObjectWriter.WriteObject()  and than get it back using  ObjectReader.ReadObject() .

For example, serializing:

using (ObjectWriter writer = new ObjectWriter(stream))
{
    writer.WriteObject(10);
    writer.WriteObject(MyEnum.X);
    writer.WriteObject("hello");
    writer.WriteObject(new int[] { 10, 20 });
    writer.WriteObject(new Stack<int>(new int[] { 100, 200 }));
    writer.WriteObject(null);
    writer.WriteObject(typeof(string));
}


Then, reading it back:

using (ObjectReader reader = new ObjectReader(stream))
{
    int i = (int)reader.ReadObject();
    MyEnum e = (MyEnum)reader.ReadObject();
    string s = (string)reader.ReadObject();
    int[] arr = (int[])reader.ReadObject();
    Stack<int> st = (Stack<int>)reader.ReadObject();
    object o = reader.ReadObject();
    Type t = (Type)reader.ReadObject();
}


How is it licensed?

It is licensed under Apache 2.0.

No comments:

Post a Comment