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.
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                
#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                

Tenon.Serialization.Json

基于 System.Text.Json 的序列化实现,提供了高性能的 JSON 序列化和反序列化功能。

项目特点

  • 基于 System.Text.Json 实现
  • 支持依赖注入
  • 提供日期时间转换器
  • 支持命名服务注入

快速开始

  1. 安装 NuGet 包:
dotnet add package Tenon.Serialization.Json
  1. 添加服务:
// 使用默认配置
services.AddSystemTextJsonSerializer();

// 或使用自定义配置
services.AddSystemTextJsonSerializer(new JsonSerializerOptions
{
    PropertyNameCaseInsensitive = true,
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});
  1. 使用命名服务注入:
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

注意事项

  1. 性能考虑:

    • System.Text.Json 相比 Newtonsoft.Json 具有更好的性能
    • 建议在性能敏感场景使用
  2. 兼容性:

    • 确保目标框架支持 System.Text.Json
    • 注意日期时间格式的处理
  3. 最佳实践:

    • 建议在应用程序启动时配置序列化选项
    • 使用依赖注入管理序列化器实例
    • 需要多个序列化配置时使用命名服务注入

使用示例

基本使用

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 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.

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.

Version Downloads Last updated
0.0.1-alpha-202502101554 40 2/10/2025
0.0.1-alpha-202502101448 36 2/10/2025
0.0.1-alpha-202502101434 37 2/10/2025
0.0.1-alpha-202501130258 35 1/13/2025
0.0.1-alpha-202412311524 72 12/31/2024
0.0.1-alpha-202412061617 59 12/6/2024
0.0.1-alpha-202412051527 61 12/5/2024
0.0.1-alpha-202412051431 55 12/5/2024
0.0.1-alpha-202412041445 51 12/4/2024
0.0.1-alpha-202412021409 53 12/2/2024
0.0.1-alpha-202411301019 55 11/30/2024
0.0.1-alpha-202411170525 57 11/17/2024
0.0.1-alpha-202411161308 52 11/16/2024
0.0.1-alpha-202411131604 62 11/13/2024
0.0.1-alpha-202411111439 68 11/11/2024
0.0.1-alpha-202411051434 60 11/5/2024
0.0.1-alpha-202410281339 74 10/28/2024
0.0.1-alpha-202410131500 82 10/13/2024
0.0.1-alpha-202407261457 93 7/26/2024
0.0.1-alpha-202407261325 63 7/26/2024
0.0.1-alpha-202406271301 165 6/27/2024
0.0.1-alpha-202406251508 71 6/25/2024
0.0.1-alpha-202406251310 59 6/25/2024
0.0.1-alpha-202406141611 145 6/14/2024
0.0.1-alpha-202406141550 58 6/14/2024
0.0.1-alpha-202406121515 70 6/12/2024
0.0.1-alpha-202406061553 78 6/6/2024
0.0.1-alpha-202406041519 63 6/4/2024
0.0.1-alpha-202406011613 69 6/1/2024
0.0.1-alpha-202406011238 65 6/1/2024
0.0.1-alpha-202405311458 72 5/31/2024
0.0.1-alpha-202405291213 81 5/29/2024
0.0.1-alpha-202405190457 79 5/19/2024
0.0.1-alpha-202405161229 57 5/16/2024
0.0.1-alpha-202405141510 60 5/14/2024
0.0.1-alpha-202405101323 65 5/10/2024
0.0.1-alpha-202405081356 67 5/8/2024
0.0.1-alpha-202405021337 30 5/2/2024
0.0.1-alpha-202405021336 28 5/2/2024
0.0.1-alpha-202405020452 44 5/2/2024
0.0.1-alpha-202405011443 47 5/1/2024
0.0.1-alpha-202404291541 61 4/29/2024
0.0.1-alpha-202404281218 65 4/28/2024