3

我对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 Playground 。

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

4

1 回答 1

3

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

{
    "@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"
    }
}

那应该会给您想要的结果。

于 2014-01-17T15:47:01.487 回答