RDFLib / rdflib-jsonld

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

Broken stdin (-) handling #94

Closed aucampia closed 3 years ago

aucampia commented 4 years ago

JSON-LD does not correctly process stdin when supplied with -.

Test case:

echo '{"@context": { "@vocab": "http://xmlns.com/foaf/0.1/" }, "@id": "http://example.com", "name": "Example"}' | rdfpipe -i  json-ld -o turtle -

Expected result:

$ echo '{"@context": { "@vocab": "http://xmlns.com/foaf/0.1/" }, "@id": "http://example.com", "name": "Example"}' | rdfpipe -i  json-ld -o turtle -
@prefix : <http://xmlns.com/foaf/0.1/> .

<http://example.com/> :name "Example" .

Actual result:

$ echo '{"@context": { "@vocab": "http://xmlns.com/foaf/0.1/" }, "@id": "http://example.com", "name": "Example"}' | rdfpipe -i  json-ld -o turtle -
Traceback (most recent call last):
  File "/home/iwana/.local/bin/rdfpipe", line 8, in <module>
    sys.exit(main())
  File "/home/iwana/.local/lib/python3.8/site-packages/rdflib/tools/rdfpipe.py", line 183, in main
    parse_and_serialize(args, opts.input_format, opts.guess,
  File "/home/iwana/.local/lib/python3.8/site-packages/rdflib/tools/rdfpipe.py", line 51, in parse_and_serialize
    graph.parse(fpath, format=use_format, **kws)
  File "/home/iwana/.local/lib/python3.8/site-packages/rdflib/graph.py", line 1549, in parse
    context.parse(source, publicID=publicID, format=format, **args)
  File "/home/iwana/.local/lib/python3.8/site-packages/rdflib/graph.py", line 1078, in parse
    parser.parse(source, self, **args)
  File "/home/iwana/.local/lib/python3.8/site-packages/rdflib_jsonld/parser.py", line 83, in parse
    data = source_to_json(source)
  File "/home/iwana/.local/lib/python3.8/site-packages/rdflib_jsonld/util.py", line 28, in source_to_json
    return json.load(StringIO(stream.read().decode('utf-8')))
AttributeError: 'str' object has no attribute 'decode'

Workaround

Specify /dev/stdin instead of -

$ echo '{"@context": { "@vocab": "http://xmlns.com/foaf/0.1/" }, "@id": "http://example.com", "name": "Example"}' | rdfpipe -i  json-ld -o turtle /dev/stdin 
@prefix : <http://xmlns.com/foaf/0.1/> .

<http://example.com/> :name "Example" .
Fak3 commented 3 years ago

Duplicate of #91

aucampia commented 3 years ago

Thanks, closing this then.