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
                    
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="Asgard.Extends.Json" Version="3.1.9" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Asgard.Extends.Json" Version="3.1.9" />
                    
Directory.Packages.props
<PackageReference Include="Asgard.Extends.Json" />
                    
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 Asgard.Extends.Json --version 3.1.9
                    
#r "nuget: Asgard.Extends.Json, 3.1.9"
                    
#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 Asgard.Extends.Json@3.1.9
                    
#: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=Asgard.Extends.Json&version=3.1.9
                    
Install as a Cake Addin
#tool nuget:?package=Asgard.Extends.Json&version=3.1.9
                    
Install as a Cake Tool

Asgard.Extends.Json

简介

Asgard.Extends.Json 是 Asgard 框架的一部分,提供了一些常用的 System.Text.Json 扩展功能,包括自定义的转换器(Converters)。

功能特性

  • 公共序列化配置:定义了多种 JsonSerializerOptions,支持驼峰命名、中文字符编码、忽略大小写等多种配置。
  • JSON 扩展函数:提供了用于序列化和反序列化的扩展方法,支持不同的压缩级别和自定义配置选项。

主要类和文件

  • CommonSerializerOptions.cs:定义了各种 JsonSerializerOptions 的静态实例,方便在项目中复用。
  • JsonExtends.cs:包含 JSON 序列化和反序列化的扩展方法,支持不同配置选项和压缩级别。
  • Converters/:包含多个自定义的 JSON 转换器,如 DateTime2StringConverterLong2StringConverter

自定义转换器 (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.csStartup.csJsonOptions 里注册即可。

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.

Version Downloads Last Updated
3.1.9 171 1/16/2026
3.1.8 157 1/15/2026
3.1.7 250 12/22/2025
3.1.6 495 12/11/2025
3.1.5 292 12/7/2025
3.1.4 754 12/2/2025
3.1.2 334 11/30/2025
3.1.1 338 11/30/2025
3.1.0 315 11/22/2025
3.0.2 420 11/17/2025
3.0.1 308 11/14/2025
3.0.0 328 11/5/2025