2

With Json.NET, is there a way to find out what JSON type (array, object, string, integer etc.) that a given .NET type (or actually a MemberInfo) will be serialized to?

http://www.newtonsoft.com/json/help/html/serializationguide.htm

The documenation above gives a pretty good idea, but is there a public API in Json.NET I can use? Ideally it would also take into account any custom attributes that would affect serialization (like JsonObjectAttribute etc)

4
  • This question doesn't make much sense, as JavaScript is not a typed language. What do you need this exactly for? Knowing your use case can help understanding and answering the question Commented May 4, 2016 at 11:13
  • This question has nothing to do with JavaScript per say. JSON has some basic data types (en.wikipedia.org/wiki/JSON#Data_types.2C_syntax_and_example) and for various reasons I would like to know which of these types a given .NET type will be serialized to (when using Json.NET) Commented May 4, 2016 at 11:37
  • Basically JSON represents a JavaScript object, with JavaScript properties, which are loosely typed (i.e. althoug something is a number, you can use it as a string, for example). I'm asking about the use case, because perhaps there is a different solution for what you need. There are more strict type definitions for JSON, like json-schema.org Commented May 4, 2016 at 11:45
  • Did you see my answer? Didn't it work for you? Commented May 9, 2016 at 8:22

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this looks promising but unfortunately its not free (for commercial use at least, newtonsoft.com/store/jsonschema) so I haven't checked it out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.