forked from RDFLib/rdflib-rdfjson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
78 lines (68 loc) · 2.03 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from rdflib import Graph, plugin, Namespace
from rdflib.parser import Parser
from rdflib.serializer import Serializer
plugin.register("rdf-json", Parser,
"rdflib_rdfjson.rdfjson_parser", "RdfJsonParser")
plugin.register("rdf-json", Serializer,
"rdflib_rdfjson.rdfjson_serializer", "RdfJsonSerializer")
plugin.register("rdf-json-pretty", Serializer,
"rdflib_rdfjson.rdfjson_serializer", "PrettyRdfJsonSerializer")
testrdfjson = '''{
"http://example.org/about" :
{
"http://purl.org/dc/elements/1.1/title": [
{ "type" : "literal" , "value" : "Anna's Homepage." }
]
}
}'''
g = Graph()
g.bind("dc", "http://purl.org/dc/elements/1.1/")
g.parse(data=testrdfjson, format="rdf-json")
rdfxml = g.serialize(format="xml")
assert '''Anna's Homepage''' in rdfxml
print(rdfxml)
print("""<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
>
<rdf:Description rdf:about="http://example.org/about">
<dc:title>Anna's Homepage.</dc:title>
</rdf:Description>
</rdf:RDF>""")
testrdfn3 = '''
<http://example.org/about>
<http://purl.org/dc/elements/1.1/title>
"Anna's Homepage" .'''
g = Graph()
g.bind("dc", "http://purl.org/dc/elements/1.1/")
g.bind("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
g.parse(data=testrdfn3, format="n3")
rdfjson = g.serialize(None, format="rdf-json")
assert '''"Anna's Homepage"''' in rdfjson
print(rdfjson)
print("""{
"http://example.org/about": {
"http://purl.org/dc/elements/1.1/title": [
{
"type": "literal",
"value": "Anna's Homepage"
}
]
}
}""")
prettyrdfjson = g.serialize(None, format="rdf-json-pretty")
assert '''"Anna's Homepage"''' in prettyrdfjson
print(prettyrdfjson)
print("""{
"xmlns$rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"http://example.org/about": {
"dc$title": [
{
"type": "literal",
"value": "Anna's Homepage"
}
]
},
"xmlns$dc": "http://purl.org/dc/elements/1.1/"
}""")