RDFLib / rdflib-jsonld

JSON-LD parser and serializer plugins for RDFLib
Other
283 stars 71 forks source link

Use id to return a named graph in rdflib-jsonld parser #76

Open oohlaf opened 4 years ago

oohlaf commented 4 years ago

Hi,

Currently I use the following to manually retrieve the identifier from the source data to make sure I get a graph instance back that contains the context identifier set to whatever was in the ID field of the source json data.

But this requires loading the data twice (one time to just read the id and one time by rdflib-json to parse the data). I can see that there is some get_publicID methods, but passing a ConjunctiveGraph().parse does not return a Graph with the identifier from the source. It's a BNode with random number. So below is my workaround.

Is it possible for the parser to return a named graph based on incoming id value of data?

    json_data = json.loads(data)
    identifier = json_data.get("@id", json_data.get("id", None))
    return ConjunctiveGraph().parse(
        data=data, format=AS_JSONLD_MIMETYPE, publicID=identifier
    )