-
Notifications
You must be signed in to change notification settings - Fork 4
/
jsonld.js
125 lines (108 loc) · 3.53 KB
/
jsonld.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* Copyright 2014 IBM Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Parses and serializes JSON-LD (application/ld+json) to and from an array of
* triples, the triple format used by N3.js.
*/
var N3 = require('n3');
var jsonld = require('jsonld');
var media = require('./media.js'); // media types
var ldp = require('./vocab/ldp.js'); // LDP vocabulary
var rdf = require('./vocab/rdf.js'); // RDF vocabulary
var context = require('./context.json'); // our JSON-LD context
exports.parse = function (req, resourceURI, callback) {
var json = JSON.parse(req.rawBody);
jsonld.toRDF(json, { base: resourceURI }, function(err, dataset) {
if (err) {
callback(err);
return;
}
// transform the dataset to the N3.js triples format we use in our database
// both libraries use a different internal format unfortunately
var result = [];
for(var graphName in dataset) {
var triples = dataset[graphName];
// FIXME: what about graph names?
triples.forEach(function (triple) {
var next = {};
next.subject = triple.subject.value;
next.predicate = triple.predicate.value;
if (triple.object.type === 'IRI' || triple.object.type === 'blank node') {
next.object = triple.object.value;
} else {
var literal = '"' + triple.object.value + '"';
if (triple.object.language) {
literal += '@' + triple.object.language;
} else if (triple.object.datatype && triple.object.datatype !== 'http://www.w3.org/2001/XMLSchema#string') {
literal += '^^<' + triple.object.datatype + '>';
}
next.object = literal;
}
result.push(next);
});
}
callback(null, result);
});
};
function jsonldResource(subject) {
return { '@id': subject };
}
function jsonldObject(object) {
if (N3.Util.isUri(object) || N3.Util.isBlank(object)) {
return jsonldResource(object);
}
var result = {};
var value = N3.Util.getLiteralValue(object);
result['@value'] = value;
var type = N3.Util.getLiteralType(object);
if (type && type !== 'http://www.w3.org/2001/XMLSchema#string') {
result['@type'] = type;
}
var language = N3.Util.getLiteralLanguage(object);
if (language) {
result['@language'] = language;
}
return result;
}
exports.serialize = function(triples, callback) {
var resources = [];
var map = {};
triples.forEach(function(triple) {
var sub = map[triple.subject];
if (!sub) {
sub = jsonldResource(triple.subject);
map[triple.subject] = sub;
resources.push(sub);
}
var object;
if ((N3.Util.isUri(triple.object) || N3.Util.isBlank(triple.object)) && !map[triple.object]) {
object = jsonldResource(triple.object);
}
if (triple.predicate === rdf.type) {
jsonld.addValue(sub, '@type', triple.object, { propertyIsArray: true });
return;
}
object = jsonldObject(triple.object);
jsonld.addValue(sub, triple.predicate, object, { propertyIsArray: true });
});
jsonld.compact(resources, context, function(err, json) {
if (err) {
callback(err);
}
var content = JSON.stringify(json, undefined, 4);
callback(null, media.jsonld, content);
});
};