Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For what is the JsonObjectAttribute.Id?

Tags:

c#

json.net

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

like image 779
xmedeko Avatar asked Mar 04 '16 07:03

xmedeko


1 Answers

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.

like image 116
dbc Avatar answered Nov 07 '22 16:11

dbc