简体   繁体   中英

Get JSON Schema from dynamic (or actually JSON data) using JSON.Net

I'm trying to get JSON Schema from a JSON entity using Newtonsoft JSON.Net.

This works as expected:

dynamic customObject = new
{
    Title = "Test"
};
var schemaGenerator = new JsonSchemaGenerator { };
var schema = schemaGenerator.Generate(customObject.GetType());

This works fine. The .Properties property of the schema shows the 'Title' property with details.

However my source for the schema is a piece of JSON data . So I first serialize it to dynamic :

string json = JsonConvert.SerializeObject(customObject);
dynamic customObjectAfterSerialize = JsonConvert.DeserializeObject<dynamic>(json);

JsonSchemaGenerator schemaGenerator = new JsonSchemaGenerator { };
JsonSchema schemaAfterSerialize = schemaGenerator.Generate(customObjectAfterSerialize.GetType());

But now the .Properties property of the JSON schema object is null .

How can I get a JSON schema from an arbitrary piece of JSON data ?

dynamic in your first example is a reference to an anonymous typed object.

dynamic in your second example is untyped.

It isn't possible to generate a schema from an untyped object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM