Skip to content

Commit 5db772a

Browse files
committed
Expose JSON-LD parser and serializer
1 parent 132d407 commit 5db772a

File tree

7 files changed

+55
-16
lines changed

7 files changed

+55
-16
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ repos:
2323
- id: requirements-txt-fixer
2424
- id: trailing-whitespace
2525
- repo: https://github.com/astral-sh/ruff-pre-commit
26-
rev: v0.12.10
26+
rev: v0.13.0
2727
hooks:
2828
- id: ruff
2929
args: [ --fix ]
3030
- id: ruff-format
3131
- repo: https://github.com/pre-commit/mirrors-mypy
32-
rev: v1.17.1
32+
rev: v1.18.1
3333
hooks:
3434
- id: mypy

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ The following formats are supported:
9393
- `ox-nquads` (`ox-nq`)
9494
- `ox-turtle` (`ox-ttl`)
9595
- `ox-trig`
96+
- `ox-n3`
9697
- `ox-xml`
98+
- `ox-json-ld` (`ox-streaming-json-ld` for streaming JSON-LD, note that only JSON-LD 1.0 is supported)
9799

98100
Note that Oxigraph parser and serializers are not 1:1 compatible with the rdflib ones and some minor differences exist.
99101

pyproject.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ classifiers = [
1717
"Programming Language :: Python :: 3.11",
1818
"Programming Language :: Python :: 3.12",
1919
"Programming Language :: Python :: 3.13",
20+
"Programming Language :: Python :: 3.14",
2021
"License :: OSI Approved :: BSD License",
2122
"Topic :: Software Development :: Libraries :: Python Modules",
2223
"Topic :: Database :: Database Engines/Servers",
@@ -40,6 +41,8 @@ Oxigraph = "oxrdflib.store:OxigraphStore"
4041
oxigraph = "oxrdflib.store:OxigraphStore"
4142

4243
[project.entry-points."rdf.plugins.parser"]
44+
ox-json-ld = "oxrdflib.parser:OxigraphJsonLdParser"
45+
ox-streaming-json-ld = "oxrdflib.parser:OxigraphStreamingJsonLdParser"
4346
ox-turtle = "oxrdflib.parser:OxigraphTurtleParser"
4447
ox-ttl = "oxrdflib.parser:OxigraphTurtleParser"
4548
ox-n3 = "oxrdflib.parser:OxigraphN3Parser"
@@ -52,6 +55,8 @@ ox-trig = "oxrdflib.parser:OxigraphTriGParser"
5255
ox-xml = "oxrdflib.parser:OxigraphRdfXmlParser"
5356

5457
[project.entry-points."rdf.plugins.serializer"]
58+
ox-json-ld = "oxrdflib.serializer:OxigraphJsonLdSerializer"
59+
ox-streaming-json-ld = "oxrdflib.serializer:OxigraphJsonLdSerializer"
5560
ox-turtle = "oxrdflib.serializer:OxigraphTurtleSerializer"
5661
ox-ttl = "oxrdflib.serializer:OxigraphTurtleSerializer"
5762
ox-n3 = "oxrdflib.serializer:OxigraphN3Serializer"

src/oxrdflib/parser.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from abc import ABC, abstractmethod
2-
from typing import Optional
2+
from typing import Final, Optional
33

44
from pyoxigraph import DefaultGraph, RdfFormat, parse
55
from rdflib import Graph
@@ -16,10 +16,12 @@
1616
from oxrdflib.store import OxigraphStore
1717

1818
__all__ = [
19+
"OxigraphJsonLdParser",
1920
"OxigraphN3Parser",
2021
"OxigraphNQuadsParser",
2122
"OxigraphNTriplesParser",
2223
"OxigraphRdfXmlParser",
24+
"OxigraphStreamingJsonLdParser",
2325
"OxigraphTriGParser",
2426
"OxigraphTurtleParser",
2527
]
@@ -73,25 +75,33 @@ def _format(self) -> RdfFormat:
7375
pass
7476

7577

78+
class OxigraphJsonLdParser(_OxigraphParser):
79+
_format: Final = RdfFormat.JSON_LD
80+
81+
82+
class OxigraphStreamingJsonLdParser(_OxigraphParser):
83+
_format: Final = RdfFormat.STREAMING_JSON_LD
84+
85+
7686
class OxigraphTurtleParser(_OxigraphParser):
77-
_format = RdfFormat.TURTLE
87+
_format: Final = RdfFormat.TURTLE
7888

7989

8090
class OxigraphNTriplesParser(_OxigraphParser):
81-
_format = RdfFormat.N_TRIPLES
91+
_format: Final = RdfFormat.N_TRIPLES
8292

8393

8494
class OxigraphRdfXmlParser(_OxigraphParser):
85-
_format = RdfFormat.RDF_XML
95+
_format: Final = RdfFormat.RDF_XML
8696

8797

8898
class OxigraphN3Parser(_OxigraphParser):
89-
_format = RdfFormat.N3
99+
_format: Final = RdfFormat.N3
90100

91101

92102
class OxigraphNQuadsParser(_OxigraphParser):
93-
_format = RdfFormat.N_QUADS
103+
_format: Final = RdfFormat.N_QUADS
94104

95105

96106
class OxigraphTriGParser(_OxigraphParser):
97-
_format = RdfFormat.TRIG
107+
_format: Final = RdfFormat.TRIG

src/oxrdflib/serializer.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from abc import ABC, abstractmethod
2-
from typing import IO, Optional
2+
from typing import IO, Final, Optional
33

44
from pyoxigraph import RdfFormat, serialize
55
from rdflib import Dataset
@@ -9,6 +9,7 @@
99
from oxrdflib.store import OxigraphStore
1010

1111
__all__ = [
12+
"OxigraphJsonLdSerializer",
1213
"OxigraphN3Serializer",
1314
"OxigraphNQuadsSerializer",
1415
"OxigraphNTriplesSerializer",
@@ -47,25 +48,29 @@ def _format(self) -> RdfFormat:
4748
pass
4849

4950

51+
class OxigraphJsonLdSerializer(_OxigraphSerializer):
52+
_format: Final = RdfFormat.JSON_LD
53+
54+
5055
class OxigraphN3Serializer(_OxigraphSerializer):
51-
_format = RdfFormat.N3
56+
_format: Final = RdfFormat.N3
5257

5358

5459
class OxigraphTurtleSerializer(_OxigraphSerializer):
55-
_format = RdfFormat.TURTLE
60+
_format: Final = RdfFormat.TURTLE
5661

5762

5863
class OxigraphNTriplesSerializer(_OxigraphSerializer):
59-
_format = RdfFormat.N_TRIPLES
64+
_format: Final = RdfFormat.N_TRIPLES
6065

6166

6267
class OxigraphRdfXmlSerializer(_OxigraphSerializer):
63-
_format = RdfFormat.RDF_XML
68+
_format: Final = RdfFormat.RDF_XML
6469

6570

6671
class OxigraphNQuadsSerializer(_OxigraphSerializer):
67-
_format = RdfFormat.N_QUADS
72+
_format: Final = RdfFormat.N_QUADS
6873

6974

7075
class OxigraphTriGSerializer(_OxigraphSerializer):
71-
_format = RdfFormat.TRIG
76+
_format: Final = RdfFormat.TRIG

tests/test_parser.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ def test_parse_graph(self) -> None:
2020
for store in ("default", "oxigraph"):
2121
for transactional in (True, False):
2222
for fmt, serialization in (
23+
("ox-json-ld", '{"@context":{"v":"http://example.com/vocab#"},"@id":"s","v:p":{"@id":"o"}}'),
24+
(
25+
"ox-streaming-json-ld",
26+
'{"@context":{"v":"http://example.com/vocab#"},"@id":"s","v:p":{"@id":"o"}}',
27+
),
2328
("ox-turtle", "@prefix v: <http://example.com/vocab#> . <s> v:p <o> ."),
2429
("ox-ttl", "@prefix v: <http://example.com/vocab#> . <s> v:p <o> ."),
2530
("ox-ntriples", "<http://example.com/s> <http://example.com/vocab#p> <http://example.com/o> .\n"),

tests/test_serializer.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ class TestSerializer(unittest.TestCase):
1212
def test_serialize_graph(self) -> None:
1313
for store in ("default", "oxigraph"):
1414
for fmt, serialization in (
15+
(
16+
"ox-json-ld",
17+
'[{"@id":"http://example.com/s","http://example.com/vocab#p":[{"@id":"http://example.com/o"}]}]',
18+
),
19+
(
20+
"ox-streaming-json-ld",
21+
'[{"@id":"http://example.com/s","http://example.com/vocab#p":[{"@id":"http://example.com/o"}]}]',
22+
),
1523
("ox-turtle", "<s> v:p <o> .\n"),
1624
("ox-ttl", "<s> v:p <o> .\n"),
1725
("ox-ntriples", "<http://example.com/s> <http://example.com/vocab#p> <http://example.com/o> .\n"),
@@ -39,6 +47,10 @@ def test_serialize_graph(self) -> None:
3947
def test_serialize_dataset(self) -> None:
4048
for store in ("default", "oxigraph"):
4149
for fmt, serialization in (
50+
(
51+
"ox-json-ld",
52+
'[{"@id":"http://example.com/g","@graph":[{"@id":"http://example.com/s","http://example.com/vocab#p":[{"@id":"http://example.com/o"}]}]}]',
53+
),
4254
(
4355
"ox-nquads",
4456
"<http://example.com/s> <http://example.com/vocab#p> "

0 commit comments

Comments
 (0)