forked from linkeddata/rdflib.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonparser.js
53 lines (53 loc) · 1.72 KB
/
jsonparser.js
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
$rdf.jsonParser = (function () {
return {
parseJSON: function (data, source, store) {
var subject, predicate, object
var bnodes = {}
var why = store.sym(source)
for (var x in data) {
if (x.indexOf('_:') === 0) {
if (bnodes[x]) {
subject = bnodes[x]
} else {
subject = store.bnode(x)
bnodes[x] = subject
}
} else {
subject = store.sym(x)
}
var preds = data[x]
for (var y in preds) {
var objects = preds[y]
predicate = store.sym(y)
for (var z in objects) {
var obj = objects[z]
if (obj.type === 'uri') {
object = store.sym(obj.value)
store.add(subject, predicate, object, why)
} else if (obj.type === 'bnode') {
if (bnodes[obj.value]) {
object = bnodes[obj.value]
} else {
object = store.bnode(obj.value)
bnodes[obj.value] = object
}
store.add(subject, predicate, object, why)
} else if (obj.type === 'literal') {
var datatype
if (obj.datatype) {
object = store.literal(obj.value, undefined, store.sym(obj.datatype))
} else if (obj.lang) {
object = store.literal(obj.value, obj.lang)
} else {
object = store.literal(obj.value)
}
store.add(subject, predicate, object, why)
} else {
throw new Error('error: unexpected termtype: ' + z.type)
}
}
}
}
}
}
})()