forked from IKS/rdfQuery
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.rdf.json.js
84 lines (82 loc) · 2.39 KB
/
jquery.rdf.json.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
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
79
80
81
82
83
84
/*
* jQuery RDF @VERSION
*
* Copyright (c) 2008,2009 Jeni Tennison
* Licensed under the MIT (MIT-LICENSE.txt)
*
* Depends:
* jquery.uri.js
* jquery.xmlns.js
* jquery.datatype.js
* jquery.curie.js
* jquery.rdf.js
* jquery.json.js
*/
/**
* @fileOverview jQuery RDF/JSON parser
* @author <a href="mailto:[email protected]">Jeni Tennison</a>
* @copyright (c) 2008,2009 Jeni Tennison
* @license MIT license (MIT-LICENSE.txt)
* @version 1.0
*/
/**
* @exports $ as jQuery
*/
/**
* @ignore
*/
(function ($) {
$.rdf.parsers['application/json'] = {
parse: $.secureEvalJSON,
serialize: $.toJSON,
triples: function (data) {
var s, subject, p, property, o, object, i, opts, triples = [];
for (s in data) {
subject = (s.substring(0, 2) === '_:') ? $.rdf.blank(s) : $.rdf.resource('<' + s + '>', {base : this.baseURI});
for (p in data[s]) {
property = $.rdf.resource('<' + p + '>', {base : this.baseURI});
for (i = 0; i < data[s][p].length; i += 1) {
o = data[s][p][i];
if (o.type === 'uri') {
object = $.rdf.resource('<' + o.value + '>', {base : this.baseURI});
} else if (o.type === 'bnode') {
object = $.rdf.blank(o.value);
} else {
// o.type === 'literal'
if (o.datatype !== undefined) {
object = $.rdf.literal(o.value, { datatype: o.datatype, base : this.baseURI });
} else {
opts = {};
if (o.lang !== undefined) {
opts.lang = o.lang;
}
var escapedValue = typeof o.value === "string" ? o.value.replace(/\"/g,'\\"') : o.value;
opts.base = this.baseURI;
object = $.rdf.literal('"' + escapedValue + '"', opts);
}
}
triples.push($.rdf.triple(subject, property, object));
}
}
}
return triples;
},
dump: function (triples) {
var e = {},
i, t, s, p;
for (i = 0; i < triples.length; i += 1) {
t = triples[i];
s = t.subject.value.toString();
p = t.property.value.toString();
if (e[s] === undefined) {
e[s] = {};
}
if (e[s][p] === undefined) {
e[s][p] = [];
}
e[s][p].push(t.object.dump());
}
return e;
}
};
})(jQuery);