Sparkdo.Serialization 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Sparkdo.Serialization --version 1.0.0
                    
NuGet\Install-Package Sparkdo.Serialization -Version 1.0.0
                    
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="Sparkdo.Serialization" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Sparkdo.Serialization" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Sparkdo.Serialization" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Sparkdo.Serialization --version 1.0.0
                    
#r "nuget: Sparkdo.Serialization, 1.0.0"
                    
#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.
#:package Sparkdo.Serialization@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Sparkdo.Serialization&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Sparkdo.Serialization&version=1.0.0
                    
Install as a Cake Tool

Sparkdo.Serialization

Sparkdo 序列化处理库,提供对象序列化和反序列化的通用功能。

功能特性

  • 基于 System.Text.Json 的默认序列化实现
  • 支持自定义特定类型的序列化器
  • 通过模块系统自动注册服务
  • 支持依赖注入扩展

安装

<PackageReference Include="Sparkdo.Serialization" Version="x.x.x" />

使用方法

基本用法

// 通过依赖注入获取序列化服务
public class MyService
{
    private readonly IObjectSerializer _serializer;
    
    public MyService(IObjectSerializer serializer)
    {
        _serializer = serializer;
    }
    
    public byte[] SerializeObject<T>(T obj)
    {
        return _serializer.Serialize(obj);
    }
    
    public T DeserializeObject<T>(byte[] data)
    {
        return _serializer.Deserialize<T>(data);
    }
}

自定义特定类型序列化器

对于特定类型,可以实现自定义序列化器:

// 实现特定类型的序列化器
public class CustomTypeSerializer : IObjectSerializer<CustomType>
{
    public byte[] Serialize(CustomType obj)
    {
        // 自定义序列化逻辑
        return Encoding.UTF8.GetBytes(obj.ToString());
    }

    public CustomType Deserialize(byte[] bytes)
    {
        // 自定义反序列化逻辑
        return new CustomType(Encoding.UTF8.GetString(bytes));
    }
}

// 注册自定义序列化器
services.AddObjectSerializer<CustomType, CustomTypeSerializer>();

当为特定类型注册了自定义序列化器后,DefaultObjectSerializer 会自动使用该序列化器。

模块注册

SparkdoSerializationModule 会自动注册默认的序列化服务:

public class SparkdoSerializationModule : Module
{
    public override void Configure(IConfigureContext context)
    {
        // 注册默认序列化服务
        context.Services.AddTransient<IObjectSerializer, DefaultObjectSerializer>();
    }
}

接口定义

IObjectSerializer

通用对象序列化接口:

public interface IObjectSerializer
{
    byte[] Serialize<T>(T obj);
    T Deserialize<T>(byte[] bytes);
}

IObjectSerializer<T>

特定类型序列化接口:

public interface IObjectSerializer<T>
{
    byte[] Serialize(T obj);
    T Deserialize(byte[] bytes);
}

扩展方法

提供了扩展方法用于注册自定义序列化器:

// 扩展方法定义在 Microsoft/Extensions/DependencyInjection/SerializationServiceCollectionExtensions.cs 文件中
public static class SerializationServiceCollectionExtensions
{
    public static IServiceCollection AddObjectSerializer<T, TObjectSerializer>(this IServiceCollection services)
        where TObjectSerializer : class, IObjectSerializer<T>
    {
        services.AddTransient<IObjectSerializer<T>, TObjectSerializer>();
        return services;
    }
}

扩展性

可以通过以下方式扩展序列化功能:

  1. 实现 IObjectSerializer<T> 接口并注册为服务来自定义特定类型的序列化
  2. 通过 AddObjectSerializer<T, TObjectSerializer> 扩展方法注册自定义序列化器
Product 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.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework 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. 
Compatible target framework(s)
Included target framework(s) (in package)
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
1.0.4-preview.3 70 6/8/2026
1.0.4-preview.2 70 6/7/2026
1.0.4-preview.1 64 6/7/2026
1.0.3 138 6/6/2026
1.0.3-preview.1 70 5/17/2026
1.0.2 115 4/27/2026
1.0.2-preview.5 74 4/15/2026
1.0.2-preview.4 87 2/8/2026
1.0.2-preview.3 90 2/1/2026
1.0.2-preview.2 77 1/31/2026
1.0.2-preview.1 195 12/4/2025
1.0.1 237 11/27/2025
1.0.0 231 11/25/2025
1.0.0-preview.5 147 10/24/2025