Skip to content

Commit

Permalink
Issue #2812: Reflect explicitly XSD-typed Literals in JSON-LD seriali…
Browse files Browse the repository at this point in the history
…zation (#2889)

* feat: Reflect explicitly XSD-typed Literals in JSON-LD serialization

Supplying an XSD type argument to the rdflib.Literal datatype
parameter should be reflected in JSON-LD serializations.

Closes: #2812

* test: Add/modify tests for XSD-typed JSON-LD serialization

Modify test "t#0018" in JSON-LD test-suite: Add XSD types to the
expected JSON-LD output.

Add test "t#0020" in JSON-LD test-suite: Add another test with mixed
explicit typing in the input source.

---------

Co-authored-by: Ashley Sommer <[email protected]>
  • Loading branch information
lu-pl and ashleysommer authored Sep 29, 2024
1 parent dc5da9b commit 14d1006
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 15 deletions.
7 changes: 2 additions & 5 deletions rdflib/plugins/serializers/jsonld.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,11 +393,8 @@ def to_raw_value(
else:
v = str(o)
if o.datatype:
if native:
if self.context.active:
return v
else:
return {context.value_key: v}
if native and self.context.active:
return v
return {
context.type_key: context.to_symbol(o.datatype),
context.value_key: v,
Expand Down
35 changes: 25 additions & 10 deletions test/jsonld/test-suite/tests/fromRdf-0018-out.jsonld
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
[
{
"@id": "http://example.com/Subj1",
"http://example.com/prop": [
{ "@value": true },
{ "@value": false },
{ "@value": 1 },
{ "@value": "1.1", "@type": "http://www.w3.org/2001/XMLSchema#decimal"},
{ "@value": 0.11 }
]
}
{
"@id": "http://example.com/Subj1",
"http://example.com/prop": [
{
"@type": "http://www.w3.org/2001/XMLSchema#boolean",
"@value": true
},
{
"@type": "http://www.w3.org/2001/XMLSchema#boolean",
"@value": false
},
{
"@type": "http://www.w3.org/2001/XMLSchema#integer",
"@value": 1
},
{
"@type": "http://www.w3.org/2001/XMLSchema#decimal",
"@value": "1.1"
},
{
"@type": "http://www.w3.org/2001/XMLSchema#double",
"@value": 0.11
}
]
}
]
5 changes: 5 additions & 0 deletions test/jsonld/test-suite/tests/fromRdf-0020-in.nq
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<http://example.com/Subj1> <http://example.com/prop> "true"^^<http://www.w3.org/2001/XMLSchema#boolean> .
<http://example.com/Subj1> <http://example.com/prop> "false" .
<http://example.com/Subj1> <http://example.com/prop> "1" .
<http://example.com/Subj1> <http://example.com/prop> "1.1"^^<http://www.w3.org/2001/XMLSchema#decimal> .
<http://example.com/Subj1> <http://example.com/prop> "1.1E-1"^^<http://www.w3.org/2001/XMLSchema#double> .
25 changes: 25 additions & 0 deletions test/jsonld/test-suite/tests/fromRdf-0020-out.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[
{
"@id": "http://example.com/Subj1",
"http://example.com/prop": [
{
"@type": "http://www.w3.org/2001/XMLSchema#boolean",
"@value": true
},
{
"@value": "false"
},
{
"@value": "1"
},
{
"@type": "http://www.w3.org/2001/XMLSchema#decimal",
"@value": "1.1"
},
{
"@type": "http://www.w3.org/2001/XMLSchema#double",
"@value": 0.11
}
]
}
]
10 changes: 10 additions & 0 deletions test/jsonld/test-suite/tests/fromRdf-manifest.jsonld
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@
},
"input": "fromRdf-0019-in.nq",
"expect": "fromRdf-0019-out.jsonld"
}, {
"@id": "#t0020",
"@type": ["jld:PositiveEvaluationTest", "jld:FromRDFTest"],
"name": "Mixed explicit XSD typing",
"purpose": "Explicitly XSD-typed Literals in the input are reflected in the JSON-LD output.",
"option": {
"useNativeTypes": true
},
"input": "fromRdf-0020-in.nq",
"expect": "fromRdf-0020-out.jsonld"
}
]
}

0 comments on commit 14d1006

Please sign in to comment.