Skip to content

Commit 14d1006

Browse files
lu-plashleysommer
andauthored
Issue #2812: Reflect explicitly XSD-typed Literals in JSON-LD serialization (#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]>
1 parent dc5da9b commit 14d1006

File tree

5 files changed

+67
-15
lines changed

5 files changed

+67
-15
lines changed

rdflib/plugins/serializers/jsonld.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -393,11 +393,8 @@ def to_raw_value(
393393
else:
394394
v = str(o)
395395
if o.datatype:
396-
if native:
397-
if self.context.active:
398-
return v
399-
else:
400-
return {context.value_key: v}
396+
if native and self.context.active:
397+
return v
401398
return {
402399
context.type_key: context.to_symbol(o.datatype),
403400
context.value_key: v,
Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
[
2-
{
3-
"@id": "http://example.com/Subj1",
4-
"http://example.com/prop": [
5-
{ "@value": true },
6-
{ "@value": false },
7-
{ "@value": 1 },
8-
{ "@value": "1.1", "@type": "http://www.w3.org/2001/XMLSchema#decimal"},
9-
{ "@value": 0.11 }
10-
]
11-
}
2+
{
3+
"@id": "http://example.com/Subj1",
4+
"http://example.com/prop": [
5+
{
6+
"@type": "http://www.w3.org/2001/XMLSchema#boolean",
7+
"@value": true
8+
},
9+
{
10+
"@type": "http://www.w3.org/2001/XMLSchema#boolean",
11+
"@value": false
12+
},
13+
{
14+
"@type": "http://www.w3.org/2001/XMLSchema#integer",
15+
"@value": 1
16+
},
17+
{
18+
"@type": "http://www.w3.org/2001/XMLSchema#decimal",
19+
"@value": "1.1"
20+
},
21+
{
22+
"@type": "http://www.w3.org/2001/XMLSchema#double",
23+
"@value": 0.11
24+
}
25+
]
26+
}
1227
]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<http://example.com/Subj1> <http://example.com/prop> "true"^^<http://www.w3.org/2001/XMLSchema#boolean> .
2+
<http://example.com/Subj1> <http://example.com/prop> "false" .
3+
<http://example.com/Subj1> <http://example.com/prop> "1" .
4+
<http://example.com/Subj1> <http://example.com/prop> "1.1"^^<http://www.w3.org/2001/XMLSchema#decimal> .
5+
<http://example.com/Subj1> <http://example.com/prop> "1.1E-1"^^<http://www.w3.org/2001/XMLSchema#double> .
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[
2+
{
3+
"@id": "http://example.com/Subj1",
4+
"http://example.com/prop": [
5+
{
6+
"@type": "http://www.w3.org/2001/XMLSchema#boolean",
7+
"@value": true
8+
},
9+
{
10+
"@value": "false"
11+
},
12+
{
13+
"@value": "1"
14+
},
15+
{
16+
"@type": "http://www.w3.org/2001/XMLSchema#decimal",
17+
"@value": "1.1"
18+
},
19+
{
20+
"@type": "http://www.w3.org/2001/XMLSchema#double",
21+
"@value": 0.11
22+
}
23+
]
24+
}
25+
]

test/jsonld/test-suite/tests/fromRdf-manifest.jsonld

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,16 @@
145145
},
146146
"input": "fromRdf-0019-in.nq",
147147
"expect": "fromRdf-0019-out.jsonld"
148+
}, {
149+
"@id": "#t0020",
150+
"@type": ["jld:PositiveEvaluationTest", "jld:FromRDFTest"],
151+
"name": "Mixed explicit XSD typing",
152+
"purpose": "Explicitly XSD-typed Literals in the input are reflected in the JSON-LD output.",
153+
"option": {
154+
"useNativeTypes": true
155+
},
156+
"input": "fromRdf-0020-in.nq",
157+
"expect": "fromRdf-0020-out.jsonld"
148158
}
149159
]
150160
}

0 commit comments

Comments
 (0)