.net 从 JSON Schema 生成 C# 类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6358745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generate C# classes from JSON Schema
提问by Julien N
I'm creating a C# WCF Web Service that return a lot of data in a JSON format.
The client is an iPad application that is currently being developped by another team, So I'm working on specifications, without example data.
Currently the JSON string is created by the .net framework, my Web Service is returning a C# object containing all the information that are then serialized by the framework using DataContracts.
我正在创建一个 C# WCF Web 服务,它以 JSON 格式返回大量数据。客户端是一个 iPad 应用程序,目前正在由另一个团队开发,所以我正在制定规范,没有示例数据。
目前 JSON 字符串是由 .net 框架创建的,我的 Web 服务返回一个 C# 对象,其中包含所有信息,然后框架使用 DataContracts 将这些信息序列化。
My problem is that the communication specifications only contain JSON Schema files (based on http://json-schema.org/). In order to facilitate the development I'd like to generate the corresponding classes in C# but as the files contain quite a lot of information and there are a dozen of files, I don't really want to create those classes manually.
我的问题是通信规范仅包含 JSON 架构文件(基于http://json-schema.org/)。为了方便开发,我想在C#中生成相应的类,但是由于文件信息量比较大,而且有十几个文件,我真的不想手动创建这些类。
So I'm looking for a tool that would allow me either :
所以我正在寻找一种工具,可以让我:
- To generate C# classes from a JSON Schema.
- To convert a JSON Schema to an XSD file. Then it would be easy to create the classes as there are plenty of tool to generate classes from XSD.
- 从 JSON 架构生成 C# 类。
- 将 JSON 架构转换为 XSD 文件。然后很容易创建类,因为有很多工具可以从 XSD 生成类。
I found a lot of tools to validate a JSON string against a JSON Schema or to generate classes from the JSON string but nothing that seem to help me.
There is JSON.NETbut it seems to be a library and not a tool and I didn't found any information about generating classes with it.
我找到了很多工具来根据 JSON 模式验证 JSON 字符串或从 JSON 字符串生成类,但似乎没有任何帮助。
有JSON.NET但它似乎是一个库而不是一个工具,我没有找到任何关于用它生成类的信息。
So if anyone knows a tools or has an idea on how I could generate those classes (I tried a tool that create the classes in Java but I couldn't make it work).
因此,如果有人知道工具或对我如何生成这些类有想法(我尝试了一个在 Java 中创建类的工具,但我无法使其工作)。
回答by SDAL
回答by Ro Hit
Look up this library on nuget. The NJsonSchema.CodeGenerationcan be used to generate C# or TypeScript code from a JSON schema:
在 nuget 上查找这个库。的NJsonSchema.CodeGeneration可用于生成从JSON模式C#或打字稿代码:
var generator = new CSharpGenerator(schema);
var file = generator.GenerateFile();
The file variable now contains the C# code for all the classes defined in the JSON schema.
文件变量现在包含 JSON 模式中定义的所有类的 C# 代码。
I needed a class generated today but couldn't find any site that could convert json schema to c# classes online so used the above mentioned library and wrote something up quickly http://json2csharp.rohitl.com/hope this helps.
我需要一个今天生成的类,但找不到任何可以在线将 json 模式转换为 c# 类的网站,所以使用了上面提到的库并快速写了一些东西http://json2csharp.rohitl.com/希望这会有所帮助。
回答by Rico Suter
You can use the library NJsonSchemato read a JSON schema or generate one from a type, and generate a C# class from it.
您可以使用库NJsonSchema读取 JSON 模式或从类型生成一个,并从中生成 C# 类。
If you need a GUI for these tasks, you can try the NSwagStudio GUI from the NSwagtools to do so... (it is also based on NJsonSchema)
如果您需要这些任务的 GUI,您可以尝试使用NSwag工具中的 NSwagStudio GUI来执行此操作...(它也基于 NJsonSchema)
回答by Ogglas
In order to answer this correctly you need to know what version (draft) the Json Schema has.
为了正确回答这个问题,您需要知道 Json Schema 的版本(草案)。
Examples which libraries can handle which Schema (2018-01-19):
哪些库可以处理哪些架构的示例 (2018-01-19):
Json.NET Schema supports draft 3, draft 4, draft 6 (MIT)
Manatee.Json supports draft 4, draft 6, draft 7 (MIT)
NJsonSchema supports draft 4 (Ms-PL)
http://json-schema.org/implementations.html#validator-dotnet
http://json-schema.org/implementations.html#validator-dotnet
With NJsonSchema.CodeGenerationyou can't send the actual JSON in directly either, you first need to convert it to an actual schema (You will often get the error: Unable to cast object of type 'System.String' to type 'NJsonSchema.JsonSchema4otherwise).
由于NJsonSchema.CodeGeneration您也不能直接发送实际的 JSON,您首先需要将其转换为实际的模式(您经常会收到错误:Unable to cast object of type 'System.String' to type 'NJsonSchema.JsonSchema4否则)。
Example with running code, Schemasfolder located at project root:
运行代码示例,Schemas文件夹位于项目根目录:
class Program
{
static void Main(string[] args)
{
var location = Assembly.GetExecutingAssembly().Location;
var path = Path.GetFullPath(Path.Combine(location, @"..\..\..\Schemas"));
var schemaJson = File.ReadAllText($"{path}Test.json");
var schema = JsonSchema4.FromJsonAsync(schemaJson).Result;
var generator = new CSharpGenerator(schema);
var generatedFile = generator.GenerateFile();
}
}
回答by Tim Cooke
So I'm looking for a tool that would allow me either : To generate C# classes from a JSON Schema...
所以我正在寻找一种工具,它可以让我:从 JSON 模式生成 C# 类...
I haven't used it myself so I can't comment too much about it, but it seems like the tool "json-schema-to-poco" would fit what you need.
我自己没有使用过它,所以我不能对此发表太多评论,但似乎工具“ json-schema-to-poco”适合您的需要。
Per its github readme:
根据其 github 自述文件:
Converts JSON schema files into plain old CLR objects in C#. Useful for running as part of an automated build process.
在 C# 中将 JSON 模式文件转换为普通的旧 CLR 对象。用于作为自动构建过程的一部分运行。
回答by Rush Frisby
I had a need for this today and didn't see any solid answers to your question so I whipped this up. It's not perfect but it's a good starting point to build off of.
我今天需要这个,但没有看到你问题的任何可靠答案,所以我提出了这个问题。它并不完美,但它是一个很好的起点。
回答by Bill Sambrone
Here is an online class generator that I've used in the past to generate C# classes from a sample set of JSON data:
这是我过去使用的一个在线类生成器,用于从 JSON 数据示例集生成 C# 类:
回答by Carlos Quintanilla
Have a look at the Help of Json.NET There is a Json.Schema namespace that can be useful.
看看 Json.NET 的帮助有一个 Json.Schema 命名空间很有用。
http://james.newtonking.com/projects/json/help/
http://james.newtonking.com/projects/json/help/
Json.NET - Quick Starts & API Documentation Newtonsoft.Json.Schema Namespace Namespaces ? Newtonsoft.Json.Schema
Json.NET - 快速入门和 API 文档 Newtonsoft.Json.Schema 命名空间命名空间?Newtonsoft.Json.Schema
The project page: http://json.codeplex.com/
项目页面:http: //json.codeplex.com/


