开发者

Generate C# classes from JSON Schema [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines guidelines. It is not currently accepting answers.

We don’t 开发者_StackOverflowallow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 4 years ago.

Improve this question

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.

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.

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.

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.NET but it seems to be a library and not a tool and I didn't found any information about generating classes with it.

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).


Visual Studio 2017 has this feature.

From the menu, choose Edit, Paste Special, Paste JSON As Classes. Paste in the JSON and Visual Studio will create the required classes.

Generate C# classes from JSON Schema [closed]


Look up this library on nuget. The NJsonSchema.CodeGeneration can be used to generate C# or TypeScript code from a JSON schema:

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.


You can use the library NJsonSchema to read a JSON schema or generate one from a type, and generate a C# class from it.

If you need a GUI for these tasks, you can try the NSwagStudio GUI from the NSwag tools to do so... (it is also based on NJsonSchema)


In order to answer this correctly you need to know what version (draft) the Json Schema has.

Examples which libraries can handle which Schema (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

With NJsonSchema.CodeGeneration you 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.JsonSchema4 otherwise).

Example with running code, Schemas folder located at project root:

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();
    }
}


So I'm looking for a tool that would allow me either : To generate C# classes from a JSON Schema...

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.

Per its github readme:

Converts JSON schema files into plain old CLR objects in C#. Useful for running as part of an automated build process.


Here is an online class generator that I've used in the past to generate C# classes from a sample set of JSON data:

http://json2csharp.com/


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.

https://gist.github.com/rushfrisby/c8f58f346548bf31e045


Have a look at the Help of Json.NET There is a Json.Schema namespace that can be useful.

http://james.newtonking.com/projects/json/help/

Json.NET - Quick Starts & API Documentation Newtonsoft.Json.Schema Namespace Namespaces ► Newtonsoft.Json.Schema

The project page: http://json.codeplex.com/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