Tenon.Serialization.Json 0.0.1-alpha-202502241449

This is a prerelease version of Tenon.Serialization.Json.
dotnet add package Tenon.Serialization.Json --version 0.0.1-alpha-202502241449
                    
NuGet\Install-Package Tenon.Serialization.Json -Version 0.0.1-alpha-202502241449
                    
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-202502241449" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Tenon.Serialization.Json" Version="0.0.1-alpha-202502241449" />
                    
Directory.Packages.props
<PackageReference Include="Tenon.Serialization.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 Tenon.Serialization.Json --version 0.0.1-alpha-202502241449
                    
#r "nuget: Tenon.Serialization.Json, 0.0.1-alpha-202502241449"
                    
#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.
#addin nuget:?package=Tenon.Serialization.Json&version=0.0.1-alpha-202502241449&prerelease
                    
Install Tenon.Serialization.Json as a Cake Addin
#tool nuget:?package=Tenon.Serialization.Json&version=0.0.1-alpha-202502241449&prerelease
                    
Install Tenon.Serialization.Json as a Cake Tool

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-202502241449 55 a month ago
0.0.1-alpha-202502101554 68 2 months ago
0.0.1-alpha-202502101448 64 2 months ago
0.0.1-alpha-202502101434 63 2 months ago
0.0.1-alpha-202501130258 46 3 months ago
0.0.1-alpha-202412311524 77 3 months ago
0.0.1-alpha-202412061617 62 4 months ago
0.0.1-alpha-202412051527 62 4 months ago
0.0.1-alpha-202412051431 56 4 months ago
0.0.1-alpha-202412041445 54 4 months ago
0.0.1-alpha-202412021409 56 4 months ago
0.0.1-alpha-202411301019 56 4 months ago
0.0.1-alpha-202411170525 58 5 months ago
0.0.1-alpha-202411161308 53 5 months ago
0.0.1-alpha-202411131604 64 5 months ago
0.0.1-alpha-202411111439 69 5 months ago
0.0.1-alpha-202411051434 61 5 months ago
0.0.1-alpha-202410281339 75 5 months ago
0.0.1-alpha-202410131500 83 6 months ago
0.0.1-alpha-202407261457 93 8 months ago
0.0.1-alpha-202407261325 64 8 months ago
0.0.1-alpha-202406271301 166 9 months ago
0.0.1-alpha-202406251508 72 9 months ago
0.0.1-alpha-202406251310 60 9 months ago
0.0.1-alpha-202406141611 146 10 months ago
0.0.1-alpha-202406141550 59 10 months ago
0.0.1-alpha-202406121515 71 10 months ago
0.0.1-alpha-202406061553 78 10 months ago
0.0.1-alpha-202406041519 64 10 months ago
0.0.1-alpha-202406011613 71 10 months ago
0.0.1-alpha-202406011238 66 10 months ago
0.0.1-alpha-202405311458 75 5/31/2024
0.0.1-alpha-202405291213 82 5/29/2024
0.0.1-alpha-202405190457 80 5/19/2024
0.0.1-alpha-202405161229 58 5/16/2024
0.0.1-alpha-202405141510 61 5/14/2024
0.0.1-alpha-202405101323 66 5/10/2024
0.0.1-alpha-202405081356 68 5/8/2024
0.0.1-alpha-202405021337 33 5/2/2024
0.0.1-alpha-202405021336 31 5/2/2024
0.0.1-alpha-202405020452 45 5/2/2024
0.0.1-alpha-202405011443 48 5/1/2024
0.0.1-alpha-202404291541 68 4/29/2024
0.0.1-alpha-202404281218 67 4/28/2024