Hand.ParseJson 0.3.1.3-alpha

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

Json解析器

  • 相当于自定义json反序列化

一、读取单个属性

1. 读取文本原始值

  • 使用First或String方法读取属性文本原始值
string json = "{\"id\": 123}";
var config = HandJson.Default;
var idReader = config.First("id");
var result = idReader.Parse(json);

string json2 = "{\"name\": \"jxj\"}";
var idReader2 = config.First("name", config.String());
var result2 = idReader.Parse(json2);

2. 读取布尔原始值

  • 使用Bool方法读取属性布尔原始值
string json = "{\"state\": true}";
var config = HandJson.Default;
var stateReader = config.First("state", config.Bool());
var result = stateReader.Parse(json);

3. 读取强类型值

  • 使用First泛型或Value泛型方法读取属性强类型值
string json = "{\"id\": 123}";
var config = HandJson.Default;
var idReader = config.First<int>("id");
var result = idReader.Parse(json);

var idReader2 = config.First("id", config.Value<int>());
var result2 = idReader2.Parse(json);

二、解析到实体

  • Entity泛型方法指定实体类型
  • WithProperty泛型方法读取属性强类型值并绑到实体
string json = "{ \"Id\": 123, \"Name\": \"Jxj\",  \"State\": true}";
var userParser = HandJson.Default.Entity<User>()
    .WithProperty<int>(nameof(User.Id))
    .WithProperty<string>(nameof(User.Name))
    .WithProperty<bool>(nameof(User.State));
var result = userParser.Parse(json);

三、解析集合

  • 通过Each方法定义集合解析
string json = "[{ \"Id\": 1, \"Name\": \"张三\",  \"State\": true}, { \"Id\": 2, \"Name\": \"李四\",  \"State\": false}]";
var repeatReader = HandJson.Default.Entity<User>()
    .WithProperty<int>(nameof(User.Id))
    .WithProperty<string>(nameof(User.Name))
    .WithProperty<bool>(nameof(User.State))
    .Each();
var result = repeatReader.Parse(json);

四、解析字典

1. 把列表解析为字典

  • 先定义键的解析规则
  • 使用Dictionary扩展方法解析字典,acceptDefault可选参数配置是否包含默认值
string json = "[{ \"Id\": 1, \"Name\": \"张三\"}, { \"Id\": 2, \"Name\": \"李四\"}]";

var config = HandJson.Default;
var dictionaryReader = config.Property<int>(nameof(User.Id))            
    .Dictionary(config.Property("Name").First());
IDictionary<int, string> result = dictionaryReader.Parse(json);

2.默认字典

  • 使用Dictionary方法解析字典,acceptDefault可选参数配置是否包含默认值
  • 支持一个值类型参数的重载
  • 支持键类型和值类型参数的重载
        string json = "{ \"Id\": 1, \"Name\": \"张三\",  \"State\": true}";

        var dictionaryReader = HandJson.Default.Dictionary();
        IDictionary<string, object> result = dictionaryReader.Parse(json);

五、包装类型转化

  • Convert方法转入一个转化器可以变成另一个类型的解析器
string json = "{\"id\": 123}";
var config = HandJson.Default;
var idReader = config.First<int>("id")
    .Convert(id => new UserId(id));
UserId result = idReader.Parse(json);

六、复杂类型

  • 通过WithProperty可以一个解析器绑定到主解析器的成员上
var userId = 123;
var roleId = 1;
string json = @$"{{""User"": {{ ""Id"": {userId}, ""Name"": ""Jxj"",  ""State"": true}}, ""Role"": {{ ""Id"": {roleId}, ""Name"": ""Admin""}}}}";
var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(json));
var config = HandJson.Default;
var userParser = config.Entity<User>()
    .WithProperty<int>(nameof(User.Id))
    .WithProperty<string>(nameof(User.Name))
    .WithProperty<bool>(nameof(User.State));
var roleParser = config.Entity<Role>()
    .WithProperty<int>(nameof(Role.Id))
    .WithProperty<string>(nameof(Role.Name));

var complexParser = config.Entity<Complex>()
    .WithProperty(userParser, nameof(Complex.User))
    .WithProperty(roleParser, nameof(Complex.Role));
var result = complexParser.Parse(reader);
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 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 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.  net10.0 is compatible.  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 netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 is compatible.  net463 was computed.  net47 is compatible.  net471 was computed.  net472 was computed.  net48 is compatible.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  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.

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
0.3.1.3-alpha 42 7/15/2026
0.3.1.2-alpha 48 7/10/2026
0.3.1.1-alpha 52 7/9/2026
0.3.1-alpha 48 7/9/2026