diff options
author | Gunnar Aastrand Grimnes <gromgull@gmail.com> | 2017-02-02 13:04:02 +0100 |
---|---|---|
committer | Gunnar Aastrand Grimnes <gromgull@gmail.com> | 2017-02-02 13:14:30 +0100 |
commit | a621aa2801fec3f5da7537f0095c40b6b2a47963 (patch) | |
tree | e6b87f645e07461684423197504729607bd044d0 | |
parent | 2ff5a7414110729f2d396b15db8460cb66107ec1 (diff) | |
download | rdflib-a621aa2801fec3f5da7537f0095c40b6b2a47963.tar.gz |
fixed support for datatyped literals in SPARQL json results
the old version was many years outdated, `typed-literal` was last
seen in the working group note from 2006: https://www.w3.org/TR/2006/NOTE-rdf-sparql-json-res-20061004/
-rw-r--r-- | rdflib/plugins/sparql/results/jsonresults.py | 20 |
1 files changed, 8 insertions, 12 deletions
diff --git a/rdflib/plugins/sparql/results/jsonresults.py b/rdflib/plugins/sparql/results/jsonresults.py index 4bffb515..755cca48 100644 --- a/rdflib/plugins/sparql/results/jsonresults.py +++ b/rdflib/plugins/sparql/results/jsonresults.py @@ -103,9 +103,7 @@ def parseJsonTerm(d): if t == 'uri': return URIRef(d['value']) elif t == 'literal': - if 'xml:lang' in d: - return Literal(d['value'], lang=d['xml:lang']) - return Literal(d['value']) + return Literal(d['value'], datatype=d.get('datatype'), lang=d.get('xml:lang')) elif t == 'typed-literal': return Literal(d['value'], datatype=URIRef(d['datatype'])) elif t == 'bnode': @@ -118,16 +116,14 @@ def termToJSON(self, term): if isinstance(term, URIRef): return {'type': 'uri', 'value': text_type(term)} elif isinstance(term, Literal): + r = {'type': 'literal', + 'value': text_type(term)} + if term.datatype is not None: - return {'type': 'typed-literal', - 'value': text_type(term), - 'datatype': text_type(term.datatype)} - else: - r = {'type': 'literal', - 'value': text_type(term)} - if term.language is not None: - r['xml:lang'] = term.language - return r + r['datatype'] = text_type(term.datatype) + if term.language is not None: + r['xml:lang'] = term.language + return r elif isinstance(term, BNode): return {'type': 'bnode', 'value': str(term)} |