titanium-json-ld icon indicating copy to clipboard operation
titanium-json-ld copied to clipboard

Weird behvior with framing and native language mappings

Open DavideRossi opened this issue 6 months ago • 0 comments

Describe the bug I'm failing to perform round-trip conversions JSON-LD/RDF with WOT Thing Descriptions. The pipeline should be: JSON-LD -> N-Quads -> expanded JSON-LD -> framed JSON-LD. The conversion works with other libraries, you can even perform the pipeline using online tools like the LSON-LD Playground (for JSON-LD to N-Quads and Framing) and RDF Distiller (for N-Quads to JSON-LD). I've been able to restrict the problem by using the following toy TD with a simplified context:

{
    "@context": {
        "td": "https://www.w3.org/2019/wot/td#",
        "jsonschema": "https://www.w3.org/2019/wot/json-schema#",
        "xsd": "http://www.w3.org/2001/XMLSchema#",
        "id": "@id",
        "properties": {
            "@id": "td:hasPropertyAffordance",
            "@type": "@id",
            "@container": "@index",
            "@index": "name",
            "@context": {
                "td": "https://www.w3.org/2019/wot/td#",
                "jsonschema": "https://www.w3.org/2019/wot/json-schema#",
                "type": {
                    "@id": "@type"
                },
                "readOnly": {
                    "@id": "jsonschema:readOnly"
                },
                "boolean": "jsonschema:BooleanSchema",
                "string": "jsonschema:StringSchema",
                "properties": {
                    "@id": "jsonschema:properties",
                    "@container": "@index",
                    "@index": "name",
                    "@context": {
                        "properties": {
                            "@id": "jsonschema:properties",
                            "@container": "@index",
                            "@index": "propertyName"
                        }
                    }
                },
                "propertyName": {
                    "@id": "jsonschema:propertyName"
                },
                "unit": {
                    "@id": "schema:unitCode",
                    "@type": "@vocab"
                }
            }
        },
        "Thing": {
            "@id": "td:Thing"
        },
        "name": {
            "@id": "td:name"
        },
        "title": {
            "@id": "td:title",
            "@language": "en"
        }
    },
    "id": "urn:uuid:0804d572-cce8-422a-bb7c-4412fcd56f06",
    "@type": "Thing",
    "title": "MyThing Title",
    "name": "MyThing Name",
    "properties": {
        "status": {
            "type": "string",
            "readOnly": false
        },
        "isOn": {
            "type": "boolean"
        }
    }
}

The conversion using the JSON-LD playground and Titanium lead to the same result:

<urn:uuid:0804d572-cce8-422a-bb7c-4412fcd56f06> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://www.w3.org/2019/wot/td#Thing> .
<urn:uuid:0804d572-cce8-422a-bb7c-4412fcd56f06> <https://www.w3.org/2019/wot/td#hasPropertyAffordance> _:b0 .
<urn:uuid:0804d572-cce8-422a-bb7c-4412fcd56f06> <https://www.w3.org/2019/wot/td#hasPropertyAffordance> _:b1 .
<urn:uuid:0804d572-cce8-422a-bb7c-4412fcd56f06> <https://www.w3.org/2019/wot/td#name> "MyThing Name" .
<urn:uuid:0804d572-cce8-422a-bb7c-4412fcd56f06> <https://www.w3.org/2019/wot/td#title> "MyThing Title"@en .
_:b0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://www.w3.org/2019/wot/json-schema#BooleanSchema> .
_:b0 <https://www.w3.org/2019/wot/td#name> "isOn" .
_:b1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://www.w3.org/2019/wot/json-schema#StringSchema> .
_:b1 <https://www.w3.org/2019/wot/json-schema#readOnly> "false"^^<http://www.w3.org/2001/XMLSchema#boolean> .
_:b1 <https://www.w3.org/2019/wot/td#name> "status" .

The conversion to N-Quads lead to a basically equal results using Titanium and RDF Distiller (the difference being that RDF Distiller also performs a compaction). Here is Titanium's version:

[
    {
        "@id": "_:b0",
        "@type": [
            "https://www.w3.org/2019/wot/json-schema#StringSchema"
        ],
        "https://www.w3.org/2019/wot/json-schema#readOnly": [
            {
                "@value": false
            }
        ],
        "https://www.w3.org/2019/wot/td#name": [
            {
                "@value": "status"
            }
        ]
    },
    {
        "@id": "_:b1",
        "@type": [
            "https://www.w3.org/2019/wot/json-schema#BooleanSchema"
        ],
        "https://www.w3.org/2019/wot/td#name": [
            {
                "@value": "isOn"
            }
        ]
    },
    {
        "@id": "urn:uuid:0804d572-cce8-422a-bb7c-4412fcd56f06",
        "@type": [
            "https://www.w3.org/2019/wot/td#Thing"
        ],
        "https://www.w3.org/2019/wot/td#hasPropertyAffordance": [
            {
                "@id": "_:b0"
            },
            {
                "@id": "_:b1"
            }
        ],
        "https://www.w3.org/2019/wot/td#name": [
            {
                "@value": "MyThing Name"
            }
        ],
        "https://www.w3.org/2019/wot/td#title": [
            {
                "@value": "MyThing Title",
                "@type": "http://www.w3.org/1999/02/22-rdf-syntax-ns#langString"
            }
        ]
    }
]

When I try to frame the results with the very same @context contained in the original JSON-LD (with the addition of "@type": "Thing") things fall apart. JSON-LD Playground returns this document (@context omitted):

  "id": "urn:uuid:0804d572-cce8-422a-bb7c-4412fcd56f06",
  "@type": "Thing",
  "properties": {
    "isOn": {
      "type": "boolean"
    },
    "status": {
      "type": "string",
      "readOnly": false
    }
  },
  "name": "MyThing Name",
  "title": "MyThing Title"
}

The conversion is online here. Titanium, instead, returns:

{
    "id": "urn:uuid:0804d572-cce8-422a-bb7c-4412fcd56f06",
    "@type": "Thing",
    "properties": {
        "status": {
            "type": "jsonschema:StringSchema",
            "readOnly": false
        },
        "isOn": {
            "type": "jsonschema:BooleanSchema"
        }
    },
    "name": "MyThing Name",
    "td:title": {
        "@value": "MyThing Title",
        "@type": "http://www.w3.org/1999/02/22-rdf-syntax-ns#langString"
      }
    }
}

The only difference w.r.t. the options used in the various conversions is that for N-Quards to JSON-LD I had to set setUseNativeTypes to true in RDF Distiller otherwise the resulting JSON-LD cannot be framed correctly. If I use the same option in Titanium things get even messier. But I think this is another problem. The relevant problem now is the final framing. I can make it work correctly with JSON-LD playground and I found no way for doing that with Titanium (of course I tried with various options, but no success).

To Reproduce See above

Expected behavior Same behavior as jsonld.js

DavideRossi avatar Aug 16 '24 10:08 DavideRossi