Sparkdo.Serialization
1.0.3
There is a newer prerelease version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Sparkdo.Serialization --version 1.0.3
NuGet\Install-Package Sparkdo.Serialization -Version 1.0.3
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.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Sparkdo.Serialization" Version="1.0.3" />
<PackageReference Include="Sparkdo.Serialization" />
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.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Sparkdo.Serialization, 1.0.3"
#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.3
#: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.3
#tool nuget:?package=Sparkdo.Serialization&version=1.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
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;
}
}
扩展性
可以通过以下方式扩展序列化功能:
- 实现 IObjectSerializer<T> 接口并注册为服务来自定义特定类型的序列化
- 通过 AddObjectSerializer<T, TObjectSerializer> 扩展方法注册自定义序列化器
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net10.0
- JetBrains.Annotations (>= 2025.2.4)
- Microsoft.Extensions.Configuration (>= 10.0.8)
- Microsoft.Extensions.Configuration.CommandLine (>= 10.0.8)
- Microsoft.Extensions.Configuration.EnvironmentVariables (>= 10.0.8)
- Microsoft.Extensions.Configuration.FileExtensions (>= 10.0.8)
- Microsoft.Extensions.Configuration.Json (>= 10.0.8)
- Microsoft.Extensions.Configuration.UserSecrets (>= 10.0.8)
- Microsoft.Extensions.DependencyInjection (>= 10.0.8)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.8)
- Microsoft.Extensions.DependencyModel (>= 10.0.8)
- Microsoft.Extensions.FileProviders.Physical (>= 10.0.8)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.8)
- Microsoft.Extensions.Localization (>= 10.0.8)
- Microsoft.Extensions.Logging (>= 10.0.8)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.8)
- Microsoft.Extensions.Options (>= 10.0.8)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 10.0.8)
- Microsoft.Extensions.Options.DataAnnotations (>= 10.0.8)
- Microsoft.Extensions.Primitives (>= 10.0.8)
- Nito.AsyncEx.Context (>= 5.1.2)
- Sparkdo.Core (>= 1.0.3)
- System.Linq.Dynamic.Core (>= 1.7.2)
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 | 69 | 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 |