Generate C # classes from JSON Schema

I am creating a C # WCF web service that returns a lot of data in JSON format. Client is an iPad app that is currently being developed by another team. Therefore, I am working on specifications, without example data.
Currently, the JSON string is created by the .net framework, my web service returns a C # object containing all the information, which is then serialized by the framework using DataContracts.

My problem is that the communication specifications contain only JSON Schema files (based on http://json-schema.org/ ). To facilitate development, I would like to generate the corresponding classes in C #, but since the files contain quite a lot of information, and there are a dozen files, I really do not want to create these classes manually.

So, I am looking for a tool that would allow me:

  • To generate C # classes from a JSON schema.
  • Convert JSON schema to XSD file. Then it would be easy to create classes, since there are many tools for generating classes from XSD.

I found many tools for checking a JSON string against a JSON schema or for creating classes from a JSON string, but nothing seems to me to be of any help.
There is JSON.NET , but it seems to be a library, not a tool, and I did not find any information on creating classes with it.

So, if anyone knows the tools or has an idea of ​​how I can generate these classes (I tried a tool that created classes in Java, but I could not get it to work).

+45
json wcf jsonschema
Jun 15 2018-11-11T00:
source share
6 answers

Check out this library on nuget. 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 C # code for all classes defined in the JSON schema.

I need a class that was generated today, but could not find any site that could convert a json scheme to C # classes online, so I used the above library and quickly wrote something quickly http://json2csharp.rohitl.com/ hope this helps.

+14
Apr 05 '16 at 11:23
source share
β€” -

You can use the NJsonSchema library to read the JSON schema or generate it 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 NSwag tools for this ... (it is also based on NJsonSchema)

+5
Oct 24 '15 at 7:09
source share

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

I have not used it myself, so I can’t comment too much about it, but it looks like the json-schema-to-poco tool is suitable for what you need.

For his github readme:

Converts JSON schema files to plain old CLR objects in C #. Useful for working as part of an automatic build process.

+3
Jul 12 '14 at 7:12
source share

I had a need for this today, and I did not see any solid answers to your question, so I was furious. This is not ideal, but it is a good starting point for creating.

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

+1
Jul 25 '14 at 20:33
source share

Take a look at Json.NET Help. There is a Json.Schema namespace that may be useful.

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

Json.NET - Quick Reference and API Documentation Namespace Newtonsoft.Json.Schema Namespaces β–Ί Newtonsoft.Json.Schema

Project Page: http://json.codeplex.com/

0
Jun 15 2018-11-15T00:
source share

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

http://json2csharp.com/

-2
Jul 07 '14 at 19:55
source share



All Articles