在 JSON-LD 中引用另一个资源的正确方法是什么

What is the proper way to reference another resource in JSON-LD

所以我试图围绕 JSON-LD 展开思考,我看到的所有示例主要由嵌入 "linked data" 组成。但我想提供对链接数据的引用(主要是因为将其全部嵌入可能会产生 10MB 的有效负载)。所以我想知道我这样做是否正确。

这是我的:

{
  "@context": "/contexts/Customers.jsonld",
  "@id": "/customers/1",
  "@type": "Customer",
  "sessions": {
    "@id": "/customers/1/sessions",
    "@type": "Session"
  },
  "dealer": "/dealers/2",
  "givenName": "Abe",
  "familyName": "Abrahms",
  /* ... snip ... */
}

我在这里谈论的对链接数据的引用由 sessions 属性 表示。假设这是正确的,那么我需要在我的 Customer 上下文中更改什么?

"@context": {
    "hydra": "http://www.w3.org/ns/hydra/core#",
    "doc": "https://api.waterlinkconnect.com/doc#",
    "Customer": "doc:Customer",
    "givenName": "doc:Customer/givenName",
    "familyName": "doc:Customer/familyName",
    "email":"doc:Customer/email",
    "address":"doc:Customer/address",
    "notes":"doc:Customer/notes",
    "phone1":"doc:Customer/phone1",
    "phone2":"doc:Customer/phone2"
    "sessions": "???????"
}

您只需提供一个 IRI 作为值,例如:

"propertyFoo": { "@id": "https://example.com/some-iri" }

(此处使用 @id 以便 IRI 不会被解释为字符串值。)

因此,您使用 sessions 的示例很好,但是如果您不 want/need,则 没有 提供 @type ] 它。

type coercion

如果这个 属性 总是得到一个 IRI 作为值,你可以在你的 @context:

中定义它
"propertyFoo": 
{ 
  "@id": "https://your-vocabulary.example.com/propertyFoo",
  "@type": "@id"
}

那么你可以在提供值的时候省略@id:

"propertyFoo": "https://example.com/some-iri" 

(如果像这样使用类型强制,您不能为该节点提供额外的属性。)