GithubHelp home page GithubHelp logo

rdflib / rdflib-jsonld Goto Github PK

View Code? Open in Web Editor NEW
279.0 33.0 71.0 421 KB

JSON-LD parser and serializer plugins for RDFLib

License: Other

Python 98.62% HTML 1.38%
rdf json json-ld python serializer parser

rdflib-jsonld's Introduction

RDFLib plugin providing JSON-LD parsing and serialization

ARCHIVED

This rdflib plugin is deprecated for, as of the 2021-09-17 release of rdflib 6.0.1, JSON-LD handing has been integrated. All functionality in this package has been removed, as of release 0.6.2.

This plugin is now 'tombstoned' meaning this - 0.6.2 - is a final release and all users of Python > 3.6 are encouraged to move to rdflib > 6.0.1.

If you are forced to keep using Python <= 3.6, you will need to keep using release <= 0.5.0 of this plugin with RDFlib 5.0.0.


This is an implementation of JSON-LD for RDFLib. For more information about this technology, see the JSON-LD website.

This implementation will:

  • read in an JSON-LD formatted document and create an RDF graph
  • serialize an RDF graph to JSON-LD formatted output

Installation

The easiest way to install the RDFLib JSON-LD plugin is directly from PyPi using pip by running the command below:

pip install rdflib-jsonld

Otherwise you can download the source and install it directly by running:

python setup.py install

Using the plug-in JSONLD serializer/parser with RDFLib

The plugin parser and serializer are automatically registered if installed by setuptools.

>>> from rdflib import Graph, plugin
>>> from rdflib.serializer import Serializer

>>> testrdf = """
... @prefix dcterms: <http://purl.org/dc/terms/> .
... <http://example.org/about>
...     dcterms:title "Someone's Homepage"@en .
... """

>>> g = Graph().parse(data=testrdf, format='n3')

>>> print(g.serialize(format='json-ld', indent=4))
{
    "@id": "http://example.org/about",
    "http://purl.org/dc/terms/title": [
        {
            "@language": "en",
            "@value": "Someone's Homepage"
        }
    ]
}

>>> context = {"@vocab": "http://purl.org/dc/terms/", "@language": "en"}
>>> print(g.serialize(format='json-ld', context=context, indent=4))
{
    "@context": {
        "@language": "en",
        "@vocab": "http://purl.org/dc/terms/"
    },
    "@id": "http://example.org/about",
    "title": "Someone's Homepage"
}

Building the Sphinx documentation

If Sphinx is installed, Sphinx documentation can be generated with:

$ python setup.py build_sphinx

The documentation will be created in ./build/sphinx.

Continuous integration tests

Build Status

rdflib-jsonld's People

Contributors

ajnelson-nist avatar edwardbetts avatar garnertb avatar gromgull avatar hsolbrig avatar joernhees avatar lrowe avatar maparent avatar nagesh4193 avatar nicholascar avatar niklasl avatar noirbizarre avatar piyush69 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rdflib-jsonld's Issues

Travis test builds fail on python 2.5, 3.2 and 3.3

seems this happens for quite a while now.
maybe someone who's more into the code (e.g., @niklasl or @gjhiggins) can decide on whether these are actual bugs or wrong configurations of the travis builds.

For the py3 builds i guess one of the problems is a bug in the tests: the implicit "<" comparison of several types when ordering the list in:

key=lambda x: (_ord_key(x), type(x).__name__))

py3 does not allow such comparisons cause they usually make little sense:
on python 2.7: 1 < {} == True
on python 3.3:

>>> 1 < {}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: int() < dict()

In this case it's actually comparing dicts, but that also seems to be problematic in py3:

>>> {} < {}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: dict() < dict()

rdf:List as @list

I'm trying to serialize an rdf:List as a native JSON list. Maybe I'm doing something wrong, or perhaps there's a bug?

import json
import rdflib

from rdflib.plugin import register, Parser, Serializer
register('json-ld', Serializer, 'rdflib_jsonld.serializer', 'JsonLDSerializer')

ex = rdflib.Namespace("http://example.com/ns#")
g = rdflib.ConjunctiveGraph()

u0 = rdflib.URIRef("http://example.com/list")
u1 = rdflib.BNode()
u2 = rdflib.BNode()
u3 = rdflib.BNode()
u4 = rdflib.BNode()


g.add((u0, ex.hasStuff, u1))
g.add((u1, rdflib.RDF.type, rdflib.RDF.List))
g.add((u1, rdflib.RDF.first, u2))
g.add((u2, rdflib.RDF.value, rdflib.Literal(1)))
g.add((u1, rdflib.RDF.rest, u3))
g.add((u3, rdflib.RDF.type, rdflib.RDF.List))
g.add((u3, rdflib.RDF.first, u4))
g.add((u4, rdflib.RDF.value, rdflib.Literal(2)))
g.add((u3, rdflib.RDF.rest, rdflib.RDF.nil))

context = {
    "ex": "http://example.com/ns#",
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "ex:hasStuff": {
        "@container": "@list"
    }
}

jsonld = json.loads(g.serialize(format='json-ld', context=context))

print json.dumps(jsonld, indent=2)

ImportError: No module named jsonld

Hi!

I'm trying to use rdflib-jsonld, but get this error.

I have installed the following dependencies:

rdflib>=3.2.1
rdfextras>=0.2

When I try to run:

from rdflib import Graph, plugin
from rdflib.serializer import Serializer
plugin.register('json-ld', Serializer, 'rdfextras.serializers.jsonld', 'JsonLDSerializer')

testrdf = '''@prefix dc: <http://purl.org/dc/terms/> .
<http://example.org/about> dc:title "Someone's Homepage"@en .'''

g = Graph().parse(data=testrdf, format='n3')

print(g.serialize(format='json-ld', indent=4))

I get error:

print(g.serialize(format='json-ld', indent=4))
  File "venv\lib\site-packages\rdflib\graph.py", line 799, in serialize
    serializer = plugin.get(format, Serializer)(self)
  File "\ven\lib\site-packages\rdflib\plugin.py", line 98, in get
    return p.getClass()
  File "\venv\lib\site-packages\rdflib\plugin.py", line 61, in getClass
    module = __import__(self.module_path, globals(), locals(), [""])
ImportError: No module named jsonld

This is the exact code from jsonld_serializer.py

