SerialBox is a library designed to simplify serialization in .Net. By default it supports XML and JSON but can be expanded upon to support other serialization targets as well.
The library can be initialized by registering it with your IoC container during startup. Example code:
ServiceProvider? ServiceProvider = new ServiceCollection().RegisterSerialBox()?.BuildServiceProvider();
or
ServiceProvider? ServiceProvider = new ServiceCollection().AddCanisterModules()?.BuildServiceProvider();
As the library supports Canister Modules.
This line is required prior to using the extension methods for the first time. Once it is set up, you can call the extension methods provided:
[DataContract]
public class Temp
{
[DataMember(Name = "A", Order = 1)]
public int A { get; set; }
}
...
var TestObj = new Temp() { A = 100 };
string Value = TestObj.Serialize<string, Temp>();
Temp TestObj2 = Value.Deserialize<Temp, string>();
The Serialize function takes the serialization type as a parameter. If one is not passed in, it defaults to JSON. This parameter can either be the MIME type for the serialization type as a string or it can be a SerializationType object. The Deserialize function acts in the same manner.
The system comes with JSON and XML serialization, however you may wish to add other targets such as binary. In order to do this all that you need to do is create a class that inherits from ISerializer:
public class MySerializer : ISerializer<byte[]>
{
public string ContentType => "application/octet-stream";
public string FileType => ".blob";
public string Name => "Binary";
public object Deserialize(Type objectType, byte[] data) { ... }
public byte[] Serialize(Type objectType, object data) { ... }
}
After the class is created, the system will automatically pick it up and use it.
By default the system uses the built in JSON and XML providers in .Net. However it is possible to override these by simply creating a class that inherits from ISerializer and setting the correct ContentType to match the one that you wish to override. For instance to override the JSON provider with your own you would do the following:
public class MySerializer : ISerializer<string>
{
public string ContentType => "application/json";
public string FileType => ".json";
public string Name => "JSON";
public object Deserialize(Type objectType, string data) { ... }
public string Serialize(Type objectType, object data) { ... }
}
After the class is created, the system will automatically pick it up and use it.
The library is available via Nuget with the package name "SerialBox". To install it run the following command in the Package Manager Console:
Install-Package SerialBox
In order to build the library you will require the following as a minimum:
- Visual Studio 2022
Other than that, just clone the project and you should be able to load the solution and build without too much effort.