For what you need, the most appropriate solution is to use JSON schemas. JSON.NET has support for them:
http://www.newtonsoft.com/jsonschema
And one of the features is exactly what you need:
Generate JSON Schemas
Generate JSON Schemas automatically from your existing .NET types.
from its home page.
There are examples on its documentation., like this:
.NET Type
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
Generate JSON schema
JSchemaGenerator generator = new JSchemaGenerator();
JSchema schema = generator.Generate(typeof(Person));
// {
// "type": "object",
// "properties": {
// "Name": {
// "type": [ "string", "null" ]
// },
// "Age": { "type": "integer" }
// },
// "required": [ "Name", "Age" ]
// }
NOTE about license
When I wrote the answer I didn't realized that you need a license for commercial use. This is the license link, included in the comment by the OP, @Oskar.
NOTE about implementing a custom solution
Doing your own implementatin is really complex because the generated JSON depends on JSON.NET configuration, which can include from simple attributes to custom (de)serializers, and several other configurations. So, "predicting" what JSON will be generated by JSON.NET is really hard to do. The easiest way would be:
- to instance a sample .NET object
- serialize it
- and analyze the resulting JSON
Doing this is very difficult, both instancing a valid sample .NET object, and analyzing the resulting JSON. To make the last step easier you could create a .NET object back from the JSON, and analyze it with Reflection. And caching the results of these analyses to avoid too much use of Reflection, which is quite slow.
Unfortunately, there isn't still a JavaScript language service in Visual Studio, but it will be available in the next release. This would make it much easier to analyze the resulting Javascript object. Rememeber that JSON is a literal JavaScript object, which can be easyly instanced, but in the JavaScript domain.