For what is the JsonObjectAttribute.Id?

老子叫甜甜 提交于 2021-02-20 08:35:06

问题


JSON.NET JsonObjectAttribute has a property Id. It's inherited from the JsonContainerAttribute. I cannot find, for what is the Id property is used?


回答1:


It's used by Json.NET Schema to override the default "$id" property value when generating a schema for a type.

E.g. if I have the following type:

[JsonObject(Id = "http://foo.bar/schemas/rootobject.json")]
public class RootObject { }

And auto-generate a schema using JSchemaGenerator as follows:

var schema = new JSchemaGenerator().Generate(typeof(RootObject)).ToString();

The result is (demo fiddle here):

{
  "$id": "http://foo.bar/schemas/rootobject.json",
  "type": "object"
}

When not overridden, the value of "$id" is controlled by the SchemaIdGenerationHandling enumeration.

It was also used by the obsolete JsonSchemaGenerator according to JamesNK:

It was used by JsonSchemaGenerator but that is deprecated.



来源:https://stackoverflow.com/questions/35790617/for-what-is-the-jsonobjectattribute-id

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!