Can't parse jsonld generated by rdflib-jsonld

I try to serialize schema.org rdfa into jsonld file and store it in a file with suffix '.jsonld' first, and it saved file properly.
after that I try to load graph from this file, and the length of the graph is 0.

updated:

seems that file serialized with auto-compact=True is not properly loaded in large data

Graph.parse converts `https` to `http`

Given,

from rdflib import Graph                                                                                                                   
obj=\
'''
{
    "@context": "https://schema.org",
    "@type": "Movie",
    "name": "Jason Bourne"
}
'''
g = Graph().parse(data=obj, format='json-ld')                                                                                              
list(g.namespaces())

Shouldn't I expect to see ('', rdflib.term.URIRef('https://schema.org/')), and not ('', rdflib.term.URIRef('http://schema.org/')), in the namespace?

Why does https get conflated with http? This is prevents object matching against a specification that uses https://schema.org as the default namespace.

Not to serialize unused namespaces in the graph

I use surfrdf (http://code.google.com/p/surfrdf/) and it pre-fillies with a lot known namespaces and when serialize in XML, serializer only pulls in namespaces referred to in the graph, but when serialized in json-ld, it pulls in all namespaces including the unused ones. Is there an option to tell serializer to just pull in the one that used in the graph?

{
  "@context": {
    "ANNOTATION": "http://www.w3.org/2000/10/annotation-ns#",
    "ANNOTEA": "http://www.w3.org/2002/01/bookmark#",
    "ATOM": "http://atomowl.org/ontologies/atomrdf#",
    "BIBO": "http://purl.org/ontology/bibo/",
    "BIBO_DEGREES": "http://purl.org/ontology/bibo/degrees/",
    "BIBO_EVENTS": "http://purl.org/ontology/bibo/events/",
    "BIBO_ROLES": "http://purl.org/ontology/bibo/roles/",
    "BIBO_STATUS": "http://purl.org/ontology/bibo/status/",
    "CALENDAR": "http://www.w3.org/2002/12/cal/icaltzd#",
    "CFX": "http://ns.cxf.com/2011/core#",
    "CONTACT": "http://www.w3.org/2000/10/swap/pim/contact#",
    "CORRIB_TAX": "http://jonto.corrib.org/taxonomies#",
    "DBLP": "http://www4.wiwiss.fu-berlin.de/dblp/terms.rdf#",
    "DBPEDIA": "http://dbpedia.org/property/",
    "DC": "http://purl.org/dc/elements/1.1/",
    "DCTERMS": "http://purl.org/dc/terms/",
    "DOAP": "http://usefulinc.com/ns/doap#",
    "EVENT": "http://purl.org/NET/c4dm/event.owl#",
    "EXIF": "http://www.w3.org/2003/12/exif/ns/",
    "FOAF": "http://xmlns.com/foaf/0.1/",
    "FRBR": "http://purl.org/vocab/frbr/core#",
    "FRESNEL": "http://www.w3.org/2004/09/fresnel#",
    "FTI": "http://franz.com/ns/allegrograph/2.2/textindex/",
    "GEO": "http://www.w3.org/2003/01/geo/wgs84_pos#",
    "GR": "http://purl.org/goodrelations/v1#",
    "IBIS": "http://purl.org/ibis#",
    "IDEAS": "http://protege.stanford.edu/rdf",
    "IMDB": "http://www.csd.abdn.ac.uk/~ggrimnes/dev/imdb/IMDB#",
    "JDL_STRUCTURE": "http://www.jeromedl.org/structure#",
    "JONTO_DDC": "http://www.corrib.org/jonto/ddc#",
    "JONTO_PKT": "http://www.corrib.org/jonto/pkt#",
    "LUBM": "http://www.lehigh.edu/~zhp2/2004/0401/univ-bench.owl#",
    "MARCONT": "http://www.marcont.org/ontology#",
    "MO": "http://purl.org/ontology/mo/",
    "OWL": "http://www.w3.org/2002/07/owl#",
    "PIM": "http://www.w3.org/2000/10/swap/pim/contact#",
    "RDF": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "RDFS": "http://www.w3.org/2000/01/rdf-schema#",
    "RESUME": "http://captsolo.net/semweb/resume/cv.rdfs#",
    "REVIEW": "http://www.isi.edu/webscripter/communityreview/abstract-review-o#",
    "SERENITY3": "http://serenity.deri.org/imdb#",
    "SIOC": "http://rdfs.org/sioc/ns#",
    "SIOC_SERVICES": "http://rdfs.org/sioc/services#",
    "SIOC_TYPES": "http://rdfs.org/sioc/types#",
    "SKOS": "http://www.w3.org/2004/02/skos/core#",
    "SURF": "http://code.google.com/p/surfrdf/",
    "TIME": "http://www.w3.org/2006/time#",
    "VANN": "http://purl.org/vocab/vann/",
    "VCARD": "http://nwalsh.com/rdf/vCard#",
    "VS": "http://www.w3.org/2003/06/sw-vocab-status/ns#",
    "WGS84_POS": "http://www.w3.org/2003/01/geo/wgs84_pos#",
    "WIKIONT": "http://sw.deri.org/2005/04/wikipedia/wikiont.owl",
    "WORDNET": "http://xmlns.com/wordnet/1.6/",
    "WOT": "http://xmlns.com/wot/0.1/",
    "XFOAF": "http://www.foafrealm.org/xfoaf/0.1/",
    "XSD": "http://www.w3.org/2001/XMLSchema#",
    "YAGO": "http://dbpedia.org/class/yago/",
    "__fallback_namespace": "http://code.google.com/p/surfrdf/",
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "rdfs": "http://www.w3.org/2000/01/rdf-schema#",
    "v": "http://xmlns.com/wordnet/1.6/"
  },
  "@id": "http://id123",
  "@type": "CFX:ABC",
  "CFX:hasChild": {
    "@id": "http://id234"
  }
}
<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF
   xmlns:CFX="http://ns.cxf.com/2011/core#"
   xmlns:RDF="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
>
  <rdf:Description rdf:about="http://id123">
    <RDF:type rdf:resource="http://ns.cxf.com/2011/core#ABC "/>
    <CFX:hasChild rdf:resource="http://id234"/>
  </rdf:Description>
</rdf:RDF>

Error reading JSON from text IO stream under Python 3

I've been getting errors under Python 3.7 when trying to read JSON-LD from a steam opened in text mode.

The error takes this form:

  File ".../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'

(My data is being generated internally by json.dump, so I'm not in a position to provide a binary byte stream.)

I've worked out a fix to rdflib_jsonld/util.py that appears to work for me:

if PY3:
    from io import StringIO, TextIOBase, TextIOWrapper

and

def source_to_json(source):
    # TODO: conneg for JSON (fix support in rdflib's URLInputSource!)
    source = create_input_source(source, format='json-ld')

    stream = source.getByteStream()
    try:
        if PY3:
            if isinstance(stream, TextIOBase):
                use_stream = stream
            else:
                use_stream = TextIOWrapper(stream, encoding='utf-8')
            return json.load(use_stream)

            # json_data = stream.read()
            # if isinstance(json_data, bytes):
            #     json_data = json_data.decode('utf-8')
            # return json.loads(json_data)

            # return json.load(StringIO(json_data))

            # return json.load(StringIO(stream.read().decode('utf-8')))
        else:
            return json.load(stream)
    finally:
        stream.close()

(I also note that the original code uses read(), which reads the entire data into memory, and then streams it back. The modified code here should allow data to be streamed directly into the resulting JSON object.)

(I've left the original code and some of my experiments here as comments.)

A list of '@id'

We use rdflib a while and used to serialize data to rdf/xml. There are multiple toplevel graphs in our data, like it is shown in below. There are two "<rdf:Description rdf:about=…/>" in the data set…

<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF
   xmlns:ns1="http://example.com/ns#"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
>
  <rdf:Description rdf:about="http://example.com/3">
    <rdf:type rdf:resource="http://example.com/ns#Verb_A"/>
    <ns1:hasChild rdf:resource="http://example.com/4"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://example.com/1">
    <rdf:type rdf:resource="http://example.com/ns#Verb_A"/>
    <ns1:hasChild rdf:resource="http://example.com/2"/>
  </rdf:Description>
</rdf:RDF>

When we try to serialize the same graph to json-ld using rdflib-jsonld serializer, the top-level graphs are serialized as “@id” : [ {…} , {…}]
I try this out from the jsonld playground http://json-ld.org/playground/ and it complains that there is syntax error.
{"name":"jsonld.CompactError","message":"Could not expand input before compaction.","details":{"cause":{"name":"jsonld.SyntaxError","message":"Invalid JSON-LD syntax; "@id" value must a string.","details":{}}}}
So is this the bug in jsonld.js or the rdflib-jsonld ?

{
  "@context": {
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "rdfs": "http://www.w3.org/2000/01/rdf-schema#"
  },
  "@id": [
    {
      "@id": "http://example.com/3",
      "@type": "http://example.com/ns#Verb_A",
      "http://example.com/ns#hasChild": {
        "@id": "http://example.com/4"
      }
    },
    {
      "@id": "http://example.com/1",
      "@type": "http://example.com/ns#Verb_A",
      "http://example.com/ns#hasChild": {
        "@id": "http://example.com/2"
      }
    }
  ]
}

not loading jsonld file

I have a valid jsonld file at https://data.hikob.com/osc/parking but rdflib is not loading it. My codes are:

>>> from rdflib import Graph, plugin
INFO:rdflib:RDFLib Version: 4.2.1
>>> g = Graph().parse("https://data.hikob.com/osc/parking",format="json-ld")
>>> g.serialize()
'<?xml version="1.0" encoding="UTF-8"?>\n<rdf:RDF\n   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"\n>\n</rdf:RDF>\n'

Properties with @type:@id do not have namespace prefix while others have it when serializing

I have the following code:

context_jsonld = "https://raw.githubusercontent.com/catalogue-of-services-isa/CPSV-AP/master/releases/2.2.1/CPSV-AP_v2.2.1.jsonld"
jsonldfile = 'output.jsonld'
g.serialize(destination=jsonldfile, format='json-ld', context=context_jsonld, indent=4, encoding='utf-8')

I just noticed that the output file has for example:
....
"dct:identifier": "ca/294WVJ",
"dct:title": {
"@language": "it",
"@value": "Provincia Autonoma di Trento - Servizio turismo e sport"
},
"skos:prefLabel": {
"@language": "it",
"@value": "Provincia Autonoma di Trento - Servizio turismo e sport"
},
"spatial": "http://publications.europa.eu/resource/authority/place/ITA_TRT"

you can see that spatial (indicated as @type:@id in the context, basically an object relation) does not have the namespace while the other properties have.
The output is still correct but I would have expected to see:

"dct:spatial": "http://publications.europa.eu/resource/authority/place/ITA_TRT"

ssl certificates problem

  File "C:\inetpub\wwwroot_chemsem.com\jsonld2ttl.py", line 8, in <module>
    pg = g.parse(sys.argv[1], format='json-ld')
  File "C:\Python27\lib\site-packages\rdflib\graph.py", line 1037, in parse
    parser.parse(source, self, **args)
  File "C:\Python27\lib\site-packages\rdflib_jsonld\parser.py", line 76, in parse
    to_rdf(data, conj_sink, base, context_data)
  File "C:\Python27\lib\site-packages\rdflib_jsonld\parser.py", line 88, in to_rdf
    return parser.parse(data, context, graph)
  File "C:\Python27\lib\site-packages\rdflib_jsonld\parser.py", line 106, in parse
    context.load(l_ctx, context.base)
  File "C:\Python27\lib\site-packages\rdflib_jsonld\context.py", line 192, in load
    self._prep_sources(base, source, sources)
  File "C:\Python27\lib\site-packages\rdflib_jsonld\context.py", line 205, in _prep_sources
    source = source_to_json(source_url)
  File "C:\Python27\lib\site-packages\rdflib_jsonld\util.py", line 23, in source_to_json
    source = create_input_source(source, format='json-ld')
  File "C:\Python27\lib\site-packages\rdflib\parser.py", line 171, in create_input_source
    input_source = URLInputSource(absolute_location, format)
  File "C:\Python27\lib\site-packages\rdflib\parser.py", line 100, in __init__
    file = urlopen(req)
  File "C:\Python27\lib\urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python27\lib\urllib2.py", line 431, in open
    response = self._open(req, data)
  File "C:\Python27\lib\urllib2.py", line 449, in _open
    '_open', req)
  File "C:\Python27\lib\urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 1240, in https_open
    context=self._context)
  File "C:\Python27\lib\urllib2.py", line 1166, in do_open
    h = http_class(host, timeout=req.timeout, **http_conn_args)
  File "C:\Python27\lib\httplib.py", line 1258, in __init__
    context = ssl._create_default_https_context()
  File "C:\Python27\lib\ssl.py", line 440, in create_default_context
    context.load_default_certs(purpose)
  File "C:\Python27\lib\ssl.py", line 391, in load_default_certs
    self._load_windows_store_certs(storename, purpose)
  File "C:\Python27\lib\ssl.py", line 378, in _load_windows_store_certs
    for cert, encoding, trust in enum_certificates(storename):
WindowsError: [Error 5] Access is denied

jsonld-0.3 breaks rdflib tests

rdflib by default doesn't install rdflib-jsonld. so if you run rdflib's tests it will magically skip them in case jsonld can't be found.

I just ran the tests locally (Mac OS X 10.9.5, rdflib 4.2.1-dev, with ./run_tests.py) in a venv where jsonld 0.3 is installed and it shows the following errors:

======================================================================
FAIL: http://code.google.com/p/rdflib/issues/detail?id=5
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/Users/joern/coding/git/rdflib/rdflib/test/test_finalnewline.py", line 30, in testFinalNewline
    assert len(failed)==0, "No final newline for formats: '%s'" % failed
AssertionError: No final newline for formats: 'set(['json-ld'])'

======================================================================
FAIL: test.test_roundtrip.test_cases(('nt', 'json-ld', 'test/nt/keywords-04.nt'),)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/Users/joern/coding/git/rdflib/rdflib/test/test_roundtrip.py", line 62, in roundtrip
    assert rdflib.compare.isomorphic(g1, g2)
AssertionError

======================================================================
FAIL: test.test_roundtrip.test_cases(('nt', 'json-ld', 'test/nt/lists-03.nt'),)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/Users/joern/coding/git/rdflib/rdflib/test/test_roundtrip.py", line 62, in roundtrip
    assert rdflib.compare.isomorphic(g1, g2)
AssertionError

======================================================================
FAIL: test.test_roundtrip.test_cases(('nt', 'json-ld', 'test/nt/lists-04.nt'),)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/Users/joern/coding/git/rdflib/rdflib/test/test_roundtrip.py", line 62, in roundtrip
    assert rdflib.compare.isomorphic(g1, g2)
AssertionError

----------------------------------------------------------------------
Ran 2387 tests in 37.948s

FAILED (SKIP=97, failures=4)

maybe someone could have a look?

once this is fixed, i'm tempted to update travis so it also installs jsonld and tests the rdflib integration...

rdf:type doesn't serialize

Test Case:

test = """
... @Prefix bibo: http://purl.org/ontology/bibo/ .
... @Prefix dc: http://purl.org/dc/elements/1.1/ .
...
... http://example.org/book1 a bibo:Book;
... dc:title "Title" .
... """
g = Graph().parse(data = test, format='n3')
g.serialize(format='json-ld')
Traceback (most recent call last):
File "", line 1, in
File "build/bdist.macosx-10.4-x86_64/egg/rdflib/graph.py", line 817, in serialize
File "build/bdist.macosx-10.4-x86_64/egg/rdflib_jsonld/jsonld_serializer.py", line 74, in serialize
File "build/bdist.macosx-10.4-x86_64/egg/rdflib_jsonld/jsonld_serializer.py", line 100, in to_tree
File "build/bdist.macosx-10.4-x86_64/egg/rdflib_jsonld/ldcontext.py", line 47, in init
File "build/bdist.macosx-10.4-x86_64/egg/rdflib_jsonld/ldcontext.py", line 86, in load
File "build/bdist.macosx-10.4-x86_64/egg/rdflib_jsonld/ldcontext.py", line 98, in _create_term
File "build/bdist.macosx-10.4-x86_64/egg/rdflib_jsonld/ldcontext.py", line 106, in _rec_expand
TypeError: unsupported operand type(s) for +: 'NoneType' and 'unicode'

If the rdf:type is omitted then the (minimal) serialization works as expected.

default graph is not handled correctly

According to the JSON-LD specification:

When a JSON-LD document's top-level structure is an object that contains no other properties than @graph and optionally @context (properties that are not mapped to an IRI or a keyword are ignored), @graph is considered to express the otherwise implicit default graph.

Still, when parsing a JSON-LD file with a default graph into a ConjunctiveGraph,
I end up with the default_context of that ConjunctiveGraph being empty,
and the triples of the default graph stored in another context, identified with a blank node...

Example in the following code:

from StringIO import StringIO
from rdflib import ConjunctiveGraph
from rdflib.plugin import register, Parser
register('application/ld+json', Parser, 'rdflib_jsonld.parser', 'JsonLDParser')

f = StringIO("""
{
    "@context": "http://schema.org/",
    "@graph": [
        { "@id": "http://example.org/data#jdoe",
          "name": "John"
        },
        { "@id": "http://example.org/data#janedoe",
          "name": "Jane"
        },
        { "@id": "http://example.org/data#metadata",
          "@graph": [
              { "@id": "http://example.org/data",
                "creator": "http://example.org/data#janedoe"
              }
          ]
        }
    ]
}
""")

cg = ConjunctiveGraph()
cg.parse(f, format="application/ld+json")
print "conjunctive graph contains %s triples" % len(cg)
print "default graph contains %s triples (expected 2)" % len(cg.default_context)
for i in cg.contexts():
    print "other graph in cg (%s) contains %s triples" % (i.identifier, len(i))

Serialization problem

We use the json-ld plugin to store rdf in a MongoDB database. Now I have upgrade the rdflib-jsonld pluging version and we found a problem. No context field is included when serializing, so, now the complete URI is used as a key, this is a problem because MongoDB does not allow the '.' character in its keys.

Using the example included in your documentation, the searialization should returns:

{
    "@context": {
        "dc": "http://purl.org/dc/terms/",
        "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
        "rdfs": "http://www.w3.org/2000/01/rdf-schema#"
    },
    "@id": "http://example.org/about",
    "dc:title": {
        "@language": "en",
        "@literal": "Someone's Homepage"
    }
}

and it returns:

{
    "@id": "http://example.org/about",
    "http://purl.org/dc/terms/title": {
        "@language": "en",
        "@value": "Someone's Homepage"
    }
}

So, is it now necessary to provide a new type of param when serializing?

base is None when the context is a relative IRI

when the load method of a context object is called (parser.py:83), variable base is None although in the parser.py:80 the base is already set in the constructor.

this caused reading a jsonld document of a hydra api demo in (http://www.markus-lanthaler.com/hydra/api-demo/) ended up in a failure as relative IRI stated in the @context /hydra/api-demo/context/EntryPoint.jsonld is interpreted as local file source.

adding 2 lines below in the beginning of load method (context.py:37) fixes the problem.

if base is None and self.base is not None:
            base = self.base

@base no longer prepended in local names

Hi,

I've noticed that the JSON-LD parser has changed its output w.r.t. prepending the @base URI to local names. The following code used to read this file

from rdflib import Graph

g = Graph()
g.parse('test.csv-metadata.json', format='json-ld')

and the resulting graph contained the triple

N86eb3d54036d4bb98a416ec6022be35b http://www.w3.org/ns/csvw#aboutUrl https://iisg.amsterdam/resource/{_row}

However this has changed and now the triple is

N86eb3d54036d4bb98a416ec6022be35b http://www.w3.org/ns/csvw#aboutUrl {_row}

So it seems either this specific property is not expected to be a URI anymore (but a literal instead), or the parser is not prepending @base to URIs.

Could you help me out with this?

Thanks,
Albert

Multiple id aliases interpreted incorrectly

The following JSON:

{
   "id": "https://monarchinitiative.org/genelist/",
   "kg_source": "Test",
   "type": "biolink:KnowledgeGraph",
   "@context": {
        "id": "@id",
        "type": "@type",
        "z": "@id"
    }
}

Produces the following (incorrect) output:

@prefix ns1: <@> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

[] a <biolink:KnowledgeGraph> ;
    ns1:id "https://monarchinitiative.org/genelist/" .

Removing the alias for "z" produces:

@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<https://monarchinitiative.org/genelist/> a <biolink:KnowledgeGraph> .

Which is what was expected.

Not supporting @base with relative URI reference value

On MacOS, using Python 2.7.5 with:

rdflib==4.2.1
rdflib-jsonld==0.3

The following file parses and returns the expected triples:

{ "@id":            "http://example.com/sampledata/testsite/_annalist_site/../"
, "@base":          "http://example.com/sampledata/testsite/_annalist_site/./"
, "@context":       "site_context.jsonld"
, "rdfs:label":     "Annalist data notebook test site"
, "rdfs:comment":   "Annalist test site metadata"
, "annal:comment":  "Initialized from `sampledata/init/annalist_site/_annalist_site/site_meta.jsonld`"
}

But this file parses OK but returns no triples:

{ "@id":            "../"
, "@base":          "http://example.com/sampledata/testsite/_annalist_site/./"
, "@context":       "site_context.jsonld"
, "rdfs:label":     "Annalist data notebook test site"
, "rdfs:comment":   "Annalist test site metadata"
, "annal:comment":  "Initialized from `sampledata/init/annalist_site/_annalist_site/site_meta.jsonld`"
}

The context file I'm using is this:

{
  "@context": {
    "@base": ".",
    "annal": "http://purl.org/annalist/#",
    "owl": "http://www.w3.org/2002/07/owl#",
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "rdfs": "http://www.w3.org/2000/01/rdf-schema#",
    "xsd": "http://www.w3.org/2001/XMLSchema#"
  }
}

In case it's relevant, the code I'm using to do the parsing test looks like this:

    from rdflib import Graph
    g = Graph()
    s = self.testsite._read_stream()
    b = "file://" + os.path.join(TestBaseDir, layout.SITEDATA_DIR) + "/"
    result = g.parse(source=s, publicID=b, format="json-ld")
    print "***** g:"
    print(g.serialize(format='turtle', indent=4))

(The line s = ... returns a file-like object returning my purported JSON-LD data.)

This is important for me as I'm trying to create "portable" directory structures that can be interpreted as RDF using the current base URI of the data. My reading of the JSON-LD spec is that relative references should be allowed for "@id" as long as there is an active base URI. (I also want the @base to be relative to the document location, but that's a separate concern.)

Serialization problem: Form dict to List?

Hi, I have updated the lib, and it has started to fail. When serializing to json-ld my code was specting a dict, but now it returns a list. tring the example of your documentation:

testrdf = '''
... @prefix dc:  .
... 
...     dc:title "Someone's Homepage"@en .
... ''

g = Graph().parse(data=testrdf, format='n3')

print(g.serialize(format='json-ld', indent=4))

It returns:

[
    {
        "@id": "http://example.org/about",
        "http://purl.org/dc/terms/title": [
            {
                "@language": "en",
                "@value": "Someone's Homepage"
            }
        ]
    }
]

and it should return:

{
    "@id": "http://example.org/about",
    "http://purl.org/dc/terms/title": [
        {
            "@language": "en",
            "@value": "Someone's Homepage"
        }
    ]
}

Note that it returns a list containing the spected result instead of the result itself. However, the second example (the example passing a new context) is correct, the serialization returns exactly the same as in your example.

Is this the expected behaviour?

Handling urn : unsupported operand type(s) for +: 'NoneType' and 'unicode'

Hi,
I was trying to output a simple json-ld document from the following input :

@prefix lt: 
<urn:lt:onto:> .

<urn:lt:members:f.biville> a lt:Person;
    lt:email "[email protected]" .

And i got the following error at build/bdist.macosx-10.4-x86_64/egg/rdflib_jsonld/ldcontext.py in _rec_expand at the line 119 using latest master.

Context was :

nxt u'lt:onto:'
expr u'urn:lt:onto:'
is_term False
pfx u'urn'
prev None
data {'lt': u'urn:lt:onto:',
 'rdf': u'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
 'rdfs': u'http://www.w3.org/2000/01/rdf-schema#'}
self    <rdflib_jsonld.ldcontext.Context object at 0x104c81950>

It seems that the converter is trying to match a "binded namespace : URN" which is a bit confusing.
What did i do wrong ?

Regards,

Olivier.

IRI_KEY undefined

Line 171 of ldcontext.py should say ID_KEY not IRI_KEY I believe.

@type:@id not supported for @container:@list?

@type:@id doesn't work for coercing URI strings into resources, when used with @container:@list.

Test case:

{
  "@context": {
    "sc": "http://www.shared-canvas.org/ns#",
    "canvases" : {"@container":"@list", "@id":"sc:contents", "@type":"@id"},
    "structures" : {"@container":"@list", "@id":"sc:contents", "@type":"@id"}
  },
  "@id" : "http://example.org/iiif/seq/1",
  "structures": [
    {
        "@id": "http://www.example.org/iiif/book1/range/1",
        "@type":"sc:Range",
        "canvases": [
          "http://www.example.org/iiif/book1/canvas/1",
          "http://www.example.org/iiif/book1/canvas/2",
          "http://www.example.org/iiif/book1/canvas/3#xywh=0,0,750,300"
        ]
    }
  ]
}

in the JSON-LD playground results in the correct interpretation of "canvases" as a list of resources, where as the rdflib implementation results in a list of literals. The turtle serialization of the json-ld:

@prefix sc: <http://www.shared-canvas.org/ns#> .

<http://example.org/iiif/seq/1> sc:contents ( <http://www.example.org/iiif/book1/range/1> ) .

<http://www.example.org/iiif/book1/range/1> a sc:Range ;
    sc:contents ( "http://www.example.org/iiif/book1/canvas/1" "http://www.example.org/iiif/book1/canvas/2" "http://www.example.org/iiif/book1/canvas/3#xywh=0,0,750,300" ) .

The URIs above should be resources, not literal strings. The expected result would be:

@prefix sc: <http://www.shared-canvas.org/ns#> .

<http://example.org/iiif/seq/1> sc:contents ( <http://www.example.org/iiif/book1/range/1> ) .

<http://www.example.org/iiif/book1/range/1> a sc:Range ;
    sc:contents ( <http://www.example.org/iiif/book1/canvas/1> <http://www.example.org/iiif/book1/canvas/2> <http://www.example.org/iiif/book1/canvas/3#xywh=0,0,750,300> ) .

DOC: usage with rdfpipe and compaction

How to use this from the command line with rdfpipe to convert from one format to another (with optional {compaction,}) could be helpfully added to README.rst.

Is there a plan to create a new PyPI release?

I'm keen to start using the updates created for issue #33 in my software, but for installation I need to be able to install dependencies from PyPI.

I could create a private copy and use that, but gratuitous forking of the in-use codebase seems not the best thing to be doing here.

use the default namespaces in generating context for json-ld output from rdflib

import rdflib as rl
g = rl.Graph()
g.bind('ex', 'http://example.org/')
g.bind('ex2', 'http://example2.org/')
g.add((rl.term.URIRef('http://example.org/a'), rl.RDF.type, rl.term.URIRef('http://example2.org/a')))
print(g.serialize(format='turtle').decode())
print(g.serialize(format='json-ld').decode())

returns

@prefix ex: <http://example.org/> .
@prefix ex2: <http://example2.org/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

ex:a a ex2:a .

and

[
  {
    "@id": "http://example.org/a",
    "@type": [
      "http://example2.org/a"
    ]
  }
]

it would be nice if the default prefixes could automatically be used for generating the context internally.

Can't serialize unicode characters properly

Origin string:"一切事务的基类”
after serilizing: "\uxxxx\uxxxx\uxxx...."

need to serilize it in origin

Maybe I didn't find to right parameters to control it.
if there is one, thank for tell me it.

Example JSON-LD does not work -- bad @context?

The example JSON-LD on page
https://github.com/RDFLib/rdflib-jsonld/blob/master/docs/jsonld-parser.rst
does not produce any RDF triples. Currently it is:

{
    "@context": {
        "dc": "http://purl.org/dc/terms/",
        "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
        "rdfs": "http://www.w3.org/2000/01/rdf-schema#"
    },
    "@id": "http://example.org/about",
    "dc:title": {
        "@language": "en",
        "@literal": "Someone's Homepage"
    }
}

It should be something like:

{
    "@context": {
        "@language": "en",
        "@vocab": "http://purl.org/dc/terms/"
    },
    "@id": "http://example.org/about",
    "title": "Someone's Homepage"
}

dependency on rdflib 4.1

I've just made some debian packaging for rdflib-jsonld 0.3 in jessie.

Everything seems to work fine except that I had to lower the minimum version of rdflib required because only rdflib 4.1 is available in debian/jessie.

The test suite seems to run fine with rdflib 4.1, why is 4.2 explicitly required ?

blank node id serialization lacks prefix

I've noticed a problem with Blank Node serialization when a context is included. Here's an example that should demonstrate it:

from rdflib import URIRef, BNode, Literal, ConjunctiveGraph, RDFS
from rdflib.plugin import register, Serializer

register( 'json-ld', Serializer, 'rdflib_jsonld.serializer', 'JsonLDSerializer')

g = ConjunctiveGraph()
b = BNode()

g.add((URIRef('http://example.com'), URIRef('http://example.com/ns/has'), b))
g.add((b, RDFS.label, Literal('foo')))

context = {
    "has": {
        "@type": "@id",
        "@id": "http://example.com/ns/has"
    }
}

print g.serialize(format="json-ld", context=context)

When you run this, you get this output, where the Blank Node identifier lacks the _: prefix, and the label assertion is lost?

{
  "@context": {
    "has": {
      "@id": "http://example.com/ns/has",
      "@type": "@id"
    }
  },
  "@id": "http://example.com",
  "has": "N048ab6e6426448408ee34e29e8c38602"
}

Turning rdflib-jsonld into a "full processor" (a.o. for schema.org compliance)

The JSON-LD 1.1 draft spec mentions different levels of processing for JSON-LD: https://w3c.github.io/json-ld-syntax/#processor-levels

A pure processor can only parse JSON-LD expressed in JSON directly, but a full processor can also parse JSON-LD embedded in HTML.

It would be great if rdflib-jsonld would support this. It would make rdflib-jsonld a library that could be used for HTML documents following the schema.org guidelines for embedding (meta)data in HTML pages as described in their getting started guide https://schema.org/docs/gs.html.

Together with the RDFa & microdata parsers this can then work as a fully RDF based version of the Structured Data Testing tool from Google: https://search.google.com/structured-data/testing-tool.

Triples not loaded when using @context and @graph

I tried converting the following rdfa to jsonld on rdf translator.

<body>   
<div typeof="rdfs:Class" resource="http://schema.org/CreativeWork">
      <span class="h" property="rdfs:label">CreativeWork</span>
      <span property="rdfs:comment">The most generic kind of creative work, including books, movies, photographs, software programs, etc.</span>
       <span>Subclass of: <a property="rdfs:subClassOf" href="http://schema.org/Thing">Thing</a></span>
       <span>Source:  <a property="dc:source" href="http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_rNews">rNews</a></span>
    </div>

    <div typeof="rdfs:Class" resource="http://schema.org/WebPage">
      <span class="h" property="rdfs:label">WebPage</span>
      <span property="rdfs:comment">A web page. Every web page is implicitly assumed to be declared to be of type WebPage, so the various properties about that webpage, such as &lt;code&gt;breadcrumb&lt;/code&gt; may be used. We recommend explicit declaration if these properties are specified, but if they are found outside of an itemscope, they will be assumed to be about the page.</span>
       <span>Subclass of: <a property="rdfs:subClassOf" href="http://schema.org/CreativeWork">CreativeWork</a></span>
    </div>
</body>

The result was the following -

{
  "@context": {
    "dc": "http://purl.org/dc/terms/",
    "dcterms": "http://purl.org/dc/terms/",
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "rdfs": "http://www.w3.org/2000/01/rdf-schema#",
    "schema": "http://schema.org/",
    "xsd": "http://www.w3.org/2001/XMLSchema#"
  },
  "@graph": [
    {
      "@id": "schema:CreativeWork",
      "@type": "rdfs:Class",
      "dcterms:source": {
        "@id": "http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_rNews"
      },
      "rdfs:comment": "The most generic kind of creative work, including books, movies, photographs, software programs, etc.",
      "rdfs:label": "CreativeWork",
      "rdfs:subClassOf": {
        "@id": "schema:Thing"
      }
    },
    {
      "@id": "schema:WebPage",
      "@type": "rdfs:Class",
      "rdfs:comment": "A web page. Every web page is implicitly assumed to be declared to be of type WebPage, so the various properties about that webpage, such as <code>breadcrumb</code> may be used. We recommend explicit declaration if these properties are specified, but if they are found outside of an itemscope, they will be assumed to be about the page.",
      "rdfs:label": "WebPage",
      "rdfs:subClassOf": {
        "@id": "schema:CreativeWork"
      }
    }
  ]
}

However, when I try to load this json-ld using the following

import rdflib
g = rdflib.Graph()
# converted.jsonld is the file I saved the above output to.
g.load("converted.jsonld", format="json-ld")
list(g.triples((None, None, None)))
# the output is []

DOC: README.rst: Add JSON-LD wikipedia, homepage, standards links

JSON-LD
---------------
https://en.wikipedia.org/wiki/JSON-LD

* http://json-ld.org/
* http://json-ld.org/learn.htm
* http://json-ld.org/playground/index.html

Web Standards

* http://www.w3.org/TR/json-ld/
* http://www.w3.org/TR/json-ld-api/
* http://lov.okfn.org/dataset/lov/
* http://schema.org/docs/schemas.html

Convert Json to Jsonld: Don't get the properties

Hi all,

I have this simple json file and i wantt to convert it to a jsonld format with the below python program[1]:

[
  {
   "Event":{
            "10924":"1"
        }
  },
  {
   "Event":"2"
  }
] 

[1]

import json
from rdflib import Graph, plugin, ConjunctiveGraph
import json, rdflib_jsonld
from rdflib.plugin import register, Serializer
register('json-ld', Serializer, 'rdflib_jsonld.serializer', 'JsonLDSerializer')

context = {
         "@context": {
           "foaf" : "http://xmlns.com/foaf/0.1/",

           "@id":"http://localhost",
           "Event": {
               "@id": "http://localhost/index.php/Event",
               "@type": "foaf:Event",
               "10924": {
                       "@id": "foaf:run",
               }
           }
       }
}
x=json.loads(open('Json_File_test.txt.Short').read())
g = ConjunctiveGraph()
g.parse(data=json.dumps(x), format='json-ld', context=context)
g.serialize("event.jsonld", format='json-ld')
g.serialize("pruebajson3.rdf")
g.serialize("pruebajson3.nt", format='nt')
print(g.serialize(format='json-ld').decode())
g.close()

But i can't get the subproperty "10924", only the "Event" property in the json-ld output:

[
  {
    "@id": "_:Nf89b993bb7974a7da878a69fe81dd442",
    "http://localhost/index.php/Event": [
      {
        "@id": "_:N6c48733f5b6d4271b760c598421bbe61"
      }
    ]
  },
  {
    "@id": "_:N7e5db391ab3b4ff6b31f1938aa14abee",
    "http://localhost/index.php/Event": [
      {
        "@type": "http://xmlns.com/foaf/0.1/Event",
        "@value": "2"
      }
    ]
  },
  {
    "@id": "_:N6c48733f5b6d4271b760c598421bbe61"
  }
]

I can't figure it out what i left in the program, do i have to create a new context for the subproperty "10924"?

Thank you so much in advance.

Kind Regards!

CLI

Can this library be used from the CLI, for example to convert a file from XML to JSON-LD?

Use the correct mime-type

When parsing remote URIs rdflib sets the accept header to 'application/rdf+xml,text/rdf+n3;q=0.9,application/xhtml+xml;q=0.5, /;q=0.1' despite specifying 'format="json-ld"'. Should be something along the lines of 'application/ld+json,application/json'...

Reading slashless @context URLs adds a slash where it doesn't belong

Hi - i am not sure if this is bug in the json-ld parser itself, but i am seeing incorrect interpretation of URLs that lack a forward slash. It looks like the parser is overenthusiastic about adding slashes where they don't belong.

Consider this example:

from rdflib import ConjunctiveGraph
# references rdflib-jsonld v0.4.0

context = {
    "@context": {
        "@base": "https://stone.bogus/",
        "Fred": "flint:Fred",
        "flint": "https://flint.bogus#",
        "stone": "https://stone.bogus/",
        "barney": "flint:barney"
    }
}

content = '{"@id": "yyy", "barney": {"@id": "xxx", "@type": "Fred"}}'

g = ConjunctiveGraph()
g.parse(data=content, context=context, format='json-ld')

output = g.serialize(format='json-ld', context=context, indent=4)
print(str(output))

The output (whatever the output serialization the behavior is consistent, so this appears to be happening inbound):

{
    "@context": {
        "@context": {
            "@base": "https://stone.bogus/",
            "Fred": "flint:Fred",
            "barney": "flint:barney",
            "flint": "https://flint.bogus#",
            "stone": "https://stone.bogus/"
        }
    },
    "@graph": [
        {
            "@id": "stone:xxx",
            "@type": "https://flint.bogus/#Fred"
        },
        {
            "@id": "stone:yyy",
            "barney": {
                "@id": "stone:xxx"
            }
        }
    ]
}

The resource stone:xxx gets an extra / in its @type. This should be:

            "@type": "https://flint.bogus#Fred"

or

            "@type": "Fred"

This happens for any URL that has no forward slash following the domain. For example, these are both OK: http://flint.com/Stone#Fred and http://flint.com/.

The culprit appears to be the function util.norm_url, specifically the bits that seem to be undermining the work of urljoin:

def norm_url(base, url):
    """
    >>> norm_url('http://example.org/', '/one')
    'http://example.org/one'
    >>> norm_url('http://example.org/', '/one#')
    'http://example.org/one#'
    >>> norm_url('http://example.org/one', 'two')
    'http://example.org/two'
    >>> norm_url('http://example.org/one/', 'two')
    'http://example.org/one/two'
    >>> norm_url('http://example.org/', 'http://example.net/one')
    'http://example.net/one'
    """
    parts = urlsplit(urljoin(base, url))
    path = normpath(parts[2])
    if sep != '/':
        path = '/'.join(path.split(sep))
    if parts[2].endswith('/') and not path.endswith('/'):
        path += '/'
    result = urlunsplit(parts[0:2] + (path,) + parts[3:])
    if url.endswith('#') and not result.endswith('#'):
        result += '#'
    return result

Thanks in advance,
Patrick

Context does not support to_dict()

When attempting to use an reference to a context or when creating a populated instance of Context with a dictionary, the JsonLDSerializer attempts to extract the Context to a dictionary by calling to_dict() as shown below:
if isinstance(context_data, Context):
context = context_data
context_data = context.to_dict()
else:
context = Context(context_data, base=base)

The call to_dict() results in an error since Context does not contain a method to_dict()

incorrect dealing with blank nodes

Take the following example:

@Prefix ex: http://example.org/ .

_:b1 a ex:Blank.

ex:item1 a ex:Item ;
ex:hasBlank _:b1 .

ex:item2 a ex:Item ;
ex:hasBlank _:b1 .

The expected output would be something like:
{
"@context": {
"ex": "http://example.org/"
},
"@graph": [
{
"@id": ":b1",
"@type": "ex:Blank"
},
{
"@id": "ex:item1",
"@type": "ex:Item",
"ex:hasBlank": {
"@id": "
:b1"
}
},
{
"@id": "ex:item2",
"@type": "ex:Item",
"ex:hasBlank": {
"@id": "_:b1"
}
}
]
}

Instead, the serializer generates the following, obviously incorrect (blank nodes are not distinguished properly), output:
{
"@context": {
"ex": "http://example.org/"
},
"@id": [
{
"@id": "ex:item2",
"@type": "ex:Item",
"ex:hasBlank": {
"@type": "ex:Blank"
}
},
{
"@id": "ex:item1",
"@type": "ex:Item",
"ex:hasBlank": {
"@type": "ex:Blank"
}
}
]
}

Serialized @type info if it is present

See that if type is one of

PLAIN_LITERAL_TYPES = set([XSD.integer, XSD.float, XSD.double, XSD.decimal,
        XSD.boolean, XSD.string])

They are skipped in the serialization process. Any good reason for that?
It gives me issue in my case, since I rely on the serialized data and after further processed, will create new RDF graph.. without the type info, the parser won’t be able to tell what type the data is and next time when serialized, won’t have the type info.
Right now I comment it out and force it to serialize all type info as long as it is present. That is more consistent,,'

Where range is a uri but value is string, parser prefixes file://path

It's great that the parser is aware of range types for schema.org properties, but it is a feature I'd like to turn off.

For example, Google's Structured Data Testing Tool accepts string values for schema:isBasedOn and schema:mainEntityOfPage, but the ld+json parser on finding a string value is prepending file://local/file/path/ to the string and entering it as an (often invalid) URIRef.

It also exposes the filesystem structure of the host machine, but of course it fails as soon as the Graph is serialized. True, the value type is technically in error, however I'm not sure conversion to file:// urls is the best solution, especially with the full local filesystem path.

Is it possible to relax this datatype conversion/constraint feature? Issue type-conflict warnings instead of changing the data? Or could I avoid it with an empty @base?

Fails with "http://schema.org/" context

Using this toy json-ld from the playground the following code fails:

>>> from rdflib import Graph
>>> test_json="""
... {
...   "@context": "http://schema.org/",
...   "@type": "Person",
...   "name": "Jane Doe",
...   "jobTitle": "Professor",
...   "telephone": "(425) 123-4567",
...   "url": "http://www.janedoe.com"
... }"""
>>> Graph().parse(data=test_json, format='json-ld')
<snip long traceback>
ValueError: No JSON object could be decoded

This culprit seems to be that the schema.org context cannot be obtained properly.

>>> from rdflib_jsonld.util import source_to_json
>>> source_to_json("http://schema.org/")
ValueError: No JSON object could be decoded

which seems to be the "fault" of the corresponding function from rdflib

>>> from rdflib.parser import create_input_source
>>> create_input_source("http://schema.org/").getByteStream().read()[:50]
'<!DOCTYPE html>\n<html xmlns="http://www.w3.org/199'

It yields the html variant of that page, whereas it should likely do something like this:
https://github.com/digitalbazaar/pyld/blob/master/lib/pyld/jsonld.py#L385
and request 'application/json' from the server.

Apologies if that assessment is wrong. I decided to report it here and not against rdflib, as this issues seems to be json-focused (not RDF) and schema.org looks like a relevant component of the jsonld ecosystem.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.