JSON-LD 压缩和压缩 IRI 作为值



我对 JSON-LD 压缩以及它是否可用于压缩值的 IRI 感到有些困惑。

我有以下 JSON-LD 对象

{
    "@context": {
        "@base": "file:///", 
        "x": "https://example.org/pub/x#", 
        "x-attribute": "https://example.org/pub/x-attribute#",
        "x:purpose": { 
            "@type": "@id"
        }
    },
    "https://example.org/pub/x#purpose": "https://example.org/pub/x-attribute#on"
}

和以下新上下文

{
    "x": "https://example.org/pub/x#",
    "x-attribute": "https://example.org/pub/x-attribute#"
}

我期待...并想要...要得到

{
  "@context": {
    "x": "https://example.org/pub/x#",
    "x-attribute": "https://example.org/pub/x-attribute#"
  },
  "x:purpose": "x-attribute:on"
}

但我最终得到的是

{
  "@context": {
    "x": "https://example.org/pub/x#",
    "x-attribute": "https://example.org/pub/x-attribute#"
  },
  "x:purpose": "https://example.org/pub/x-attribute#on"
}
如果你想

尝试这个,你可以把它插入JSON-LD游乐场。

我怎样才能完成我想做的事情? 即基本上在价值位置使用紧凑型 IRI。

首先快速说明:您没有使用在输入对象的上下文中定义的术语。由于使用的是完整的 URI,因此不会应用@type定义。相反,您应该使用术语 (x:purpose):

{
    "@context": {
        "@base": "file:///", 
        "x": "https://example.org/pub/x#", 
        "x-attribute": "https://example.org/pub/x-attribute#",
        "x:purpose": { 
            "@type": "@id"
        }
    },
    "x:purpose": "https://example.org/pub/x-attribute#on"
}

如果不在数据中使用该术语,则需要指定该值是一个@id,如下所示:

{
    "@context": {
        "@base": "file:///", 
        "x": "https://example.org/pub/x#", 
        "x-attribute": "https://example.org/pub/x-attribute#",
        "x:purpose": { 
            "@type": "@id"
        }
    },
    "https://example.org/pub/x#purpose": {
        "@id": "https://example.org/pub/x-attribute#on"
    }
}

现在,要在将值压缩为 CURIE 的情况下获得所需的效果,您必须指出该值实际上是词汇表的一部分(如果您愿意,可以称为"枚举")。为此,您可以将新上下文更改为:

{
    "x": "https://example.org/pub/x#",
    "x-attribute": "https://example.org/pub/x-attribute#",
    "x:purpose": {
        "@type": "@vocab"
    }
}

这应该给你你想要的结果。

最新更新