RDFLib / rdflib

RDFLib is a Python library for working with RDF, a simple yet powerful language for representing information.
https://rdflib.readthedocs.org
BSD 3-Clause "New" or "Revised" License
2.11k stars 547 forks source link

JSON-LD serialization apparently loses XSD type information. #2812

Open lu-pl opened 3 days ago

lu-pl commented 3 days ago

JSON-LD serialization apparently loses XSD type information.

from rdflib import Graph, Literal, URIRef, XSD

graph: Graph = Graph()
graph.add(
    (
        URIRef("https://test.subject"),
        URIRef("https://test.predicate"),
        Literal("test type", datatype=XSD.string),
    )
)

## 1. check literal type of constructed graph object
# pass
assert all(o.datatype for o in graph.objects())

## 2. check literal type of xml parsed graph object
xml_serialized: str = graph.serialize(format="xml")
graph_xml_parsed: Graph = Graph().parse(data=xml_serialized, format="xml")

# pass
assert all(o.datatype for o in graph_xml_parsed.objects())

## 3. check literal type of json-ld parsed graph object (fails)
json_ld_serialized: str = graph.serialize(format="json-ld")
graph_json_parsed: Graph = Graph().parse(data=json_ld_serialized, format="json-ld")

# fail
assert all(o.datatype for o in graph_json_parsed.objects())