RDFLib / rdflib-jsonld

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

Doesn't interpret newlines with python3 #82

Open leliamesteban opened 4 years ago

leliamesteban commented 4 years ago

I ran the code from the README.md and I get this output:

b'[\n    {\n        "@id": "file:///home/liam/fyp/#hasAllergens",\n        "@type": [\n            "http://www.w3.org/2000/01/rdf-schema#Property"\n        ]\n    },\n    {\n        "@id": "file:///home/liam/fyp/#Ingredient",\n        "@type": [\n            "http://www.w3.org/2000/01/rdf-schema#Class"\n        ]\n    },\n    {\n        "@id": "file:///home/liam/fyp/#Menu",\n        "@type": [\n            "http://www.w3.org/2000/01/rdf-schema#Class"\n        ],\n        "http://www.w3.org/2000/01/rdf-schema#subClassOf": [\n            {\n                "@id": "file:///home/liam/fyp/#Collection"\n            }\n        ]\n    },\n    {\n        "@id": "file:///home/liam/fyp/",\n        "http://purl.org/dc/elements/1.1/title": [\n            {\n                "@value": "Primer - Getting into the Semantic Web and RDF using N3"\n            }\n        ]\n    },\n    {\n        "@id": "file:///home/liam/fyp/#Recipe",\n        "@type": [\n            "http://www.w3.org/2000/01/rdf-schema#Class"\n        ]\n    },\n    {\n        "@id": "file:///home/liam/fyp/#StageSequence",\n        "@type": [\n            "http://www.w3.org/2000/01/rdf-schema#Class"\n        ]\n    },\n    {\n        "@id": "file:///home/liam/fyp/#Food",\n        "@type": [\n            "http://www.w3.org/2000/01/rdf-schema#Class"\n        ]\n    },\n    {\n        "@id": "file:///home/liam/fyp/#Collection",\n        "@type": [\n            "http://www.w3.org/2000/01/rdf-schema#Class"\n        ]\n    }\n]'
b'{\n    "@context": {\n        "@language": "en",\n        "@vocab": "http://purl.org/dc/terms/"\n    },\n    "@graph": [\n        {\n            "@id": "file:///home/liam/fyp/#hasAllergens",\n            "@type": "http://www.w3.org/2000/01/rdf-schema#Property"\n        },\n        {\n            "@id": "file:///home/liam/fyp/#Ingredient",\n            "@type": "http://www.w3.org/2000/01/rdf-schema#Class"\n        },\n        {\n            "@id": "file:///home/liam/fyp/#Menu",\n            "@type": "http://www.w3.org/2000/01/rdf-schema#Class",\n            "http://www.w3.org/2000/01/rdf-schema#subClassOf": {\n                "@id": "file:///home/liam/fyp/#Collection"\n            }\n        },\n        {\n            "@id": "file:///home/liam/fyp/",\n            "http://purl.org/dc/elements/1.1/title": {\n                "@value": "Primer - Getting into the Semantic Web and RDF using N3"\n            }\n        },\n        {\n            "@id": "file:///home/liam/fyp/#Recipe",\n            "@type": "http://www.w3.org/2000/01/rdf-schema#Class"\n        },\n        {\n            "@id": "file:///home/liam/fyp/#StageSequence",\n            "@type": "http://www.w3.org/2000/01/rdf-schema#Class"\n        },\n        {\n            "@id": "file:///home/liam/fyp/#Food",\n            "@type": "http://www.w3.org/2000/01/rdf-schema#Class"\n        },\n        {\n            "@id": "file:///home/liam/fyp/#Collection",\n            "@type": "http://www.w3.org/2000/01/rdf-schema#Class"\n        }\n    ]\n}'

The \n newlines are not interpeted and it's difficult to see the structure of the output. This is the case when using python3 but not python2.7.

hsolbrig commented 4 years ago

We probably need to tweak the readme.

The code should say: print(g.serialize(format='json-ld', indent=4).decode())

The default serialization is bytes, not UTF8

hsolbrig commented 4 years ago

Oops - managed to accidentally push the proposed change into the 1.1 branch w/o a pull request. Apologies...

raghav17083 commented 4 years ago

Is this issue still open? As in, do we have to use .decode() after calling .serialize() on a graph?