Asgard.Extends.Json
3.1.9
dotnet add package Asgard.Extends.Json --version 3.1.9
NuGet\Install-Package Asgard.Extends.Json -Version 3.1.9
<PackageReference Include="Asgard.Extends.Json" Version="3.1.9" />
<PackageVersion Include="Asgard.Extends.Json" Version="3.1.9" />
<PackageReference Include="Asgard.Extends.Json" />
paket add Asgard.Extends.Json --version 3.1.9
#r "nuget: Asgard.Extends.Json, 3.1.9"
#:package Asgard.Extends.Json@3.1.9
#addin nuget:?package=Asgard.Extends.Json&version=3.1.9
#tool nuget:?package=Asgard.Extends.Json&version=3.1.9
Asgard.Extends.Json
简介
Asgard.Extends.Json 是 Asgard 框架的一部分,提供了一些常用的 System.Text.Json 扩展功能,包括自定义的转换器(Converters)。
功能特性
- 公共序列化配置:定义了多种
JsonSerializerOptions,支持驼峰命名、中文字符编码、忽略大小写等多种配置。 - JSON 扩展函数:提供了用于序列化和反序列化的扩展方法,支持不同的压缩级别和自定义配置选项。
主要类和文件
- CommonSerializerOptions.cs:定义了各种
JsonSerializerOptions的静态实例,方便在项目中复用。 - JsonExtends.cs:包含 JSON 序列化和反序列化的扩展方法,支持不同配置选项和压缩级别。
- Converters/:包含多个自定义的 JSON 转换器,如
DateTime2StringConverter和Long2StringConverter。
自定义转换器 (Converters)
DateTime2StringConverter
将 DateTime 类型转换为指定格式的字符串。
使用示例
using System.Text.Json;
using Asgard.Extends.Json.Converters;
var options = new JsonSerializerOptions();
options.Converters.Add(new DateTime2StringConverter("yyyy-MM-dd HH:mm"));
var dateTime = DateTime.Now;
string jsonString = JsonSerializer.Serialize(dateTime, options);
Long2StringConverter
将 long 类型转换为字符串。
使用示例
using System.Text.Json;
using Asgard.Extends.Json.Converters;
var options = new JsonSerializerOptions();
options.Converters.Add(new Long2StringConverter());
long number = 1234567890;
string jsonString = JsonSerializer.Serialize(number, options);
使用示例
以下是一个简单的使用示例,展示如何使用 JsonExtends 类进行 JSON 序列化和反序列化:
using Asgard.Extends.Json;
var myObject = new MyClass
{
Property1 = "Value1",
Property2 = 123
};
// 序列化为 JSON 字节数组
byte[] jsonBytes = myObject.GetBytes();
// 反序列化为对象
MyClass deserializedObject = jsonBytes.GetObject<MyClass>();
ASP.NET Core 全局注册自定义 Converter
你可以让 ASP.NET Core 自动全局支持 Long2StringConverter,只需在 Program.cs 或 Startup.cs 的 JsonOptions 里注册即可。
Minimal API 示例
builder.Services.Configure<JsonOptions>(options =>
{
options.JsonSerializerOptions.Converters.Add(new Asgard.Extends.Json.Converters.Long2StringConverter());
});
MVC 示例
builder.Services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new Asgard.Extends.Json.Converters.Long2StringConverter());
});
这样所有 long 类型序列化/反序列化都会自动用你的 converter,无需每次手动标注。
依赖项
- Asgard.Tools:提供了一些辅助工具和方法,如
BrotliUTF8压缩工具。
版权信息
本项目遵循 MIT 许可协议。详细信息请参阅 LICENSE 文件。
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. 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 was computed. 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. |
-
net8.0
- Asgard.Tools (>= 3.1.9)
NuGet packages (4)
Showing the top 4 NuGet packages that depend on Asgard.Extends.Json:
| Package | Downloads |
|---|---|
|
Asgard.Extends.AspNetCore
Asgard.Extends.AspNetCore 是Asgard框架对于Asp.Net core 的封装核心,是一个抽象与辅助函数对应的层 |
|
|
Asgard.Hosts.AspNetCore
Asgard.Hosts.AspNetCore 提供基于 ASP.NET Core 的宿主层实现,负责应用启动与依赖注入引导、通用中间件与管道配置、端点路由、gRPC 与 Swagger 集成,以及与 Asgard 框架模块(日志、配置、扩展等)的托管适配。兼容 .NET 8,面向可扩展的微服务与 Web 应用托管。 |
|
|
Asgard.Caches.Redis
Asgard.Caches.Redis 为阿斯加德框架提供基于 CSRedis 的缓存实现,针对 .NET 8 进行构建与兼容性优化。 |
|
|
Asgard.ConfigCenter
Asgard.ConfigCenter 是阿斯加德框架(Asgard Framework)中的一个模块,负责配置中心相关功能的实现与管理。 |
GitHub repositories
This package is not used by any popular GitHub repositories.