Tenon.Serialization.Json
0.0.1-alpha-202502101434
This is a prerelease version of Tenon.Serialization.Json.
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 Tenon.Serialization.Json --version 0.0.1-alpha-202502101434
NuGet\Install-Package Tenon.Serialization.Json -Version 0.0.1-alpha-202502101434
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="Tenon.Serialization.Json" Version="0.0.1-alpha-202502101434" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Tenon.Serialization.Json --version 0.0.1-alpha-202502101434
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Tenon.Serialization.Json, 0.0.1-alpha-202502101434"
#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 Tenon.Serialization.Json as a Cake Addin #addin nuget:?package=Tenon.Serialization.Json&version=0.0.1-alpha-202502101434&prerelease // Install Tenon.Serialization.Json as a Cake Tool #tool nuget:?package=Tenon.Serialization.Json&version=0.0.1-alpha-202502101434&prerelease
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Tenon.Serialization.Json
基于 System.Text.Json 的序列化实现,提供了高性能的 JSON 序列化和反序列化功能。
项目特点
- 基于 System.Text.Json 实现
- 支持依赖注入
- 提供日期时间转换器
- 支持命名服务注入
快速开始
- 安装 NuGet 包:
dotnet add package Tenon.Serialization.Json
- 添加服务:
// 使用默认配置
services.AddSystemTextJsonSerializer();
// 或使用自定义配置
services.AddSystemTextJsonSerializer(new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});
- 使用命名服务注入:
services.AddKeyedSystemTextJsonSerializer("custom", new JsonSerializerOptions
{
WriteIndented = true
});
配置说明
默认配置
当不提供 JsonSerializerOptions 时,将使用默认的序列化配置。
自定义配置
你可以通过提供 JsonSerializerOptions 来自定义序列化行为:
var options = new JsonSerializerOptions
{
// 常用配置
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true,
// 日期时间处理
Converters =
{
new DateTimeConverter(),
new DateTimeNullableConverter()
}
};
services.AddSystemTextJsonSerializer(options);
依赖项
- Tenon.Serialization.Abstractions
- System.Text.Json
- Microsoft.Extensions.DependencyInjection.Abstractions
项目结构
Tenon.Serialization.Json/
├── Converters/ # JSON 转换器
│ ├── DateTimeConverter.cs
│ └── DateTimeNullableConverter.cs
├── Extensions/ # 扩展方法
│ └── ServiceCollectionExtension.cs
├── JsonSerializer.cs # JSON 序列化实现
└── SystemTextJsonSerializer.cs
注意事项
性能考虑:
- System.Text.Json 相比 Newtonsoft.Json 具有更好的性能
- 建议在性能敏感场景使用
兼容性:
- 确保目标框架支持 System.Text.Json
- 注意日期时间格式的处理
最佳实践:
- 建议在应用程序启动时配置序列化选项
- 使用依赖注入管理序列化器实例
- 需要多个序列化配置时使用命名服务注入
使用示例
基本使用
public class WeatherForecast
{
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
public string? Summary { get; set; }
}
public class WeatherService
{
private readonly ISerializer _serializer;
public WeatherService(ISerializer serializer)
{
_serializer = serializer;
}
public string SerializeWeather(WeatherForecast forecast)
{
// 序列化对象为 JSON 字符串
return _serializer.SerializeObject(forecast);
}
public WeatherForecast DeserializeWeather(string json)
{
// 从 JSON 字符串反序列化对象
return _serializer.DeserializeObject<WeatherForecast>(json);
}
public byte[] SerializeToBytes(WeatherForecast forecast)
{
// 序列化对象为字节数组
return _serializer.Serialize(forecast);
}
public WeatherForecast DeserializeFromBytes(byte[] bytes)
{
// 从字节数组反序列化对象
return _serializer.Deserialize<WeatherForecast>(bytes);
}
}
使用命名服务
public class MultiFormatService
{
private readonly ISerializer _defaultSerializer;
private readonly ISerializer _indentedSerializer;
public MultiFormatService(
ISerializer defaultSerializer,
[FromKeyedServices("indented")] ISerializer indentedSerializer)
{
_defaultSerializer = defaultSerializer;
_indentedSerializer = indentedSerializer;
}
public string SerializeCompact(WeatherForecast forecast)
{
// 使用默认序列化器(紧凑格式)
return _defaultSerializer.SerializeObject(forecast);
}
public string SerializeIndented(WeatherForecast forecast)
{
// 使用缩进格式序列化器
return _indentedSerializer.SerializeObject(forecast);
}
}
// 在 Startup.cs 或 Program.cs 中配置
services.AddSystemTextJsonSerializer(); // 默认序列化器
services.AddKeyedSystemTextJsonSerializer("indented", new JsonSerializerOptions
{
WriteIndented = true
});
配置示例
public static class SerializerConfig
{
public static IServiceCollection AddCustomJsonSerializer(this IServiceCollection services)
{
var options = new JsonSerializerOptions
{
// 常用配置
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true,
// 日期时间处理
Converters =
{
new DateTimeConverter(),
new DateTimeNullableConverter()
},
// 其他常用选项
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
NumberHandling = JsonNumberHandling.AllowReadingFromString,
ReferenceHandler = ReferenceHandler.IgnoreCycles
};
services.AddSystemTextJsonSerializer(options);
return services;
}
}
// 使用自定义配置
services.AddCustomJsonSerializer();
输出示例
// 默认输出
{
"date":"2025-02-10T14:20:02Z",
"temperatureC":23,
"summary":"Warm"
}
// 缩进输出
{
"date": "2025-02-10T14:20:02Z",
"temperatureC": 23,
"summary": "Warm"
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net9.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- Tenon.Serialization.Abstractions (>= 0.0.1-alpha-202502101434)
NuGet packages (4)
Showing the top 4 NuGet packages that depend on Tenon.Serialization.Json:
Package | Downloads |
---|---|
Tenon.Infra.Consul.RefitClient
Package Description |
|
Tenon.Caching.RedisStackExchange
Package Description |
|
Mortise.Accessibility.Locator.Json
UI Automation Framework |
|
Mortise.BrowserAccessibility
UI Automation Framework |
GitHub repositories
This package is not used by any popular GitHub repositories.