Inkslab.Json
1.2.25
.NET 6.0
This package targets .NET 6.0. The package is compatible with this framework or higher.
.NET Standard 2.1
This package targets .NET Standard 2.1. The package is compatible with this framework or higher.
.NET Framework 4.6.1
This package targets .NET Framework 4.6.1. The package is compatible with this framework or higher.
dotnet add package Inkslab.Json --version 1.2.25
NuGet\Install-Package Inkslab.Json -Version 1.2.25
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="Inkslab.Json" Version="1.2.25" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Inkslab.Json" Version="1.2.25" />
<PackageReference Include="Inkslab.Json" />
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 Inkslab.Json --version 1.2.25
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Inkslab.Json, 1.2.25"
#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 Inkslab.Json@1.2.25
#: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=Inkslab.Json&version=1.2.25
#tool nuget:?package=Inkslab.Json&version=1.2.25
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Inkslab.Json 是什么?
Inkslab.Json 是 IJsonHelper 基于 Newtonsoft.Json 的默认实现,提供:
- 统一的
NamingType命名风格转换。 - 缩进格式化输出。
- 属性忽略(
[Ignore])。 - 匿名类型反序列化。
- 可替换的
IJsonHelper契约(切换到System.Text.Json或其它实现零侵入)。
安装
dotnet add package Inkslab.Json
安装后由 JStartup 自动注册 IJsonHelper 默认实现。
快速入门
1. 序列化
var json = JsonHelper.ToJson(new { Id = 1, Name = "Tom" });
// => {"Id":1,"Name":"Tom"}
var camel = JsonHelper.ToJson(dto, NamingType.CamelCase);
var snake = JsonHelper.ToJson(dto, NamingType.SnakeCase, indented: true);
2. 反序列化
public class A
{
[Ignore] // 序列化/反序列化均忽略
public int A1 { get; set; } = 100;
public int A2 { get; set; }
public string A3 { get; set; } = string.Empty;
public DateTime A4 { get; set; }
}
var json = "{\"A2\":100,\"A3\":\"A3\",\"A4\":\"2022-12-03T14:17:55+08:00\"}";
var dto = JsonHelper.Json<A>(json);
3. 匿名类型反序列化
var anon = JsonHelper.Json(json, new { A2 = 0, A3 = "" }, NamingType.CamelCase);
// anon.A2 / anon.A3 强类型可用
核心契约
IJsonHelper src/Inkslab/Serialize/Json/IJsonHelper.cs
public interface IJsonHelper
{
string ToJson<T>(T jsonObj, NamingType namingType = NamingType.Normal, bool indented = false);
string ToJson (object jsonObj, Type type, NamingType namingType = NamingType.Normal, bool indented = false);
T Json<T> (string json, NamingType namingType = NamingType.Normal);
object Json (string json, Type type, NamingType namingType = NamingType.Normal);
}
静态门面 JsonHelper src/Inkslab/Serialize/Json/JsonHelper.cs
JsonHelper.ToJson(obj, NamingType.CamelCase);
JsonHelper.Json<T>(json);
JsonHelper.Json(json, new { Id = 0, Name = "" }); // 匿名类型
默认实现 DefaultJsonHelper src/Inkslab.Json/DefaultJsonHelper.cs
public DefaultJsonHelper();
public DefaultJsonHelper(JsonSerializerSettings settings); // 可注入自定义 Newtonsoft 设置
命名风格
配合 NamingType:
| 值 | 效果 |
|---|---|
Normal |
原样 |
CamelCase |
userName |
SnakeCase |
user_name |
PascalCase |
UserName |
KebabCase |
user-name |
var json = JsonHelper.ToJson(dto, NamingType.SnakeCase);
var dto2 = JsonHelper.Json<UserDto>(json, NamingType.SnakeCase);
自定义实现
1. 实现 IJsonHelper
public class MyJsonHelper : IJsonHelper
{
public string ToJson<T>(T jsonObj, NamingType namingType = NamingType.Normal, bool indented = false)
=> /* 自定义序列化 */;
public string ToJson(object jsonObj, Type type, NamingType namingType = NamingType.Normal, bool indented = false)
=> /* ... */;
public T Json<T>(string json, NamingType namingType = NamingType.Normal)
=> /* 自定义反序列化 */;
public object Json(string json, Type type, NamingType namingType = NamingType.Normal)
=> /* ... */;
}
2. 启动前注册
SingletonPools.TryAdd<IJsonHelper, MyJsonHelper>();
using var startup = new XStartup();
startup.DoStartup();
属性控制
忽略属性
public class User
{
public int Id { get; set; }
public string Name { get; set; }
[Ignore] // 来自 Inkslab.Annotations:序列化与反序列化均跳过
public string Password { get; set; }
}
覆盖 JSON 键名
public class Order
{
public int Id { get; set; }
[JsonProperty("order_no")] // 来自 Inkslab.Annotations:序列化输出 order_no,反序列化也读 order_no
public string OrderNo { get; set; }
}
var json = JsonHelper.ToJson(new Order { Id = 1, OrderNo = "SN001" });
// => {"Id":1,"order_no":"SN001"}
单元测试
参见 tests/Inkslab.Json.Tests/UnitTests.cs。
说明
- 基于
Newtonsoft.Json13.x。 - 命名策略通过
ContractResolver实现,内部已缓存常用策略。 - 需要
System.Text.Json时,自行实现IJsonHelper并通过SingletonPools替换即可。
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| .NET Framework | net461 is compatible. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETFramework 4.6.1
- Inkslab (>= 1.2.25)
- Newtonsoft.Json (>= 13.0.3)
-
.NETStandard 2.1
- Inkslab (>= 1.2.25)
- Newtonsoft.Json (>= 13.0.3)
-
net6.0
- Inkslab (>= 1.2.25)
- Newtonsoft.Json (>= 13.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.2.25 | 93 | 6/9/2026 |
| 1.2.24 | 118 | 4/22/2026 |
| 1.2.23 | 123 | 3/27/2026 |
| 1.2.22 | 112 | 3/24/2026 |
| 1.2.20 | 115 | 3/24/2026 |
| 1.2.18 | 241 | 10/9/2025 |
| 1.2.17 | 247 | 8/15/2025 |
| 1.2.16 | 160 | 6/7/2025 |
| 1.2.14 | 261 | 11/22/2024 |
| 1.2.13 | 666 | 9/10/2024 |
| 1.2.12 | 246 | 9/10/2024 |
| 1.2.11 | 339 | 8/4/2024 |
| 1.2.10 | 228 | 7/29/2024 |
| 1.2.9 | 199 | 7/29/2024 |
| 1.2.8.5 | 3,171 | 5/15/2024 |
| 1.2.8 | 2,127 | 3/26/2024 |
| 1.2.7 | 290 | 3/1/2024 |
| 1.2.5 | 596 | 1/2/2024 |
| 1.2.4.1 | 373 | 12/20/2023 |
| 1.2.4 | 292 | 12/12/2023 |
Loading failed