-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (53 loc) · 1.42 KB
/
index.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
const rdf = require('rdf-ext')
const SimpleRDF = require('simplerdf-core')
const terms = {
type: rdf.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type')
}
function fromJSONValue (simple, value) {
if (typeof value === 'object') {
if (typeof value.iri === 'function') {
return value
}
if (SimpleRDF.isArray(value)) {
return value
}
return simple.child().fromJSON(value)
} else {
return value
}
}
class SimpleFromJson {
fromJSON (json) {
if (!json) {
return this
}
Object.keys(json).forEach((property) => {
const value = json[property]
if (property === '@id') {
this.iri(value)
} else if (property === '@type') {
const graph = this.graph()
if (Array.isArray(value)) {
value.forEach((v) => {
graph.add(rdf.quad(this.iri(), terms.type, rdf.namedNode(v)))
})
} else {
graph.add(rdf.quad(this.iri(), terms.type, rdf.namedNode(value)))
}
} else {
if (Array.isArray(value)) {
value.forEach((item) => {
this[property].push(fromJSONValue(this, item))
})
} else {
this[property] = fromJSONValue(this, value)
}
}
})
return this
}
static fromJSON (json, context, iri, graph, options) {
return (new this(context, iri, graph, options)).fromJSON(json)
}
}
module.exports = SimpleFromJson