diff options
author | nicholascar <nicholas.car@surroundaustralia.com> | 2021-12-04 17:38:04 +1000 |
---|---|---|
committer | nicholascar <nicholas.car@surroundaustralia.com> | 2021-12-04 17:38:04 +1000 |
commit | 1f565ef9c4194c95a21cbe6697e477ef182740f9 (patch) | |
tree | 72620eb2bc136b81e9db326bede273c31a2b604b | |
parent | 6c47908c026112adecc2fc31dda5c34bf9894a00 (diff) | |
download | rdflib-1f565ef9c4194c95a21cbe6697e477ef182740f9.tar.gz |
fixed boolean JSOn representation
-rw-r--r-- | rdflib/plugins/serializers/hext.py | 34 | ||||
-rw-r--r-- | test/test_serialize_hext.py | 18 |
2 files changed, 44 insertions, 8 deletions
diff --git a/rdflib/plugins/serializers/hext.py b/rdflib/plugins/serializers/hext.py index b1561959..6977e1b0 100644 --- a/rdflib/plugins/serializers/hext.py +++ b/rdflib/plugins/serializers/hext.py @@ -55,7 +55,7 @@ def _hex_line(triple, context): return "[%s, %s, %s, %s, %s, %s]\n" % ( _iri_or_bn(triple[0]), _iri_or_bn(triple[1]), - triple[2] if type(triple[2]) == Literal else _iri_or_bn(triple[2]), + _literal(triple[2]) if type(triple[2]) == Literal else _iri_or_bn(triple[2]), (f'"{triple[2].datatype}"' if triple[2].datatype is not None else '""') if type(triple[2]) == Literal else '""', (f'"{triple[2].language}"' if triple[2].language is not None else '""') if type(triple[2]) == Literal else '""', _iri_or_bn(context) @@ -67,3 +67,35 @@ def _iri_or_bn(i_): return f"\"{i_}\"" else: return f"\"{i_.n3()}\"" + + +def _literal(i_): + raw_datatype = [ + "http://www.w3.org/2001/XMLSchema#integer", + "http://www.w3.org/2001/XMLSchema#long", + "http://www.w3.org/2001/XMLSchema#int", + "http://www.w3.org/2001/XMLSchema#short", + "http://www.w3.org/2001/XMLSchema#positiveInteger", + "http://www.w3.org/2001/XMLSchema#negativeInteger", + "http://www.w3.org/2001/XMLSchema#nonPositiveInteger", + "http://www.w3.org/2001/XMLSchema#nonNegativeInteger", + "http://www.w3.org/2001/XMLSchema#unsignedLong", + "http://www.w3.org/2001/XMLSchema#unsignedInt", + "http://www.w3.org/2001/XMLSchema#unsignedShort", + + "http://www.w3.org/2001/XMLSchema#float", + "http://www.w3.org/2001/XMLSchema#double", + "http://www.w3.org/2001/XMLSchema#decimal", + + "http://www.w3.org/2001/XMLSchema#boolean" + ] + if hasattr(i_, "datatype"): + if str(i_.datatype) in raw_datatype: + return f"{i_}" + else: + return f"\"{i_}\"" + else: + if str(i_) in ["true", "false"]: + return f"{i_}" + else: + return f"\"{i_}\"" diff --git a/test/test_serialize_hext.py b/test/test_serialize_hext.py index 0d6fe992..9c6addcb 100644 --- a/test/test_serialize_hext.py +++ b/test/test_serialize_hext.py @@ -19,6 +19,8 @@ def test_hext_graph(): ex:p4 "2021-12-03"^^xsd:date ; ex:p5 42 ; ex:p6 "42" ; + ex:p7 true ; + ex:p8 "false"^^xsd:boolean ; . """ @@ -26,10 +28,13 @@ def test_hext_graph(): out = g.serialize(format="hext") testing_lines = [ [False, '["http://example.com/s1", "http://example.com/p1", "http://example.com/o2", "", ""'], - [False, '["http://example.com/s1", "http://example.com/p3", Object 3, "", ""'], + [False, '["http://example.com/s1", "http://example.com/p3", "Object 3", "", ""'], [False, '["http://example.com/s1", "http://example.com/p5", 42, "http://www.w3.org/2001/XMLSchema#integer", ""'], - [False, '"http://www.w3.org/1999/02/22-rdf-syntax-ns#value", thingy, "", ""'], - [False, '["http://example.com/s1", "http://example.com/p4", 2021-12-03, "http://www.w3.org/2001/XMLSchema#date", ""'] + [False, '"http://www.w3.org/1999/02/22-rdf-syntax-ns#value", "thingy", "", ""'], + [False, '["http://example.com/s1", "http://example.com/p4", "2021-12-03", "http://www.w3.org/2001/XMLSchema#date", ""'], + [False, '["http://example.com/s1", "http://example.com/p6", "42", "", ""'], + [False, '["http://example.com/s1", "http://example.com/p7", true, "http://www.w3.org/2001/XMLSchema#boolean", ""'], + [False, '["http://example.com/s1", "http://example.com/p8", false, "http://www.w3.org/2001/XMLSchema#boolean", ""'], ] for line in out.splitlines(): for test in testing_lines: @@ -71,16 +76,15 @@ def test_hext_dataset(): # default graph triples ex:s1 ex:p1 ex:o1 , ex:o2 . ex:s21 ex:p21 ex:o21 , ex:o22 . - """ d.parse(data=trig_data, format="trig") out = d.serialize(format="hext") testing_lines = [ [False, '["http://example.com/s1", "http://example.com/p1", "http://example.com/o2", "", "", "http://example.com/g2"]'], - [False, '["http://example.com/s1", "http://example.com/p3", Object 3, "", "", "http://example.com/g1"]'], + [False, '["http://example.com/s1", "http://example.com/p3", "Object 3", "", "", "http://example.com/g1"]'], [False, '["http://example.com/s1", "http://example.com/p5", 42, "http://www.w3.org/2001/XMLSchema#integer", "", "http://example.com/g1"]'], - [False, '"http://www.w3.org/1999/02/22-rdf-syntax-ns#value", thingy, "", "", "http://example.com/g1"]'], - [False, '["http://example.com/s1", "http://example.com/p4", 2021-12-03, "http://www.w3.org/2001/XMLSchema#date", "", "http://example.com/g1"]'] + [False, '"http://www.w3.org/1999/02/22-rdf-syntax-ns#value", "thingy", "", "", "http://example.com/g1"]'], + [False, '["http://example.com/s1", "http://example.com/p4", "2021-12-03", "http://www.w3.org/2001/XMLSchema#date", "", "http://example.com/g1"]'] ] for line in out.splitlines(): for test in testing_lines: |