简体   繁体   English

JSON 模式到 C#

[英]JSON Schema to C#

I am looking for a class or tool to convert JSON schema into a C# class as a prebuild step.我正在寻找一个类或工具来将 JSON 模式转换为 C# 类作为预构建步骤。

I have found several "home-brew" solutions (jsonschema.net, NJsonSchema, ...) , but would prefer to use some mature / official code related to a company / project.我找到了几个“自制”解决方案(jsonschema.net,NJsonSchema,...),但更喜欢使用一些与公司/项目相关的成熟/官方代码。 I understand that the Newtonsoft.json.Schema package is only able to do it the other way round (C# Class -> JSON)我知道 Newtonsoft.json.Schema 包只能反过来做(C# Class -> JSON)

I have surprisingly found that Visual Studio is able to do this out-of-the box using "Edit -> Paste Special -> Paste JSON as classes".我惊讶地发现 Visual Studio 能够使用“编辑 -> 选择性粘贴 -> 将 JSON 粘贴为类”开箱即用地执行此操作。 Is the code/class/executable/dll that is behind this feature some how accessible programmatically for a pre-build step?此功能背后的代码/类/可执行文件/dll 是否可以通过编程方式访问预构建步骤?

As @Stiefel mentioned, you can use nswag for this .正如@Stiefel 所提到的,您可以为此使用 nswag First add the NSwag.MSBuild nuget package to your project, which also allows you to used the $(NSwagExe) macro to refer to nswag.首先将NSwag.MSBuild nuget 包添加到您的项目中,这也允许您使用$(NSwagExe)宏来引用 nswag。 Then, add a pre-build step to your project, in my case it looks like this:然后,在你的项目中添加一个预构建步骤,在我的例子中它看起来像这样:

<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
    <Exec Command="$(NSwagExe) jsonschema2csclient /name:Manifest /namespace:ManifestCreator.Models /input:$(SolutionDir)schemas\SingleFileSchema.0.1.0.json /output:$(ProjectDir)Models\ManifestSchema.cs" />
</Target>

You should be able to do this with Visual Studio template transformation (T4) files.您应该能够使用 Visual Studio 模板转换 (T4) 文件执行此操作。 A couple years ago I used them for a similar scenario -- generating C# service proxy classes from a proprietary WSDL-like XML format.几年前,我将它们用于类似的场景——从类似 WSDL 的专有 XML 格式生成 C# 服务代理类。 Unfortunately I don't have access to the code any more, but it's pretty easy to figure out once you start Googling for T4 and codegen.不幸的是,我无法再访问代码了,但是一旦您开始使用谷歌搜索 T4 和代码生成器,就很容易弄清楚了。

You may also want to take a look at the extensibility / automation model, aka DTE to figure out whether you can access the built-in feature (which I'd launch from a T4).您可能还想查看可扩展性/自动化模型,即DTE,以确定您是否可以访问内置功能(我将从 T4 启动)。

T4 is really meant for single files (hence "template") but you can just as easily kick off template "builds" from the IDE that generate multiple files. T4 确实适用于单个文件(因此称为“模板”),但您可以轻松地从生成多个文件的 IDE 启动模板“构建”。 The high-level is that you'll import helper assemblies like Json.Net to read the inputs, then use regular old file I/O to write the new files.高级是您将导入像 Json.Net 这样的辅助程序集来读取输入,然后使用常规的旧文件 I/O 来写入新文件。 There isn't much magic to it.它没有太多魔力。 DTE can do things like kick off external processes or present concatenated build logs for human review upon completion. DTE 可以执行诸如启动外部流程或在完成后呈现串联构建日志以供人工审查之类的事情。

A couple caveats: I haven't used T4 in VS2017 yet, but I've read it requires extra steps to start using them, and if you are on an earlier VS there is apparently an issue where they don't initially load with your project after you migrate to VS2017.几个警告:我还没有在 VS2017 中使用 T4,但我读过它需要额外的步骤才能开始使用它们,如果您使用的是较早的 VS,显然存在一个问题,它们最初不会加载您的迁移到 VS2017 后的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM