feiyun0112 / MyIO.ParseJsonAsClass.SourceGenerator

使用 Source generator 实现类似于 Visual Studio 菜单“编辑”>"选择性粘贴">"将 JSON 粘贴为类"的功能,将 JSON 字符串转换为 C# 类。

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

使用 Source generator 实现类似于 Visual Studio 菜单“编辑”>"选择性粘贴">"将 JSON 粘贴为类"的功能,将 JSON 字符串转换为 C# 类。

Use the Source generator to implement functionality similar to the Visual Studio menu "Edit" > "Paste Special" > "Paste JSON as Class" to convert JSON strings to C# classes.

如何使用 How to use

  1. 在项目中安装 NuGet 包

Install the NuGet package in the project

dotnet add package MyIO.ParseJsonAsClass.SourceGenerator
  1. 在项目中添加一个 JSON 文件

Add a JSON file in the project

{
  "code": 200,
  "msg": "ok",
  "obj":{"a":1,"subObj":{"a":1}},
  "data": [
    "1","2"
  ],
  "array": [
    {"a":1.0},
    {"a":null}
  ]
}
  1. 在项目中添加一个 C# 文件

Add a C# file in the project

using MyIO;
namespace ConsoleApp1
{
    [ParseJsonAsClass("sample.txt")]
    internal partial class Class1
    { 
    }
}

sample.txt 是上一步中添加的 JSON 文件的名称。

sample.txt is the name of the JSON file added in the previous step.

  1. 编译项目

Build the project

About

使用 Source generator 实现类似于 Visual Studio 菜单“编辑”>"选择性粘贴">"将 JSON 粘贴为类"的功能,将 JSON 字符串转换为 C# 类。

License:MIT License


Languages

Language:C# 100.0%